Installing MongoDB Replica Set on Linux

For redundancy and fault tolerance, you can set up a MongoDB replica set. A replica set should always have an odd number of members.

Primary node — the only member in the replica set that receives write operations.

Secondary node — replicates the primary log and applies the operations to its data sets.

Arbiter node — only votes in elections for primary, but cannot become a primary.

Example: Set up a three-member replica set.

Note: Install the arbiter node first, and then the secondary node, finally the primary node.

Note: Make sure that your network configurations allow communications among all nodes.

1.Prepare three Linux servers. See System Requirements for High Availability for more details.

2.Complete the following steps to install the arbiter, secondary, and primary nodes.

1)Log in to each Linux server as the root user.

Note: It is highly recommended to install numactl on the Linux server to optimize MongoDB performance. Run the rpm -qa|grep numactl command to check whether numactl has been installed. If it has not been installed, run the yum install numactl command to install it.

2)Run the mkdir command to create a directory under the /opt directory to place the installation package. For example, netbraintemp.

Note: Don't place the installation package under any personal directories, such as /root.

3)Run the cd /opt/netbraintemp command to navigate to the /opt/netbraintemp directory.

4)Download the installation package.

Option 1: If the Linux server has no access to the Internet, obtain the mongodb-linux-x86_64-rhel7-4.0.6-10.0a.tar.gz file from NetBrain and then upload it to the /opt/netbraintemp directory by using a file transfer tool.  

Option 2: If the Linux server has access to the Internet, run the wget <download link> command under the /opt/netbraintemp directory to download the mongodb-linux-x86_64-rhel7-4.0.6-10.0a.tar.gz file from NetBrain official download site.

Note: Contact NetBrain Support Team to get the download link. The download link is case-sensitive.

Tip: Run the yum -y install wget command to install the wget command if it has not been installed.

5)Run the tar -zxvf mongodb-linux-x86_64-rhel7-4.0.6-10.0a.tar.gz command under the /opt/netbraintemp directory to extract installation files.

[root@centos netbraintemp]# tar -zxvf mongodb-linux-x86_64-rhel7-4.0.6-10.0a.tar.gz
MongoDB/
MongoDB/config/
MongoDB/config/install.conf
MongoDB/config/setup.conf
...
MongoDB/replica/
MongoDB/replica/install_arbiter.sh
MongoDB/replica/install_primary.sh
MongoDB/replica/install_secondary.sh
...

6)Run the cd MongoDB/config command to navigate to the config directory.

7)Modify the parameters in the setup.conf file located under the config directory according to your environment and save the changes. For how to modify the configuration file, see Appendix: Editing a File with VI Editor for more details.

Note: In the following example, 10.10.3.142 is the IP address of the primary node; 10.10.3.143 is the IP address of the secondary node; 10.10.3.144 is the IP address of the arbiter node.

Sample Configurations of the Arbiter Node:

[root@centos config]# vi setup.conf
#NetBrain Database configuration file
#Note: other than the database username and password, other entries
#can only contain letters and numbers, and should start with a letter.
DataPath=/usr/lib/mongodb
LogPath=/var/log/mongodb
BindIp=10.10.3.144
FQDN=127.0.0.1
#The port must be between 1025 and 65535.
Port=27017
ReplicaSetName=rs
UseSSL=no
Certificate=/etc/ssl/cert.pem
PrivateKey=/etc/ssl/key.pem
#The UserName or Password cannot be empty
#The UserName or Password should not contain: {}[]:",'|<>@&^%\ or a space.
#The length of UserName or Password should not be more than 64 characters.
UserName=admin
Password=admin
CPULimit=55%
MemoryLimit=55%
#List all replica set members. The members should be separated with spaces. The total number of members should an odd number.
#The first member will be used as the primary member, the last will be used as the arbiter. The rest are the secondary members.
#It is recommended to use FQDN. The address of 0.0.0.0 or 127.0.0.1 is not allowed. For example:
#ReplicaSetMembers="192.168.1.1 192.168.1.2 192.168.1.3"
ReplicaSetMembers=10.10.3.142 10.10.3.143 10.10.3.144

Sample Configurations of the Secondary Node:

[root@centos config]# vi setup.conf
#NetBrain Database configuration file
#Note: other than the database username and password, other entries
#can only contain letters and numbers, and should start with a letter.
DataPath=/usr/lib/mongodb
LogPath=/var/log/mongodb
BindIp=10.10.3.143
FQDN=127.0.0.1
#The port must be between 1025 and 65535.
Port=27017
ReplicaSetName=rs
UseSSL=no
Certificate=/etc/ssl/cert.pem
PrivateKey=/etc/ssl/key.pem
#The UserName or Password cannot be empty
#The UserName or Password should not contain: {}[]:",'|<>@&^%\ or a space.
#The length of UserName or Password should not be more than 64 characters.
UserName=admin
Password=admin
CPULimit=55%
MemoryLimit=55%
#List all replica set members. The members should be separated with spaces. The total number of members should an odd number.
#The first member will be used as the primary member, the last will be used as the arbiter. The rest are the secondary members.
#It is recommended to use FQDN. The address of 0.0.0.0 or 127.0.0.1 is not allowed. For example:
#ReplicaSetMembers="192.168.1.1 192.168.1.2 192.168.1.3"
ReplicaSetMembers=10.10.3.142 10.10.3.143 10.10.3.144

Sample Configurations of the Primary Node:

[root@centos config]# vi setup.conf
#NetBrain Database configuration file
#Note: other than the database username and password, other entries
#can only contain letters and numbers, and should start with a letter.
DataPath=/usr/lib/mongodb
LogPath=/var/log/mongodb
BindIp=10.10.3.142
FQDN=127.0.0.1
#The port must be between 1025 and 65535.
Port=27017
ReplicaSetName=rs
UseSSL=no
Certificate=/etc/ssl/cert.pem
PrivateKey=/etc/ssl/key.pem
#The UserName or Password cannot be empty
#The UserName or Password should not contain: {}[]:",'|<>@&^%\ or a space.
#The length of UserName or Password should not be more than 64 characters.
UserName=admin
Password=admin
CPULimit=55%
MemoryLimit=55%
#List all replica set members. The members should be separated with spaces. The total number of members should an odd number.
#The first member will be used as the primary member, the last will be used as the arbiter. The rest are the secondary members.
#It is recommended to use FQDN. The address of 0.0.0.0 or 127.0.0.1 is not allowed. For example:
#ReplicaSetMembers="192.168.1.1 192.168.1.2 192.168.1.3"
ReplicaSetMembers=10.10.3.142 10.10.3.143 10.10.3.144

8)Complete the following steps to install MongoDB as well as create an admin username and password for MongoDB on each node.

a)On the arbiter node, run the cd MongoDB/replica command to navigate to the replica directory, and then run the ./install_arbiter.sh command.

b)After the arbiter node is successfully installed, on the secondary node, run the cd MongoDB/replica command to navigate to the replica directory, and then run the ./install_secondary.sh command.

c)After the secondary node is successfully installed, on the primary node, run the cd MongoDB/replica command to navigate to the replica directory, and then run the ./install_primary.sh command.

Note: You do not need to initialize NetworkBrain data in all MongoDB nodes because the Installation Wizard will automatically perform the initialization when you install Web API Server or Worker Server later.

9)After each MongoDB node is successfully installed, run the reboot command to restart the machine.

10) Run the ps -ef|grep mongo command to verify whether its service starts successfully.

[root@centos ~]# ps -ef|grep mongo
netbrain  46482     1  3 01:30 ?        00:00:03 /bin/mongod -f /etc/mongodb/mongod.conf
root      46639  37939  0 01:31 pts/2    00:00:00 grep --color=auto mongo

3.Complete the following steps to verify the replica set:

1)Run the mongo command on the arbiter node.

[root@centos ~]# mongo
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("40d16429-e6a9-4da6-8c78-2becb1df1a5b") }
MongoDB server version: 4.0.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
rs:ARBITER>

2)Run the rs.status () command to browse the replica set information.

