MongoDB exception: can’t use localhost in repl set member names except when using it for all members

Error: When use rs.Add():

{
“errmsg” : “exception: can’t use localhost in repl set member names except when using it for all members”,
“code” : 13393,
“ok” : 0
}

Cause:

1. Your replica set is using localhost for it members and you are adding a new member with IP/hostname other than localhost

2. Your replica set is using IP/hostname for it members and you are adding a new member with localhost as hostname

Solution: Only use localhost for replica set members if you are sure that all mongod/mongos instance are on the same local machine. Otherwise, use hostname or IP.

MongoDB exception: can’t use localhost in repl set member names except when using it for all members

MongoDB couldn’t connect to new shard socket exception [CONNECT_ERROR]

Error: addShard() command returns error:

{
“ok” : 0,
“errmsg” : “couldn’t connect to new shard socket exception [CONNECT_ERROR] for replname/hostname:27017″
}

Cause:

1. hostname:27017 cannot be reached

2. if hostname:27017 can be reached, then your replica set [replname] configuration contains members with ‘localhost‘ as host name

Solution: Unless you  are running all mongod/mongos instances on the same machine, then you can use ‘localhost’ in rs.Add() command. Otherwise, use hostname instead of localhost in all rs.Add(), sh.addShard() command. Note that when adding a host itself to replcaset/shard an usable hostname should be present in /etc/hosts, or you will get this error:

{ “errmsg” : “couldn’t initiate : can’t find self in the replset config”, “ok” : 0 }

MongoDB couldn’t connect to new shard socket exception [CONNECT_ERROR]

MongoDB: replset couldn’t find a slave with id 1, not tracking

Error: replset couldn’t find a slave with id 1, not tracking

Causes: After removing and adding replica to MongoDB replica set using rs.add() and rs.remove(), the replica set member id’s don’t follow a continuous order from _id: 0, for example:

{
“_id” : “rs0″,
“version” : 1,
“members” : [
{
“_id” : 4,
“host” : “mongodb0.example.net:27017″
},
{
“_id” : 5,
“host” : “mongodb1.example.net:27017″
},
{
“_id” : 7,
“host” : “mongodb2.example.net:27017″
}
]
}

Solution: You can use rs.reconfig() to re-etablish the order:

var cfg = {
“_id” : “rs0″,
“version” : 1,
“members” : [
{
“_id” : 0,
“host” : “mongodb0.example.net:27017″
},
{
“_id” : 1,
“host” : “mongodb1.example.net:27017″
},
{
“_id” : 2,
“host” : “mongodb2.example.net:27017″
}
]
}
rs.reconfig(cfg);

MongoDB: replset couldn’t find a slave with id 1, not tracking

MongoDB on Windows: getnameinfo errno 10106

Error: When starting the MongoDB service in the prompt (c:mongodbbinmongod.exe), I get the following error:

exception in initAndListen:13082 getnameinfo error errno:10106
The requested service provider could not be loaded or initialized.. terminating

Cause: This is a WinSock error as in the mongod.log

WSAEPROVIDERFAILEDINIT [10106]: Service provider failed to initialize. This error is returned if either a service provider’s DLL could not be loaded (LoadLibrary failed) or the provider’s WSPStartup or NSPStartup function failed.

Solution: open cmd as admin, type the following and hit Enter to repair winsock.

netsh winsock reset

MongoDB on Windows: getnameinfo errno 10106

MongoDB Exception: aggregation result exceeds maximum document size (16MB)

MongoDB aggregation result exceeds maximum document size (16MB)

{
“errmsg” : “exception: aggregation result exceeds maximum document size (16MB)”,
“code” : 16389,
“ok” : 0
}

Cause:

  • You are using MongoDB version <=2.4
  • You are using MongoDB version >=2.6 but are using a mongo shell 2.4 or a PHP/C/Java/Ruby driver to run aggregate() without specifying the output as “cursor”

Solution for 2.4: Limit the output using $skip and $limit to fit the 16MB limit

Solution for 2.6:

Java driver: Use option:

.newAggregationOptions().outputMode(
AggregationOptions.OutputMode.CURSOR).build()

PHP driver:

public MongoCommandCursor MongoCollection::aggregateCursor ( array $command [, array $options ] )

MongoDB Exception: aggregation result exceeds maximum document size (16MB)

Unclean shutdown detected when using mongodb docker

When stopping a docker with mongod daemon running using command:

docker stop

the next time you start the mongod docker, it will complain about unclean shutdown detected:

“Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery
instructions.”

Cause: “docker stop” use SIGTERM to kill mongod daemon, while it is recommended to use SIGINT

Solution:

docker kill -s INT

Unclean shutdown detected when using mongodb docker