MongoDB设置了连接池,当我未配置readPreference参数,重启MongoDB时调用接口查询会报错, 当我配置了"readPreference": "secondaryPreferred"就不会报错。 这是为什么?
一.这是不会报错的代码, 假如注释readPreference,重启时调接口就会报错。
mongo: {
mongourl: mongourl || "mongodb://127.0.0.1:37017/dgfhk",
options: {
"poolSize": 120,
"auto_reconnect": true,
"db": {
"readPreference": "secondaryPreferred" // 先从后主, 优先从secondary读取,如果secondary不可用时就从primary读。
},
}
},
调查询接口会报错
二.第一次报错,
list error MongoError: not master and slaveOk=false
at Connection.<anonymous> (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/connection/pool.js:453:61)
at Connection.emit (events.js:400:28)
at processMessage (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/connection/connection.js:456:10)
at Socket.<anonymous> (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/connection/connection.js:625:15)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:290:12)
第二次报错
list error MongoError: pool destroyed
at Pool.write (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/connection/pool.js:844:8)
at _command (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/wireprotocol/command.js:149:10)
at command (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/wireprotocol/command.js:28:5)
at Object.query (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/wireprotocol/query.js:70:3)
at Server.query (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/topologies/server.js:649:16)
at FindOperation.execute (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/operations/find.js:38:12)
at /Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/operations/execute_operation.js:148:15
at Server.selectServer (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/core/topologies/server.js:837:3)
at Server.selectServer (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/topologies/topology_base.js:342:32)
at executeWithServerSelection (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/operations/execute_operation.js:131:12)
at /Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/operations/execute_operation.js:70:9
at maybePromise (/Users/liwenhao/Desktop/imagecreate/node_modules/mongodb/lib/utils.js:692:3)
at executeOperation (/Users/liwenha