MongoDB tip: issue with specifying localhost in replica set configuration


Often time when we want to quickly spin up a demo mongodb replica set, we would simply use the localhost:port to specify the nodes assuming all members are on the same server. Here is an quick example why that's not a good practice


Here is the output of my current rs.status()
MongoDB Enterprise biDemo:PRIMARY> conf= rs.conf()
{
        "_id" : "biDemo",
        "version" : 5,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:31111",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:31112",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:31113",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {
                                "use" : "thirdNode"
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : 2000,
                "getLastErrorModes" : {
                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("599ae2f61e8b3a4f44dd1370")
        }
}

When we attempt to connect to the replica set as such from a remote client,
C:\Users\jochen>mongo --host biDemo/10.155.228.75:31113
2017-08-21T12:42:24.134-0400 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.9
connecting to: biDemo/10.155.228.75:31113/test
2017-08-21T12:42:24.168-0400 I NETWORK  [thread1] Starting new replica set monitor for biDemo/10.155.228.75:31113
2017-08-21T12:42:24.169-0400 I NETWORK  [ReplicaSetMonitorWatcher] starting
2017-08-21T12:42:25.205-0400 W NETWORK  [thread1] Failed to connect to 127.0.0.1:31111, reason: errno:10061 No connection could be made because the target machine actively refused it.
2017-08-21T12:42:26.202-0400 W NETWORK  [thread1] Failed to connect to 127.0.0.1:31113, reason: errno:10061 No connection could be made because the target machine actively refused it.
2017-08-21T12:42:27.204-0400 W NETWORK  [thread1] Failed to connect to 127.0.0.1:31112, reason: errno:10061 No connection could be made because the target machine actively refused it.
2017-08-21T12:42:27.204-0400 W NETWORK  [thread1] No primary detected for set biDemo
2017-08-21T12:42:28.211-0400 W NETWORK  [thread1] Failed to connect to 127.0.0.1:31111, reason: errno:10061 No connection could be made because the target machine actively refused it.
2017-08-21T12:42:29.211-0400 W NETWORK  [thread1] Failed to connect to 127.0.0.1:31113, reason: errno:10061 No connection could be made because the target machine actively refused it.
2017-08-21T12:42:30.216-0400 W NETWORK  [thread1] Failed to connect to 127.0.0.1:31112, reason: errno:10061 No connection could be made because the target machine actively refused it.
2017-08-21T12:42:30.216-0400 W NETWORK  [thread1] No primary detected for set biDemo

This is due to the fact that our configuration specified localhost and our mongo shell connection doesn't know how to handle that

So we need to first log back into the replica set by specifying the primary node
C:\Users\jochen>mongo --host 10.155.228.75:31111
2017-08-21T12:44:51.907-0400 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.9
connecting to: 10.155.228.75:31111/test
Server has startup warnings:
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten]
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten]
Now, let's modify configuration to use the hostname or ip address instead.
MongoDB Enterprise biDemo:PRIMARY> conf = rs.conf()
{
        "_id" : "biDemo",
        "version" : 5,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:31111",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:31112",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:31113",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {
                                "use" : "thirdNode"
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : 2000,
                "getLastErrorModes" : {
                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("599ae2f61e8b3a4f44dd1370")
        }
}
MongoDB Enterprise biDemo:PRIMARY> conf.members[1].host ="10.155.228.75:31112"
10.155.228.75:31112
MongoDB Enterprise biDemo:PRIMARY> conf.members[2].host ="10.155.228.75:31113"
10.155.228.75:31113
MongoDB Enterprise biDemo:PRIMARY> conf.members[0].host ="10.155.228.75:31111"
10.155.228.75:31111
MongoDB Enterprise biDemo:PRIMARY> rs.reconfig(conf)
{ "ok" : 1 }

As we can see the new configuration is there now
MongoDB Enterprise biDemo:PRIMARY> conf = rs.conf()
{
        "_id" : "biDemo",
        "version" : 5,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:31111",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:31112",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:31113",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {
                                "use" : "thirdNode"
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : 2000,
                "getLastErrorModes" : {
                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("599ae2f61e8b3a4f44dd1370")
        }
}
MongoDB Enterprise biDemo:PRIMARY> conf.members[1].host ="10.155.228.75:31112"
10.155.228.75:31112
MongoDB Enterprise biDemo:PRIMARY> conf.members[2].host ="10.155.228.75:31113"
10.155.228.75:31113
MongoDB Enterprise biDemo:PRIMARY> conf.members[0].host ="10.155.228.75:31111"
10.155.228.75:31111
MongoDB Enterprise biDemo:PRIMARY> rs.reconfig(conf)
{ "ok" : 1 }

Now, let's reattempt the connection the way we did it previously
C:\Users\jochen>mongo --host biDemo/10.155.228.75:31113
2017-08-21T12:48:19.535-0400 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.9
connecting to: biDemo/10.155.228.75:31113/test
2017-08-21T12:48:19.570-0400 I NETWORK  [thread1] Starting new replica set monitor for biDemo/10.155.228.75:31113
2017-08-21T12:48:19.571-0400 I NETWORK  [ReplicaSetMonitorWatcher] starting
2017-08-21T12:48:19.590-0400 I NETWORK  [thread1] changing hosts to biDemo/10.155.228.75:31111,10.155.228.75:31112,10.155.228.75:31113 from biDemo/10.155.228.75:31113
Server has startup warnings:
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten]
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-08-21T09:29:30.815-0400 I CONTROL  [initandlisten]
MongoDB Enterprise biDemo:PRIMARY>

Success!





























Comments

Popular posts from this blog

MongoDB Ops Manager Basic Installation and Configuration

Oracle Goldengate Extract, Pump, and Replicat

Oracle Goldengate Extract and Replicat within same DB from one schema to another plus some issues and fixes