MongoDB Replication on CentOS 7
MongoDB is a NoSQL enterprise-class database that offers high performance, high availability, and automatic scaling. On a NoSQL database, so you can't use SQL (Structured Query Language) to insert and retrieve data, and it does not store data in tables like MySQL or Postgres. Data is stored in a "document" structure in JSON format (in MongoDB called BSON) instead. MongoDB was first introduced in 2009 and is currently developed by the company MongoDB Inc.
In this tutorial, I will guide you step-by-step build a replica set in MongoDB. We will use 3 server nodes with CentOS 7 installed on them and then install and configure MongoDB.
Prerequisites
- Three Servers with CentOS 7 installed
10.0.15.21 mongo1
10.0.15.22 mongo2
10.0.15.23 mongo3
- Root privileges
Step 1 - Prepare the server
In this tutorial, I will disable SELinux for MongoDB. We need to edit the SELinux configuration file, and change value 'enforcing' to disabled.
Connect to all server nodes with ssh.
ssh root@mongo1
ssh root@mongo2
ssh root@mongo3
Edit the hosts file with vim.
vim /etc/hosts
Paste hosts configuration below:
10.0.15.21 mongo1
10.0.15.22 mongo2
10.0.15.23 mongo3
Save the file and exit.
Next, We will disable SELinux by editing the configuration file with vim.
vim /etc/sysconfig/selinux
Change value 'enforcing' to 'disabled'.
SELINUX=disabled
Save and exit, then reboot the servers.
reboot
Check the SELinux status with the command.
getenforce
Make sure you get 'Disabled' as the result.
Step 2 - Install MongoDB on All Nodes
In this step, we will install MongoDB from the official repository. The first step is to add the new MongoDB 3.4 repository to '/etc/yum.repos.d/' directory.
Run the command below to add the repository.
cat <<'EOF' >> /etc/yum.repos.d/mongodb.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
EOF
Next, install the mongodb-org package with yum command.
sudo yum -y install mongodb-org
MongoDB has been Installed on all CentOS 7 nodes.
Note:
If you want a complete tutorial about 'MongoDB Installation', you see this link.
Step 3 - Configure Firewalld
In the first step, we already disabled SELinux. For security reasons, we will now enable firewalld on all nodes and open only the ports that are used by MongoDB and SSH.
Install Firewalld with the yum command.
yum -y install firewalld
Start firewalld and enable it to start at boot time.
systemctl start firewalld
systemctl enable firewalld
Next, open your ssh port and the MongoDB default port 27017.
firewall-cmd --permanent --add-port=22/tcp
firewall-cmd --permanent --add-port=27017/tcp
Reload firewalld to apply the changes.
firewall-cmd --reload
Step 4 - Configure MongoDB Replica Set
A Replica Set is a group of mongod processes in MongoDB that maintain same data and information. The Replica Set provides high-availability and fault tolerance for production deployments of the database.
Replication in mongodb is composed of several MongoDB server instances running mongod process, only one instance runs as 'PRIMARY', all other instances are 'SECONDARY'. Data is written only on the 'PRIMARY' instance, the data sets are then replicated to all 'SECONDARY' instances.
In this step, we will prepare all server nodes to implement the replica sets in MongoDB.
Edit the MongoDB configuration file mongod.conf file with vim.
vim /etc/mongod.conf
In the 'net' section line 27, comment the 'bindIP'.
net:
port: 27017
# bindIP: 127.0.0.1
Next, uncomment replication line 36, and set the replication name to 'myreplica01'.
replication:
replSetName: "myreplica01"
Save th file and exit vim.
Restart MongoDB on all nodes.
systemctl restart mongod
Now check mongodb and makesure it's running on the server ipadress, not localhost ipaddress.
netstat -plntu
Step 5 - MongoDB Replica Set initiate
In this step, we will create the replica set. We will use the 'mongo1' server as 'PRIMARY' node, and 'mongo2' and 'mongo3' as 'SECONDARY' nodes.
Login to the mongo1 server and start the mongo shell.
ssh root@mongo1
mongo
Initiate the replica set from the mongo1 server with the query below.
rs.initiate()
Make sure 'ok' value is 1.
Now add the 'mongo2' and 'mongo3' nodes to the replica sets.
rs.add("mongo2")
rs.add("mongo3")
You will see the results below and make sure there is no error.
Next, check the replica sets status with the rs query below.
rs.status()
Another query to check the status is:
rs.isMaster()
Step 6 - Test the Replication
Test the data set replication from the 'PRIMARY' instance 'mongo1' to 'SECONDARY' nodes 'mongo2' and 'mongo3'.
In this step, we will try to write or create a new database on the 'PRIMARY' node 'mongo1', then check if the replication is working by checking the database on 'SECONDARY' nodes 'mongo2' and 'mongo3'.
Login to the 'mongo1' server and open mongo shell.
ssh root@mongo1
mongo
Now create a new database 'lemp' and new 'stack' collection for the database.
use lemp
db.stack.save(
{
"desc": "LEMP Stack",
"apps": ["Linux", "Nginx", "MySQL", "PHP"],
})
Next, go to the 'SECONDARY' node 'mongo2' and open the mongo shell.
ssh root@mongo2
mongo
Enable reading from the 'SECONDARY' node with the query 'rs.slaveOk()', and then check if the 'lemp' database exists on the 'SECONDARY' nodes.
rs.slaveOk()
show dbs
use lemp
show collections
db.stack.find()
If there is no error, you will see results below:
The database from the 'PRIMARY' node has been replicated to the 'SECONDARY' nodes, the database 'lemp' from the 'mongo1' instance replicated sucessfully to the 'mongo2' and 'mongo3' instances.
A MongoDB Replica Set has been successfully created.