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.
- Three Servers with CentOS 7 installed
- 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.
Edit the hosts file with vim.
Paste hosts configuration below:
Save the file and exit.
Next, We will disable SELinux by editing the configuration file with vim.
Change value 'enforcing' to 'disabled'.
Save and exit, then reboot the servers.
Check the SELinux status with the command.
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
Next, install the mongodb-org package with yum command.
sudo yum -y install mongodb-org
MongoDB has been Installed on all CentOS 7 nodes.
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.
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.
In the 'net' section line 27, comment the 'bindIP'.
# bindIP: 127.0.0.1
Next, uncomment replication line 36, and set the replication name to '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.
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.
Initiate the replica set from the mongo1 server with the query below.
Make sure 'ok' value is 1.
Now add the 'mongo2' and 'mongo3' nodes to the replica sets.
You will see the results below and make sure there is no error.
Next, check the replica sets status with the rs query below.
Another query to check the status is:
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.
Now create a new database 'lemp' and new 'stack' collection for the database.
"desc": "LEMP Stack",
"apps": ["Linux", "Nginx", "MySQL", "PHP"],
Next, go to the 'SECONDARY' node 'mongo2' and open the mongo shell.
Enable reading from the 'SECONDARY' node with the query 'rs.slaveOk()', and then check if the 'lemp' database exists on the 'SECONDARY' nodes.
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.