0

我正在尝试使用我的节点应用程序连接 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 集群与 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);
      })
    });
  };

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

4

1 回答 1

-1

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

于 2021-01-17T15:35:20.477 回答