Mongoose: [v4.11.13] MongoError: no primary found in replicaset

Created on 30 Sep 2017  路  3Comments  路  Source: Automattic/mongoose

The latest version of mongoose(v4.11.13).

Do you want to request a feature or report a bug?

bug

What is the current behavior?

{ MongoError: no primary found in replicaset
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:560:28
wehpu-engine_1    |     at connectNewServers (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:317:25)
wehpu-engine_1    |     at topologyMonitor (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:552:12)
wehpu-engine_1    |     at Server.<anonymous> (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:772:7)
wehpu-engine_1    |     at Server.g (events.js:292:16)
wehpu-engine_1    |     at emitOne (events.js:96:13)
wehpu-engine_1    |     at Server.emit (events.js:188:7)
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/server.js:300:14
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/connection/pool.js:469:18
wehpu-engine_1    |     at _combinedTickCallback (internal/process/next_tick.js:73:7)
wehpu-engine_1    |     at process._tickCallback (internal/process/next_tick.js:104:9)
wehpu-engine_1    |   name: 'MongoError',
wehpu-engine_1    |   message: 'no primary found in replicaset' }
wehpu-engine_1    | Unhandled rejection MongoError: no primary found in replicaset
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:560:28
wehpu-engine_1    |     at connectNewServers (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:317:25)
wehpu-engine_1    |     at topologyMonitor (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:552:12)
wehpu-engine_1    |     at Server.<anonymous> (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:772:7)
wehpu-engine_1    |     at Server.g (events.js:292:16)
wehpu-engine_1    |     at emitOne (events.js:96:13)
wehpu-engine_1    |     at Server.emit (events.js:188:7)
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/server.js:300:14
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/connection/pool.js:469:18
wehpu-engine_1    |     at _combinedTickCallback (internal/process/next_tick.js:73:7)
wehpu-engine_1    |     at process._tickCallback (internal/process/next_tick.js:104:9)

If the current behavior is a bug, please provide the steps to reproduce.

Connect:

mongoose.connect('mongodb://mongodb-master:27017,mongodb-slave1:27017,mongodb-slave2:27017/wehpu?replicaSet=wehpu', { useMongoClient: true });

I use the docker-compose to build. Everything is OK!
We can see the PRIMARY and SECONDARY

MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
wehpu:PRIMARY>
...
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
wehpu:SECONDARY>

When I attach to the MongoDB(PRIMARY) and print rs.conf(), I can see:

wehpu:PRIMARY> rs.config()
{
    "_id" : "wehpu",
    "version" : 1,
    "protocolVersion" : NumberLong(1),
    "members" : [
        {
            "_id" : 0,
            "host" : "mongodb-master:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "mongodb-slave1:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "mongodb-slave2:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatIntervalMillis" : 2000,
        "heartbeatTimeoutSecs" : 10,
        "electionTimeoutMillis" : 10000,
        "catchUpTimeoutMillis" : 60000,
        "getLastErrorModes" : {

        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        },
        "replicaSetId" : ObjectId("59cee31221dd0f0e4f1f566b")
    }
}

IPADDR resolution works:

root@c0cdbbcaca42:/usr/src/app# ping mongodb-master
PING mongodb-master (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.099 ms
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.114 ms
......
root@c0cdbbcaca42:/usr/src/app# ping mongodb-slave1
PING mongodb-slave1 (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: icmp_seq=0 ttl=64 time=0.167 ms
64 bytes from 172.18.0.4: icmp_seq=1 ttl=64 time=0.127 ms

Here is a detailed output of my console:

Successfully built 80057069e381
Successfully tagged wehpuengine_wehpu-engine:latest
Creating mongodb-master ... 
Creating mongodb-slave2 ... 
Creating mongodb-slave1 ... 
Creating mongodb-master
Creating mongodb-slave2
Creating mongodb-slave1 ... done
Creating wehpuengine_wehpu-engine_1 ... 
Creating wehpuengine_wehpu-engine_1 ... done
Attaching to mongodb-master, mongodb-slave2, mongodb-slave1, wehpuengine_wehpu-engine_1
mongodb-master    | + MONGO_LOG=/var/log/mongodb/mongod.log
mongodb-master    | + MONGO=/usr/bin/mongo
mongodb-master    | + MONGOD=/usr/bin/mongod
mongodb-master    | + /usr/bin/mongod --fork --replSet wehpu --noprealloc --smallfiles --logpath /var/log/mongodb/mongod.log
mongodb-master    | note: noprealloc may hurt performance in many applications
mongodb-master    | about to fork child process, waiting until server is ready for connections.
mongodb-master    | forked process: 8
mongodb-slave2    | + MONGO_LOG=/var/log/mongodb/mongod.log
mongodb-slave2    | + MONGO=/usr/bin/mongo
mongodb-slave2    | + MONGOD=/usr/bin/mongod
mongodb-slave2    | + /usr/bin/mongod --fork --replSet wehpu --noprealloc --smallfiles --logpath /var/log/mongodb/mongod.log
mongodb-master    | child process started successfully, parent exiting
mongodb-master    | + '[' mongodb-master == mongodb-master ']'
mongodb-master    | + /usr/bin/mongo --eval 'rs.initiate({
mongodb-master    |     _id: "wehpu",
mongodb-master    |     version: 1,
mongodb-master    |     members: [
mongodb-master    |         {
mongodb-master    |             _id: 0,
mongodb-master    |             host: "mongodb-master:27017",
mongodb-master    |             arbiterOnly: false,
mongodb-master    |             buildIndexes: true,
mongodb-master    |             hidden: false,
mongodb-master    |             priority: 1,
mongodb-master    |             tags: {},
mongodb-master    |             slaveDelay: 0,
mongodb-master    |             votes: 1
mongodb-master    |         },
mongodb-master    |         {
mongodb-master    |             _id: 1,
mongodb-master    |             host: "mongodb-slave1:27017",
mongodb-master    |             arbiterOnly: false,
mongodb-master    |             buildIndexes: true,
mongodb-master    |             hidden: false,
mongodb-master    |             priority: 1,
mongodb-master    |             tags: {},
mongodb-master    |             slaveDelay: 0,
mongodb-master    |             votes: 1
mongodb-master    |         },
mongodb-master    |         {
mongodb-master    |             _id: 2,
mongodb-master    |             host: "mongodb-slave2:27017",
mongodb-master    |             arbiterOnly: false,
mongodb-master    |             buildIndexes: true,
mongodb-master    |             hidden: false,
mongodb-master    |             priority: 1,
mongodb-master    |             tags: {},
mongodb-master    |             slaveDelay: 0,
mongodb-master    |             votes: 1
mongodb-master    |         }
mongodb-master    |     ]
mongodb-master    | })'
mongodb-slave2    | note: noprealloc may hurt performance in many applications
mongodb-slave2    | about to fork child process, waiting until server is ready for connections.
mongodb-slave2    | forked process: 9
mongodb-slave2    | child process started successfully, parent exiting
mongodb-slave2    | + '[' '' == mongodb-master ']'
mongodb-slave2    | + tailf /dev/null
mongodb-master    | MongoDB shell version v3.4.9
mongodb-master    | connecting to: mongodb://127.0.0.1:27017
mongodb-master    | MongoDB server version: 3.4.9
mongodb-master    | {
mongodb-master    |     "info" : "try querying local.system.replset to see current configuration",
mongodb-master    |     "ok" : 0,
mongodb-master    |     "errmsg" : "already initialized",
mongodb-master    |     "code" : 23,
mongodb-master    |     "codeName" : "AlreadyInitialized"
mongodb-master    | }
mongodb-slave1    | + MONGO_LOG=/var/log/mongodb/mongod.log
mongodb-master    | + tailf /dev/null
mongodb-slave1    | + MONGO=/usr/bin/mongo
wehpu-engine_1    | npm info it worked if it ends with ok
mongodb-slave1    | + MONGOD=/usr/bin/mongod
wehpu-engine_1    | npm info using [email protected]
mongodb-slave1    | + /usr/bin/mongod --fork --replSet wehpu --noprealloc --smallfiles --logpath /var/log/mongodb/mongod.log
wehpu-engine_1    | npm info using [email protected]
mongodb-slave1    | note: noprealloc may hurt performance in many applications
mongodb-slave1    | about to fork child process, waiting until server is ready for connections.
mongodb-slave1    | forked process: 8
mongodb-slave1    | child process started successfully, parent exiting
mongodb-slave1    | + '[' '' == mongodb-master ']'
mongodb-slave1    | + tailf /dev/null
wehpu-engine_1    | npm info lifecycle [email protected]~prestart: [email protected]
wehpu-engine_1    | npm info lifecycle [email protected]~start: [email protected]
wehpu-engine_1    | 
wehpu-engine_1    | > [email protected] start /usr/src/app
wehpu-engine_1    | > node ./bin/www
wehpu-engine_1    | 
wehpu-engine_1    | Mongodb connection error:  { MongoError: no primary found in replicaset
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:560:28
wehpu-engine_1    |     at connectNewServers (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:317:25)
wehpu-engine_1    |     at topologyMonitor (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:552:12)
wehpu-engine_1    |     at Server.<anonymous> (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:772:7)
wehpu-engine_1    |     at Server.g (events.js:292:16)
wehpu-engine_1    |     at emitOne (events.js:96:13)
wehpu-engine_1    |     at Server.emit (events.js:188:7)
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/server.js:300:14
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/connection/pool.js:469:18
wehpu-engine_1    |     at _combinedTickCallback (internal/process/next_tick.js:73:7)
wehpu-engine_1    |     at process._tickCallback (internal/process/next_tick.js:104:9)
wehpu-engine_1    |   name: 'MongoError',
wehpu-engine_1    |   message: 'no primary found in replicaset' }
wehpu-engine_1    | Unhandled rejection MongoError: no primary found in replicaset
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:560:28
wehpu-engine_1    |     at connectNewServers (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:317:25)
wehpu-engine_1    |     at topologyMonitor (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:552:12)
wehpu-engine_1    |     at Server.<anonymous> (/usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/replset.js:772:7)
wehpu-engine_1    |     at Server.g (events.js:292:16)
wehpu-engine_1    |     at emitOne (events.js:96:13)
wehpu-engine_1    |     at Server.emit (events.js:188:7)
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/topologies/server.js:300:14
wehpu-engine_1    |     at /usr/src/app/node_modules/[email protected]@mongodb-core/lib/connection/pool.js:469:18
wehpu-engine_1    |     at _combinedTickCallback (internal/process/next_tick.js:73:7)
wehpu-engine_1    |     at process._tickCallback (internal/process/next_tick.js:104:9)

What is the expected behavior?

I just want to connect to the replica set, but, i got the issue...

Please mention your node.js, mongoose and MongoDB version.

node.js: node:boron v6.10.3
MongoDB: mongo:latest v3.4
mongoose: v4.11.13

help

Most helpful comment

I'd imagine its just that the replica set hasn't decided who the primary is when you're trying to connect. Try adding a sleep for 5 seconds before starting your app. You can also add connectWithNoPrimary to make it so connection succeeds even if it can't find a primary:

mongoose.connect('mongodb://mongodb-master:27017,mongodb-slave1:27017,mongodb-slave2:27017/wehpu?replicaSet=wehpu', { useMongoClient: true, connectWithNoPrimary: true });

All 3 comments

I'd imagine its just that the replica set hasn't decided who the primary is when you're trying to connect. Try adding a sleep for 5 seconds before starting your app. You can also add connectWithNoPrimary to make it so connection succeeds even if it can't find a primary:

mongoose.connect('mongodb://mongodb-master:27017,mongodb-slave1:27017,mongodb-slave2:27017/wehpu?replicaSet=wehpu', { useMongoClient: true, connectWithNoPrimary: true });

Awesome! You are right. It works for me. Thank you very much.

It was really helpful, it solved my error too, i had the same error.

Was this page helpful?
0 / 5 - 0 ratings