Wednesday, August 26, 2020

MongoDB Replica set port number change

How to change the port number of MongoDB replica set members 


Introduction 

 Step 1 : Update the Port number in Mongodb.conf file in Primary, Slave1,Slave2 and Arbiter nodes.

New Port number  

Primary Server Port number: 30000

Slave1 Port number: 20002

Slave2 Port number: 20003

Arbiter Port number: 20004


Step 2 : Add the slave members in cluster with new port numbers 

Step 3 : Initiate the replica set from the primary node.

Step 4 : Check the status

Step 5: Reconfigure the primary server 

Step 6: Check the status of replication configuration

---------------------------------


Step 1 : - 

Primary:-

[mdashok@MongoDB mongodb]$ pwd

/usr/server1/mongodb

[mdashok@MongoDB mongodb]$ cat mongodb.conf

systemLog:

   destination: file

   path: "/usr/server1/mongodb/log/mongodb.log"

   logAppend: true

processManagement:

   fork: true

net:

   bindIp: 127.0.0.1,192.168.1.115

   port: 30000

storage:

   dbPath: "/usr/server1/mongodb/data"

replication:

   replSetName: "OurReplica"

[mdashok@MongoDB mongodb]$


Slave 1 :

[mdashok@MongoDB mongodb]$ pwd

/usr/server2/mongodb

[mdashok@MongoDB mongodb]$ cat mongodb.conf

systemLog:

   destination: file

   path: "/usr/server2/mongodb/log/mongodb.log"

   logAppend: true

processManagement:

   fork: true

net:

   bindIp: 127.0.0.1

   port: 20002

storage:

   dbPath: "/usr/server2/mongodb/data"

replication:

   replSetName: "OurReplica"

[mdashok@MongoDB mongodb]$


Slave 2 

[mdashok@MongoDB mongodb]$ pwd

/usr/server3/mongodb

[mdashok@MongoDB mongodb]$ cat mongodb.conf  | grep port*

   port: 20003

[mdashok@MongoDB mongodb]$


Arbiter :

[mdashok@MongoDB mongodb]$ pwd
/usr/server4/mongodb
[mdashok@MongoDB mongodb]$ cat mongodb.conf
systemLog:
   destination: file
   path: "/usr/server4/mongodb/log/mongodb.log"
   logAppend: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 20004
storage:
   dbPath: "/usr/server4/mongodb/data"
replication:
   replSetName: "OurReplica"
[mdashok@MongoDB mongodb]$


STEP 2:- Add the slave members nodes in primary replica set:


MongoDB Enterprise OurReplica:PRIMARY> rs.add("127.0.0.1:20002");

MongoDB Enterprise OurReplica:PRIMARY> rs.add("127.0.0.1:20003");

Arbiter node port number add:

MongoDB Enterprise OurReplica:PRIMARY> rs.add("127.0.0.1:20004");


STEP 3:

Initiate the Replication 

MongoDB Enterprise OurReplica:OTHER> rs.initiate();

{

        "operationTime" : Timestamp(1538391218, 1),

        "ok" : 0,

        "errmsg" : "already initialized",

        "code" : 23,

        "codeName" : "AlreadyInitialized",

        "$clusterTime" : {

                "clusterTime" : Timestamp(1538391218, 1),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        }

}

 Since the Replication is using the old port number, we have to reconfigure the settings 

STEP 4:

Let us check the status of replica:

MongoDB Enterprise OurReplica:OTHER> rs.status();

