猫鼬与Atlas上的ReplicaSet [英] Mongoose with ReplicaSet on Atlas

查看:64
本文介绍了猫鼬与Atlas上的ReplicaSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB Atlas上设置了一个副本,这是我的mongo shell连接字符串,可以完美连接:

I have a replica set on MongoDB Atlas and this is my mongo shell connection string which connects perfectly:

$ mongo "mongodb://MY_SERVER-shard-00-00-clv3h.mongodb.net:27017,MY_SERVER-shard-00-01-clv3h.mongodb.net:27017,MY_SERVER-shard-00-02-clv3h.mongodb.net:27017/MY_DATABASE?replicaSet=MY_REPLICASET-NAME-shard-0" --ssl --username MY_USERNAME --password MY_PASSWORD --authenticationDatabase MY_ADMIN_DATABASE

如何将其转换为在猫鼬中使用?如何建立我的uri和options变量?

How Can I convert it to use in mongoose? How Can I build my uri and options variable?

我尝试了以下操作,但未成功:

I tried the following without success:

  // connection string using mongoose:
  var uri = 'mongodb://MY_USER:MY_PASSWORD@' +
    'MY_SERVER-shard-00-00-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-01-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-02-clv3h.mongodb.net:27017/MY_DATABASE';

  var options = {
    replset: {
      ssl: true,
      authSource: 'MY_ADMIN_DATABASE',
      rs_name: 'MY_REPLICASET_NAME-shard-0'
    }
  };

  mongoose.connect(uri, options);
  var db = mongoose.connection;

我尝试过包括user:并在选项上传递:,从uri删除MY_USER:MY_PASSWORD @,将rs_name更改为copyaSet,每次尝试均不成功.似乎猫鼬没有考虑使用authSource选项.

I've tried including user: and pass: on options, removing MY_USER:MY_PASSWORD@ from uri, change rs_name to replicaSet, every unsuccessful attempt. It seems that mongoose is not considering the authSource option.

使用mongojs,它可以与以下代码配合使用:

Using the mongojs, it works fine with the following code:

  // connection string using mongojs:
  var uri = 'mongodb://MY_USER:MY_PASSWORD@' +
    'MY_SERVER-shard-00-00-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-01-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-02-clv3h.mongodb.net:27017/MY_DATABASE';

  var options = {
    ssl: true,
    authSource: 'MY_ADMIN_DATABASE',
    replicaSet: 'MY_REPLICASET_NAME-shard-0'
  };

  var db = mongojs(uri,'', options);

但是,我需要使用猫鼬,因为我的项目中存在ODM.

But, I need to use mongoose because the ODM in my project.

如何使用猫鼬构建uri和options变量?

How can I build my uri and options variable using mongoose?

推荐答案

在MONGODB 3.4.x上

根据文档( http ://mongoosejs.com/docs/connections.html ),位于副本集连接"部分.

I resolved this issue putting the 'options' value directly in 'uri' string, according to documentation (http://mongoosejs.com/docs/connections.html) on 'Replica Set Connections' section.

// connection string using mongoose:
var uri = 'mongodb://MY_USER:MY_PASSWORD@' +
  'MY_SERVER-shard-00-00-clv3h.mongodb.net:27017,' +
  'MY_SERVER-shard-00-01-clv3h.mongodb.net:27017,' +
  'MY_SERVER-shard-00-02-clv3h.mongodb.net:27017/MY_DATABASE' +
  'ssl=true&replicaSet=MY_REPLICASET_NAME-shard-0&authSource=MY_ADMIN_DATABASE';

mongoose.connect(uri);
var db = mongoose.connection;

现在,它工作正常!

MONGODB 3.6的注意事项

在使用3.6.x版本的MongoDB Atlas上,连接字符串更改为使用DNS服务器,从而缩短了链接.

On MongoDB Atlas using the version 3.6.x, the connection string changed to use a DNS server making the link shorter.

mongodb + srv://MY_USER:MY_PASSWORD@MY_SERVER.mongodb.net/MY_DATABASE

mongodb+srv://MY_USER:MY_PASSWORD@MY_SERVER.mongodb.net/MY_DATABASE

...如果在应用程序中使用此连接字符串,将成功连接,但仅能对具有更高特权访问权限的Atlas用户(atlasAdmin,readWriteAnyDatabase ...)进行读写.

...if you use this connection string in your application, this will connect with success but it will be able to read and write only with atlas users with higher privilegies access (atlasAdmin, readWriteAnyDatabase...).

要与具有特权的特定用户一起工作以读取数据库,您将需要保留MongoDB 3.4中使用的相同连接字符串,因为猫鼬无法识别DNS选项(mongodb + srv).

To you work with an specific user with privilege only to readWrite your database, you will need to keep the same connection string used in MongoDB 3.4 because the mongoose not recognized the DNS option (mongodb+srv).

P.S.来自MongoDB 3.6.x的所有新资源将继续正常工作!

P.S. all the new resources from MongoDB 3.6.x will continue working normally!

这篇关于猫鼬与Atlas上的ReplicaSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