【问题标题】:Mongodb and node Getting error while connecting with cluster?Mongodb和节点在连接集群时出错?
【发布时间】:2019-02-01 09:44:47
【问题描述】:

我正在尝试使用我的节点应用程序连接 mongoldb 集群。我有 mongodb 3.1.0 版。

我从 mongodb 复制了如下所示的连接字符串。

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

但是当我尝试使用上面的字符串进行连接时,出现以下错误。

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

所以上面的消息给了我两个错误

  1. 种子列表不包含 mongos 代理 -- 不知道为什么会这样
  2. 要在 URI 或选项对象中提供的副本集 -- 我的 URI 中有副本集。不知道为什么会这样。

如果我设置 ssl=false,第二条消息会消失,而第一条消息会保留。

知道我做错了什么吗?

如果您想知道我在应用程序中的连接方式,

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

编辑:已解决

我从下面的堆栈溢出问题中找到了答案。Mongoose cluster connection with Mongo。但它与参数 &retryWrites=true 没有联系。于是我把参数去掉,连接成功。

MongoError:“retryWrites”字段对于索引规范无效。规范:{ name: "username_1", key: { username: 1 }, unique: true, background: true, retryWrites: true }

所以我的新数据库连接如下所示

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

希望它也对其他人有所帮助。

【问题讨论】:

  • 您是否在 URI 中指定了正确的副本集名称作为副本集参数? monogd 实例的结构是这样完成的吗:mongodb://&lt;user&gt;:&lt;password&gt;@&lt;IP&gt;:&lt;PORT&gt;,&lt;IP&gt;:&lt;PORT&gt;,&lt;IP&gt;:&lt;PORT&gt;/dbName?replicaSet=&lt;NAME_OF_REPLICA_SET&gt;(...)
  • 是的,我刚刚从MongoDB集群门户复制并使用它。

标签: node.js mongodb mongodb-cluster


【解决方案1】:

报错可能是mongoose和mongodb的版本问题, 尝试运行 npm updatenpx update,, 更新软件包解决了错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2019-11-28
    • 2020-10-04
    • 1970-01-01
    • 2018-10-29
    • 2016-07-11
    相关资源
    最近更新 更多