简体   繁体   English

当选择新的主要数据库时,Express无法连接到mongodb副本集

[英]Express can't connect to mongodb replica set when new primary get elected

I have a mongodb replica set with the following config: 我有一个具有以下配置的mongodb replica set

  • M1 primary M1小学
  • M2 secondary M2中学
  • M3 secondary M3中学
  • M4 arbitrator M4仲裁员

Here is my Express code to connect to db (I use mongoose as the ODM): 这是我连接到数据库的Express代码(我使用mongoose作为ODM):

const config = {
  db: `mongodb://${process.env.DB_ADDRESS_1}/${process.env.DB_NAME},
                 ${process.env.DB_ADDRESS_2}/${process.env.DB_NAME},
                 ${process.env.DB_ADDRESS_3}/${process.env.DB_NAME}`,
  dbOptions: {
    server: {
      socketOptions: {
        keepAlive: 1
      },
      poolSize: 5,
      readPreference: 'nearest'
    },
    replSet: {
      rs_name: process.env.REPLICA_SET,
      poolSize: 5,
      readPreference: 'nearest',
      socketOptions: {
        keepAlive: 1,
        connectTimeoutMS: 30000,
        socketTimeoutMS: 0
      }
    },
    db: {
      w: 1,
      numberOfRetries: 2
    }
  }
}
mongoose.connect(config.db, config.dbOptions, (error) => {
  if (error) {
    console.log('Error on connecting to th db: ', error)
    console.log('\x1b[31m', '*** PLEASE CONNECT TO DATABASE BEFORE RUN SERVER', '\x1b[0m')
    process.exit(1)
  }
  callback()
})

The app works as expected. 该应用程序可以正常运行。 When M1 get down, either M2 or M3 get elected to be the primary , BUT my express app still CAN'T connect to the replica set . 当M1掉线时,无论M2还是M3被选primary ,但我的express应用仍然无法连接到replica set

Is there any wrong in my config? 我的配置有问题吗?

Connection URL should be: 连接网址应为:

`mongodb://${process.env.DB_ADDRESS_1},${process.env.DB_ADDRESS_2},${process.env.DB_ADDRESS_3}/${process.env.DB_NAME}`

So, there is a list of nodes and after that comes information about the database. 因此,有一个节点列表,然后是有关数据库的信息。

And your setup has one flaw too... When you have three node replica set, you SHOULD NOT have arbitrator as forth! 而且您的设置也有一个缺陷...当您设置了三个节点副本时,您不应设置仲裁者! This because, vote count must be odd not even . 这是因为,投票数必须不是奇数

Your MongoDB URI connection string is not following the proper format. 您的MongoDB URI连接字符串未遵循正确的格式。 The database name should be appended after the server list, and not appended on every server. 数据库名称应附加在服务器列表之后,而不是附加在每台服务器上。 Try: 尝试:

mongodb://${process.env.DB_ADDRESS_1},
${process.env.DB_ADDRESS_2},
${process.env.DB_ADDRESS_3}
/${process.env.DB_NAME}?replicaSet=${process.env.REPLICA_SET}

See https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options for an example. 有关示例,请参见https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options

On another note, you don't need an arbiter on the replica set. 另外,副本集上不需要仲裁器。 It's not recommended to have an even number of nodes in a replica set, since it requires a majority to elect a primary. 不建议在副本集中有偶数个节点,因为它需要多数才能选举一个主节点。

With 4 nodes, if 2 of the nodes are down, the remaining nodes are not able to elect a primary (which is also the case when you have a 3-node replica set). 对于4个节点,如果其中2个节点关闭,则其余节点将不能选择主节点(当您具有3节点副本集时也是如此)。 Hence, the additional arbiter doesn't add any value to the replica set. 因此,附加仲裁程序不会向副本集添加任何值。 See https://docs.mongodb.com/manual/core/replica-set-architectures/#consider-fault-tolerance for more information. 有关更多信息,请参见https://docs.mongodb.com/manual/core/replica-set-architectures/#consider-fault-tolerance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 本地mongo停止时,Express无法连接到mongodb副本集 - Express can't connect to mongodb replica set when local mongo is stoped MongoDB:无法连接到新的副本集主 - MongoDB: Can't connect to new replica set master 副本集故障转移后,无法从Mongoose(MongoDB,NodeJS驱动程序)自动重新连接到New PRIMARY - Failing to automatically re-connect to New PRIMARY after a replica set failover , from Mongoose (MongoDB, NodeJS Driver) 错误:无法连接到新的副本集主服务器 - Error: can't connect to new replica set master 无法通过NodeJS连接到MongoDB-在副本集中找不到主数据库错误 - Cannot connect to MongoDB via NodeJS - No primary found in replica set error Mongo副本集找不到主副本 - Mongo replica set can't find primary MongoDB NodeJS 客户端与副本集的连接在主节点宕机时不会重新连接 - MongoDB NodeJS client connection to Replica Set won't reconnect when the primary goes down Express 无法启动服务器或连接到 MongoDB - Express can't start the server or connect to MongoDB 当主节点关闭时,Node.js本机mongodb失去与副本集的连接 - Node.js native mongodb losing connection with replica set when primary goes down 无法获取Atlassian Connect Express进行安装 - Can't get Atlassian Connect Express to Install
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM