An official explanation of what is replica set in MongoDB.
A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.
Replication provides redundancy and increases data availability. With multiple copies of data on different database servers, replication provides a level of fault tolerance against the loss of a single database server.
In this post, for simplicity, I am going to setup a mongodb replica set environment like the image below.
Arbiter is a special role in this environment, it does not maintain any data but to maintain a quorum in a replica set by responding to hearteat and election rquests by other replica set members.
Steps for this tutorial,
- Prepare three servers with mongodb installed. Make sure each server is able to coonect to every other servers through 27017 port. Please refer: MongoDB-3.6 Community Edition Installation
- Create necessary users and enable authentication. Please refer: MongoDB Enable User Login Authentication
- Create Keyfile Security for replica set internal authentication access control.
/etc/mongod.confto the use of replication set.
- Configure and start replica set service, check result.
- Add arbiter and check result.
Assuming you have done steps 1 & 2, my servers information is below.
Create Keyfile Security
mongo1 which is used by all the mongod instances within the relica set for authentication. You must save keyfile at a location where mongod has read privilege. Please run the commands with root privilege.
1 2 3 openssl rand -base64 756 > /var/lib/mongo/mongodb-keyfile chmod 400 /var/lib/mongo/mongodb-keyfile chown mongod.mongod /var/lib/mongo/mongodb-keyfile
Next, copy the keyfile to
arbiter. Make sure mongod on both server has read privilege of the file.
Modify mongod.conf to the use of replication
We need to modify
arbiter so that it will authenticate with keyfile and also set a name for replica set. Edit bindIp in according to your server ip.
1 2 3 4 5 6 7 8 9 10 # network interfaces net: port: 27017 bindIp: 192.168.122.134 # change ip to mongo2 and arbiter accordingly. security: keyFile: /var/lib/mongo/mongodb-keyfile replication: replSetName: bulafish
1 systemctl start mongod
Configure, start replica set and check result
Next we need to initial and conf the replica set, let’s login to
mongo1 using syntax
mongo --host serverip -u username -p --authenticationDatabase admin.
mongo --host 192.168.122.134 -u blogroot -p --authenticationDatabase admin
Initial replica set.
Now we config the replica set. At this stage, we can see that
bulafish replica set has been configured.
Now we add
mongo2 into replica set.
Check the current status of replica set. We will see that
mongo2 are in the
members section with mongo1 showing
primary and mongo2 showing
Add arbiter into replica set
rs.addArb() to add an arbiter into relica set.
Check the status after arbiter has been added.
That’s it, we have deployed an replica set environment with an arbiter!