简体   繁体   English

Mongodb 和节点在连接集群时出错?

[英]Mongodb and node Getting error while connecting with cluster?

I am trying to connect with mongoldb cluster with my node application.我正在尝试使用我的节点应用程序连接 mongoldb 集群。 I am having mongodb version 3.1.0.我有 mongodb 版本 3.1.0。

I copied the connection string from mongodb which looks like below.我从 mongodb 复制了连接字符串,如下所示。

mongodb://username:<PASSWORD>@clusterstring,clusterstring1,clusterstring2/dbname?ssl=true&replicaSet=replicaSet&authSource=admin&retryWrites=true

But when I am trying to connect by using the above string I am getting the below error.但是,当我尝试使用上述字符串进行连接时,出现以下错误。

MongoError: seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI o r options object, mongodb://server:port/db?replicaSet=name

So the above message gives me two errors所以上面的消息给了我两个错误

  1. Seed list contains no mongos proxies -- Not sure why it is happening种子列表不包含 mongos 代理——不知道为什么会这样
  2. Replicaset to be supplied in the URI or option object -- I have replicaSet in my URI.要在 URI 或选项对象中提供的副本集——我的 URI 中有副本集。 Not sure why it is happening.不知道为什么会这样。

If I set ssl=false, the second message disappears and first one stays .如果我设置 ssl=false,则第二条消息消失而第一条消息保持不变。

Any idea what I am doing wrong?知道我做错了什么吗?

In case if you want to know how I am connecting in my application,如果您想知道我如何在我的应用程序中进行连接,

this is in config

module.exports = {
    secret: 'sectreKey',
    session: { session: false },
    database: aboveconnectionstring
  }



//the above values are passed here 
module.exports = (mongoose, config) => {
    const database = mongoose.connection;
    mongoose.Promise = Promise;
    mongoose.connect(config.database, {
      promiseLibrary: global.Promise,
       useNewUrlParser: true 
    });
    database.on('error', error => console.log(`Connection to sample  service database failed: ${error}`));
    database.on('connected', () => console.log('Connected to sample  Service database'));
    database.on('disconnected', () => console.log('Disconnected from sample  Service database'));
    process.on('SIGINT', () => {
      database.close(() => {
        console.log('sampleVueService terminated, connection closed');
        process.exit(0);
      })
    });
  };

EDIT:Resolved编辑:已解决

I found the answer from the below stack overflow question.我从下面的堆栈溢出问题中找到了答案。 Mongoose cluster connection with Mongo .But it doesn't connect with the parameter &retryWrites=true . Mongoose 集群与 Mongo 连接。但它不与参数 &retryWrites=true 连接。 So I removed the parameter and connected successfully.所以我去掉了参数,连接成功。

MongoError: The field 'retryWrites' is not valid for an index specification. MongoError:字段“retryWrites”对于索引规范无效。 Specification: { name: "username_1", key: { username: 1 }, unique: true, background: true, retryWrites: true }规范:{ name: "username_1", key: { username: 1 }, unique: true, background: true, retryWrites: true }

So my new database connect looks like below所以我的新数据库连接如下所示

module.exports = (mongoose, config) => {
    if(config.database.indexOf('replicaSet') > - 1) {
      dbOptions = {
        db: {native_parser: true},
        replset: {
          auto_reconnect:false,
          poolSize: 10,
          socketOptions: {
            keepAlive: 1000,
            connectTimeoutMS: 30000
          }
        },
        server: {
          poolSize: 5,
          socketOptions: {
            keepAlive: 1000,
            connectTimeoutMS: 30000
          }
        }
      };
    }

    const database = mongoose.connection;
    mongoose.Promise = Promise;
    mongoose.connect(config.database, dbOptions);
    database.on('error', error => console.log(`Connection to sample  service database failed: ${error}`));
    database.on('connected', () => console.log('Connected to sample  Service database'));
    database.on('disconnected', () => console.log('Disconnected from sample  Service database'));
    process.on('SIGINT', () => {
      database.close(() => {
        console.log('sampleVueService terminated, connection closed');
        process.exit(0);
      })
    });
  };

Hope it helps others too.希望它也能帮助其他人。

错误可能是由于mongoose和mongodb的版本,尝试运行npm updatenpx update ,更新包解决了错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM