代码之家  ›  专栏  ›  技术社区  ›  svk

Mongodb与集群连接时节点获取错误?

  •  0
  • svk  · 技术社区  · 8 年前

    我从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. 要在URI或option对象中提供的复制集——我的URI中有复制集。不知道为什么会这样。

    知道我做错了什么吗?

    如果你想知道我在申请中是如何联系的,

    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不连接。所以我删除了参数并成功连接。

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

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

    希望它也能帮助别人。

    0 回复  |  直到 8 年前
    推荐文章