简体   繁体   中英

Mongodb and node Getting error while connecting with cluster?

I am trying to connect with mongoldb cluster with my node application. I am having mongodb version 3.1.0.

I copied the connection string from mongodb which looks like below.

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
  2. Replicaset to be supplied in the URI or option object -- I have replicaSet in my URI. Not sure why it is happening.

If I set ssl=false, the second message disappears and first one stays .

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 . So I removed the parameter and connected successfully.

MongoError: The field 'retryWrites' is not valid for an index specification. Specification: { 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 ,更新包解决了错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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