rs:ARBITER> rs.status()
{
        "set" : "rs",
        "date" : ISODate("2019-05-07T06:43:27.054Z"),
        "myState" : 7,
        "term" : NumberLong(6),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1557211399, 1),
                        "t" : NumberLong(6)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1557211399, 1),
                        "t" : NumberLong(6)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1557211399, 1),
                        "t" : NumberLong(6)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(0, 0),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.10.3.142:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 157,
                        "optime" : {
                                "ts" : Timestamp(1557211399, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1557211399, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDate" : ISODate("2019-05-07T06:43:19Z"),
                        "optimeDurableDate" : ISODate("2019-05-07T06:43:19Z"),
                        "lastHeartbeat" : ISODate("2019-05-07T06:43:25.691Z"),
                        "lastHeartbeatRecv" : ISODate("2019-05-07T06:43:26.321Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1557211258, 1),
                        "electionDate" : ISODate("2019-05-07T06:40:58Z"),
                        "configVersion" : 1
                },
                {
                        "_id" : 1,
                        "name" : "10.10.3.143:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 251,
                        "optime" : {
                                "ts" : Timestamp(1557211399, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1557211399, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDate" : ISODate("2019-05-07T06:43:19Z"),
                        "optimeDurableDate" : ISODate("2019-05-07T06:43:19Z"),
                        "lastHeartbeat" : ISODate("2019-05-07T06:43:25.703Z"),
                        "lastHeartbeatRecv" : ISODate("2019-05-07T06:43:25.174Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "10.10.3.142:27017",
                        "syncSourceHost" : "10.10.3.142:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "10.10.3.144:27017",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 254,
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1
}
rs:ARBITER> 

Note: When your disk space is insufficient for large amounts of logs, you can modify the log settings (including log archive frequency and default archive file size) in the mongod.conf under the /etc/logrotate.d directory.

Tip: It is highly recommended to run the rm -rf /opt/netbraintemp/MongoDB/config/setup.conf command to delete the setup.conf file from each server after all nodes successfully installed because the file may cause security vulnerability.

Tip: NetBrain supports to customize data storage by separating data in different MongoDB instances when you have two or more replica sets. See Storing Data on a Replica Set for more details.

Parameters

The following table describes the parameters that can be configured when installing MongoDB.

Parameter

Default Value

Description

DataPath

/usr/lib

Specify the storage path for all MongoDB data files.

Note: Make sure the destination directory has more than 100GB free space to save all the data files.

Tip: You can run the df -h command to check which directory has been mounted to a large disk.

LogPath

/var/log

Specify the storage path for all MongoDB log files.

Note: Make sure the destination directory has more than 50GB free space to save all the log files.

BindIp

127.0.0.1

Specify the IP address of MongoDB.

Note: Don't use 127.0.0.1.

Note: If you want to use the fully qualified domain name (FQDN) to connect to MongoDB, you need to set it as 0.0.0.0.

Note: Select either to specify the actual value of BindIp or the FQDN for MongoDB by setting BindIp as 0.0.0.0.

FQDN

127.0.0.1

Specify the fully qualified domain name (FQDN) of MongoDB.

Note: If you select to specify the FQDN for MongoDB, you must specify the FQDN in the ReplicaSetMembers parameter and when installing other components that require to connect to MongoDB.

Port

27017

Specify the port number that the MongoDB service listens to. It is recommended to keep the default value.

Note: Each member in the replica set must have the same port number.

ReplicaSetName

rs

Specify the replica set name used for replication. It is recommended to keep the default value. If you want to modify it, keep notes of your customized one because it is required to connect to MongoDB when you install other components, such as Web API Server, Worker Server, Task Engine, and Front Server Controller.

Note: It can only contain letters and numbers, and must start with a letter.

Note: Each member in the replica set must have the same replica set name, UserName, and Password.

UseSSL

no

Specify whether to encrypt the connections to MongoDB with SSL.

To enable SSL, replace no with yes.

Certificate

/etc/ssl/cert.pem

Specify the name and storage path of the certificate file that contains the public key.

Note: It is required only if UseSSL is enabled.

PrivateKey

/etc/ssl/key.pem

Specify the name and storage path of the private key file.

Note: It is required only if UseSSL is enabled.

UserName

admin

Specify the admin username used to connect with and log in to MongoDB.

Note: The value of the DBUser and DBPassword parameters cannot contain any of the following special characters, and their length cannot exceed 64 characters.
{ } [ ] : " , ' | < > @ & ^ % \ and spaces

Password

Admin1.#

Specify the admin password used to connect with and log in to MongoDB.

CPULimit

55%

Specify the maximum CPU utilization that can be consumed by MongoDB. To make both MongoDB and Elasticsearch reasonably share the CPU resources of the same machine, the recommended value is 55%.

MemoryLimit

55%

Specify the maximum memory capacity of the machine that can be consumed by the MongoDB. To make both MongoDB and Elasticsearch utilize the memory resources of the same machine, the recommended value is 55%.

ReplicaSetMembers

127.0.0.1

Enter the actual IP address to be bound or FQDN.