西木-Lee

 

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()

 

posted on 2017-09-24 12:46  西木-Lee  阅读(2889)  评论(0编辑  收藏  举报

导航