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

时间:2018-08-26 18:59:04

标签: node.js mongodb mongodb-cluster

我正在尝试通过我的节点应用程序连接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中有copyingSet。不知道为什么会这样。

如果我设置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”字段对于索引规范无效。规范:{名称:“ username_1”,密钥:{用户名:1​​},唯一:true,背景: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);
      })
    });
  };

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

1 个答案:

答案 0 :(得分:-1)

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