Posts Setup MongoDB Replication With Arbiter Environment
Post
Cancel

Setup MongoDB Replication With Arbiter Environment

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.
mongodb replica

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,

  1. 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
  2. Create necessary users and enable authentication. Please refer: MongoDB Enable User Login Authentication
  3. Create Keyfile Security for replica set internal authentication access control.
  4. Modify /etc/mongod.conf to the use of replication set.
  5. Configure and start replica set service, check result.
  6. Add arbiter and check result.

Assuming you have done steps 1 & 2, my servers information is below.

nameip
mongo1192.168.122.134
mongo2192.168.122.168
Arbiter192.168.122.215

mongodb replica

Create Keyfile Security

Create keyfile on 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

mongodb replica

Next, copy the keyfile to mongo2 and arbiter. Make sure mongod on both server has read privilege of the file.
mongodb replica

Modify mongod.conf to the use of replication

We need to modify /etc/mongod.conf of mongo1, mongo2 and 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

mongodb replica

Now start mongo1, mongo2 and arbiter.

1
systemctl start mongod

mongodb replica mongodb replica mongodb replica

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

mongodb replica

Initial replica set.

rs.initiate()

mongodb replica

Now we config the replica set. At this stage, we can see that bulafish replica set has been configured.

rs.conf()

mongodb replica

Now we add mongo2 into replica set.

rs.add("192.168.122.168")

mongodb replica

Check the current status of replica set. We will see that mongo1 and mongo2 are in the members section with mongo1 showing primary and mongo2 showing secondary.

rs.status()

mongodb replica
mongodb replica

Add arbiter into replica set

Use rs.addArb() to add an arbiter into relica set.

rs.addArb("192.168.122.215")

mongodb replica

Check the status after arbiter has been added.
mongodb replica

That’s it, we have deployed an replica set environment with an arbiter!

REPLATED POSTS:
MongoDB-3.6 Community Edition Installation
MongoDB Enable User Login Authentication

REFERENCES:
Deploy a Replica Set
MongoDB Replica Set 高可用性架構搭建

This post is licensed under CC BY 4.0 by the author.