Mongoose with ReplicaSet on Atlas

纵然是瞬间 提交于 2019-12-09 12:01:20

问题


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

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;

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.

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);

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

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


回答1:


ON MONGODB 3.4.x

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;

Now, it is working fine!

NOTICE WITH MONGODB 3.6

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

...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...).

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. all the new resources from MongoDB 3.6.x will continue working normally!




回答2:


Add username and password to database connection

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

Standard Connection String Format



来源:https://stackoverflow.com/questions/41213148/mongoose-with-replicaset-on-atlas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!