MongoError: no primary found in replicaset
nodejs连接mongodb时,使用集群方式报错
2017-09-22T01:42:32.115Z - error: db connect failed 2017-09-22T01:42:32.124Z - error: MongoError: no primary found in replicaset at /opt/b2b/jiathis-service/node_modules/mongodb-core/lib/topologies/replset.js:560:28 at null.<anonymous> (/opt/xxxx-service/node_modules/mongodb-core/lib/topologies/replset.js:312:24) at g (events.js:260:16) at emitOne (events.js:77:13) at emit (events.js:169:7) at /opt/b2b/jiathis-service/node_modules/mongodb-core/lib/topologies/server.js:300:14 at /opt/b2b/jiathis-service/node_modules/mongodb-core/lib/connection/pool.js:469:18 at nextTickCallbackWith0Args (node.js:420:9) at process._tickCallback (node.js:349:13)
解决方式
一、使用非集群方式连接mongodb,去掉replicatSet参数
db: 'mongodb://username:password@IP/db_name'
OR
二、在mongodb中,配置集群 ,去掉replSet前面的#号,并设置你的集群名字 /etc/mongod.conf
# in replicated mongo databases, specify the replica set name here
#replSet=setname
添加实例到replica set
rs.initiate() rs.add("IP:27017") #查看配置 rs.conf()
记录点滴,沉淀自己,汇聚成海,重新再出发