簡體   English   中英

Mongodb 和節點在連接集群時出錯?

[英]Mongodb and node Getting error while connecting with 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 中有副本集。 不知道為什么會這樣。

如果我設置 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);
      })
    });
  };

希望它也能幫助其他人。

錯誤可能是由於mongoose和mongodb的版本,嘗試運行npm updatenpx update ,更新包解決了錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM