How To Create Replica Set In Mongodb
Create a MongoDB replica set in Windows
A replica ready in MongoDB is a group of mongod instances that maintain the aforementioned information set up. Information technology contains several data bearing nodes and optionally one arbiter node.
Of the data bearing nodes, one and simply i fellow member is deemed the primary node, while the other nodes are deemed secondary nodes.
The principal node receives all write operations and records all changes to its data sets in its performance log, i.east. oplog.
The secondaries replicate the primary's oplog and utilise the operations to their information sets asynchronously.
Past having the secondaries' data sets reflect the main'due south data set, the replica set can continue to part despite the failure of 1 or more members.
If the primary is unavailable, an eligible secondary will concord an election to elect itself the new principal.
For more detail about MongoDB replication, delight refer to MongoDB documentation.
In this commodity, nosotros are going to create a exam replica set in Windows machine.
This replica set volition contain one primary node and two secondary nodes.
Add replication options into MongoDB config file
On Windows, a default <install directory>/bin/mongod.cfg configuration file is included during the installation.
Open mongod.cfg file, add together replication options
replication:
oplogSizeMB: <int>
replSetName: <string>
enableMajorityReadConcern: <boolean>
Here we will only specify replSetName which is the replica set name, and leave oplogSizeMB and enableMajorityReadConcern every bit default value. Yous can refer to MongoDB configuration file manual to arrange them.
So our replication setting looks like this:
replication:
replSetName: rs0
Catechumen a standalone case to a replica set
Now, if you already have a mongod instance running, please close downward it.
if you have installed MongoDB as a service, delight restart it.
Open a new Command Prompt with Ambassador mode, restart the instance as a member of the replica set :
mongod --port 27017 --replSet rs0 --dbpath="C:\data\db0"
You can see nosotros use the replSet option to specify the replica ready proper name which is the the proper noun nosotros define in mongod.cfg just now.
Then this mongod instance is started. Open a new command prompt, connect to the instance with default port "27017" by just using "mongo" control
One time connected, we are going to initiate the replica set by command
rs.initiate()
Add new members to the replica fix
The replicate prepare is now operational, we can first to add new members.
Open a new Command Prompt, create a new instance listening to port 27027.
mongod --port 27027 --replSet rs0 --dbpath="C:\data\db1"
Repeat the stride for 3rd instance listening to port 27037.
mongod --port 27037 --replSet rs0 --dbpath="C:\data\db2"
Add the ii instances into our replica set up using rs.add together() command
rs.add( { host: "127.0.0.i:27027", priority: 0, votes: 0 } )
rs.add( { host: "127.0.0.1:27037", priority: 0, votes: 0 } )
Check replica set up status
At present nosotros take i principal node and two secondary nodes in the replica set.
Lets confirm it by rs.status() method
rs.status()
If yous connect to this replica set by MongoDB Compass, you tin see a list of our 3 nodes in the cluster :
Now feel costless to play effectually with this replica set, in next article I volition show y'all how to downgrade replica gear up back to a standalone node.
How To Create Replica Set In Mongodb,
Source: https://adelachao.medium.com/create-a-mongodb-replica-set-in-windows-edeab1c85894
Posted by: reinhardtdomse1987.blogspot.com
0 Response to "How To Create Replica Set In Mongodb"
Post a Comment