This blog represents steps required to enable replication in MongoDB when access control is disabled. The related details can be found on the page, Deploy Replica Set.
Following are some of the key aspects which need to be understood in relation with MongoDB replica set:
Following are some of the steps which needed to be done to enable replication with MongoDB:
First and foremost, lets check the replication status of MongoDB instance. Grant the clusterAdmin role to the admin user, if not present. Following command grants the clusterAdmin role to admin user. Before executing following command, make sure you have selected the admin database by executing command such as use admin and login into the admin database using command such as db.auth(“admin”, “adminPassword”).
db.grantRolesToUser("admin", ["clusterAdmin"]);
Once done with above, execute one of the following commands to get information in relation with replication:
rs.conf();
db.runCommand({replSetGetConfig: 1});
Observe that replication is not yet enabled.
Install two additional mongodb instances on, maybe, another VMs. One can also try with docker containers.
Start each member of the replica set by specifying the name of the replicaset using –replSet option. Following is sample command:
sudo mongod --dbpath /path/to/mongo/data --replSet "vflux01"
Make sure the name of replica set is same. In above command, –dbpath is used to specify the path of mongo data directory. The value can be found from /etc/mongod.conf.
Connect to one of the member of the replica set and execute following command, given name of the replicaSet is set to “vflux01”:
rs.initiate( { _id : "vflux01", members: [ { _id : 0, host : "ip_address_of_current_mongo_instance:27017" } ] })
Note that rs.initiate command only needs to be executed on one and only one member of the replica set. Execute following commands to make sure replica set is configured correctly.
rs.conf()
rs.status()
Note that there is just one member with ip configured using rs.initiate command.
Add other two members of the replica set by executing following command:
rs.add("ip_address_other_mongo_instance_1:27017");
Execute following command to check the replication status. Note one additional member added to the replica set.
rs.status()
rs.slaveOk();
Above command allows the current connection to allow read operations to run on secondary members.
Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…
Last updated: 2nd May, 2024 The success of machine learning models often depends on the…
When working on a machine learning project, one of the key challenges faced by data…
Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…
Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…
Last updated: 1st May, 2024 In this post, you will learn the concepts related to…