{

        "operationTime" : Timestamp(1538391218, 1),

        "ok" : 0,

        "errmsg" : "Our replica set config is invalid or we are not a member of it",

        "code" : 93,

        "codeName" : "InvalidReplicaSetConfig",

        "$clusterTime" : {

                "clusterTime" : Timestamp(1538391218, 1),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        }

          The Error message says "Our replica set configure is invalid or we are not a member of it.

      

      STEP 5: Let us re-configure the replication configuration .


a.     MongoDB Enterprise OurReplica:OTHER> rsconf = rs.conf()

b.     MongoDB Enterprise OurReplica:OTHER> rsconf.members = [{_id: 0, host: "localhost:30000"}]

 [ { "_id" : 0, "host" : "localhost:30000" } ]  


c.     MongoDB Enterprise OurReplica:OTHER> rs.reconfig(rsconf, {force: true})

{

        "ok" : 1,

        "operationTime" : Timestamp(1538391218, 1),

        "$clusterTime" : {

                "clusterTime" : Timestamp(1538391218, 1),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        }

}

d.     Let us check status of Replication :

MongoDB Enterprise OurReplica:PRIMARY> rs.status();

{

        "set" : "OurReplica",

        "date" : ISODate("2020-08-25T22:56:15.651Z"),

        "myState" : 1,

        "term" : NumberLong(17),

        "syncingTo" : "",

        "syncSourceHost" : "",

        "syncSourceId" : -1,

        "heartbeatIntervalMillis" : NumberLong(2000),

        "optimes" : {

                "lastCommittedOpTime" : {

                        "ts" : Timestamp(1598396172, 1),

                        "t" : NumberLong(17)

                },

                "readConcernMajorityOpTime" : {

                        "ts" : Timestamp(1598396172, 1),

                        "t" : NumberLong(17)

                },

                "appliedOpTime" : {

                        "ts" : Timestamp(1598396172, 1),

                        "t" : NumberLong(17)

                },

                "durableOpTime" : {

                        "ts" : Timestamp(1598396172, 1),

                        "t" : NumberLong(17)

                }

        },

        "lastStableCheckpointTimestamp" : Timestamp(1598396152, 1),

        "members" : [

                {

                        "_id" : 0,

                        "name" : "localhost:30000",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 1698,

                        "optime" : {

                                "ts" : Timestamp(1598396172, 1),

                                "t" : NumberLong(17)

                        },

                        "optimeDate" : ISODate("2020-08-25T22:56:12Z"),

                        "syncingTo" : "",

                        "syncSourceHost" : "",

                        "syncSourceId" : -1,

                        "infoMessage" : "",

                        "electionTime" : Timestamp(1598395670, 1),

                        "electionDate" : ISODate("2020-08-25T22:47:50Z"),

                        "configVersion" : 16218,

                        "self" : true,

                        "lastHeartbeatMessage" : ""

                },

                {

                        "_id" : 1,

                        "name" : "127.0.0.1:20002",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 50,

                        "optime" : {

                                "ts" : Timestamp(1598396172, 1),

                                "t" : NumberLong(17)

                        },

                        "optimeDurable" : {

                                "ts" : Timestamp(1598396172, 1),

                                "t" : NumberLong(17)

                        },

                        "optimeDate" : ISODate("2020-08-25T22:56:12Z"),

                        "optimeDurableDate" : ISODate("2020-08-25T22:56:12Z"),

                        "lastHeartbeat" : ISODate("2020-08-25T22:56:15.615Z"),

                        "lastHeartbeatRecv" : ISODate("2020-08-25T22:56:15.640Z"),

                        "pingMs" : NumberLong(0),

                        "lastHeartbeatMessage" : "",

                        "syncingTo" : "localhost:30000",

                        "syncSourceHost" : "localhost:30000",

                        "syncSourceId" : 0,

                        "infoMessage" : "",

                        "configVersion" : 16218

                },

                {

                        "_id" : 2,

                        "name" : "127.0.0.1:20003",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 42,

                        "optime" : {

                                "ts" : Timestamp(1598396172, 1),

                                "t" : NumberLong(17)

                        },

                        "optimeDurable" : {

                                "ts" : Timestamp(1598396172, 1),

                                "t" : NumberLong(17)

                        },

                        "optimeDate" : ISODate("2020-08-25T22:56:12Z"),

                        "optimeDurableDate" : ISODate("2020-08-25T22:56:12Z"),

                        "lastHeartbeat" : ISODate("2020-08-25T22:56:15.620Z"),

                        "lastHeartbeatRecv" : ISODate("2020-08-25T22:56:14.364Z"),

                        "pingMs" : NumberLong(0),

                        "lastHeartbeatMessage" : "",

                        "syncingTo" : "127.0.0.1:20002",

                        "syncSourceHost" : "127.0.0.1:20002",

                        "syncSourceId" : 1,

                        "infoMessage" : "",

                        "configVersion" : 16218

                }

        ],

        "ok" : 1,

        "operationTime" : Timestamp(1598396172, 1),

        "$clusterTime" : {

                "clusterTime" : Timestamp(1598396172, 1),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        }

}

MongoDB Enterprise OurReplica: PRIMARY>

Now the port number 30000 is modified in the primary node. 

Verify the Replication TimeStamp using printReplicationInfo Commands:

MongoDB Enterprise OurReplica:PRIMARY> rs.printReplicationInfo();

configured oplog size:   1436.840576171875MB

log length start to end: 61486517secs (17079.59hrs)

oplog first event time:  Fri Sep 14 2018 12:52:15 GMT+0530 (IST)

oplog last event time:   Wed Aug 26 2020 04:27:32 GMT+0530 (IST)

now:                     Wed Aug 26 2020 04:27:37 GMT+0530 (IST)

MongoDB Enterprise OurReplica:PRIMARY>

Here oplog size is 1436MB

Started first event time : Sep 14th 2018

Latest event time : Aug 26th 2020

Note: The Oplog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databasesMongoDB applies database operations on the primary and then records the operations on the primary's oplog


e.     Now Add the Arbiter Node with  Primary: :

Since we have two slave members in our replica set then we should have one arbiter node to participate in the election 

MongoDB Enterprise OurReplica:PRIMARY> rs.addArb("127.0.0.1:20004");

Note: Arbiters are MongoDB instances that are part of a replica set but do not hold data. Arbiters participate in elections in order to break ties. If a replica set has an even number of members then you can add an arbiter



STEP 6: Let us check the configuration of the replica set after adding Arbiter node 

MongoDB Enterprise OurReplica:PRIMARY> rs.conf()

{

        "_id" : "OurReplica",

        "version" : 16219,

        "protocolVersion" : NumberLong(1),

        "writeConcernMajorityJournalDefault" : true,

        "members" : [

                {

                        "_id" : 0,

                        "host" : "localhost:30000",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {


                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 1,

                        "host" : "127.0.0.1:20002",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {


                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 2,

                        "host" : "127.0.0.1:20003",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {


                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 3,

                        "host" : "127.0.0.1:20004",

                        "arbiterOnly" : true,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 0,

                        "tags" : {


                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                }

        ],

        "settings" : {

                "chainingAllowed" : true,

                "heartbeatIntervalMillis" : 2000,

                "heartbeatTimeoutSecs" : 10,

                "electionTimeoutMillis" : 10000,

                "catchUpTimeoutMillis" : -1,

                "catchUpTakeoverDelayMillis" : 30000,

                "getLastErrorModes" : {


                },

                "getLastErrorDefaults" : {

                        "w" : 1,

                        "wtimeout" : 0

                },

                "replicaSetId" : ObjectId("5b9b61a67b1454a13fc7610a")

        }

}

MongoDB Enterprise OurReplica:PRIMARY>


Final check : A)

MongoDB Enterprise OurReplica:PRIMARY> rs.isMaster();

{

        "hosts" : [

                "localhost:30000",

                "127.0.0.1:20002",

                "127.0.0.1:20003"

        ],

        "arbiters" : [

                "127.0.0.1:20004"

        ],

        "setName" : "OurReplica",

        "setVersion" : 16219,

        "ismaster" : true,

        "secondary" : false,

        "primary" : "localhost:30000",

        "me" : "localhost:30000",

        "electionId" : ObjectId("7fffffff0000000000000012"),

        "lastWrite" : {

                "opTime" : {

                        "ts" : Timestamp(1598461787, 1),

                        "t" : NumberLong(18)

                },

                "lastWriteDate" : ISODate("2020-08-26T17:09:47Z"),

                "majorityOpTime" : {

                        "ts" : Timestamp(1598461787, 1),

                        "t" : NumberLong(18)

                },

                "majorityWriteDate" : ISODate("2020-08-26T17:09:47Z")

        },

        "maxBsonObjectSize" : 16777216,

        "maxMessageSizeBytes" : 48000000,

        "maxWriteBatchSize" : 100000,

        "localTime" : ISODate("2020-08-26T17:09:49.132Z"),

        "logicalSessionTimeoutMinutes" : 30,

        "minWireVersion" : 0,

        "maxWireVersion" : 7,

        "readOnly" : false,

        "ok" : 1,

        "operationTime" : Timestamp(1598461787, 1),

        "$clusterTime" : {

                "clusterTime" : Timestamp(1598461787, 1),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        }

}

MongoDB Enterprise OurReplica:PRIMARY>

[ _id0 -> Primary node , _id1 -> Slave node 1, _id2 -> Skave node 2 , _id3 -> arbiter node ] 

Let us check the database status in primary :

MongoDB Enterprise OurReplica:PRIMARY> show dbs;
AshokDB     0.000GB
FOver_Test  0.000GB
RepDB       0.000GB
admin       0.000GB
config      0.000GB
local       0.001GB
MongoDB Enterprise OurReplica:PRIMARY>

[mdashok@MongoDB mongodb]$ mongo --port 20002
MongoDB Enterprise OurReplica:SECONDARY> rs.slaveOk();
MongoDB Enterprise OurReplica:SECONDARY> show dbs;
AshokDB     0.000GB
FOver_Test  0.000GB
RepDB       0.000GB
admin       0.000GB
config      0.000GB
local       0.001GB
MongoDB Enterprise OurReplica:SECONDARY>

[mdashok@MongoDB mongodb]$ mongo --port 20003
MongoDB Enterprise OurReplica:SECONDARY> rs.slaveOk();
MongoDB Enterprise OurReplica:SECONDARY> show dbs;
AshokDB     0.000GB
FOver_Test  0.000GB
RepDB       0.000GB
admin       0.000GB
config      0.000GB
local       0.000GB
MongoDB Enterprise OurReplica:SECONDARY>


Step by Step YugabyteDB 2.11 (Open Source) Distributed DB - Multi-node Cluster Setup on RHEL

Scope - ·        Infrastructure planning and requirement for installation of the multi-node cluster database ·        Prerequisites Software...