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.

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

2.On each Linux server, complete the following steps to install the MongoDB Server.

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)Log in to the Linux server as the root user.

Note: It is highly recommended to install numactl on the MongoDB Server to optimize its performance. Run the rpm -qa|grep numactl command to check whether numactl has been installed. If it is not 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)Obtain the mongodb-linux-x86_64-rhel7-3.6.4.tar.gz file from NetBrain, and then copy it to the /opt/netbraintemp directory.

Option 1: If the Linux server has no access to the Internet, upload the mongodb-linux-x86_64-rhel7-3.6.4.tar.gz file to the /opt/netbraintemp directory by using a file transfer tool.  

Option 2: If the Linux server has access to the Internet and the yum service has been installed.

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

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

c)Run the wget <NetBrain IE 7.1a2 MongoDB download link> command under the /opt/netbraintemp directory to directly download the mongodb-linux-x86_64-rhel7-3.6.4.tar.gz file to this directory.

Tip: Contact NetBrain Support Team to get the download link address.

Note: The download link is case-sensitive.

4)Run the tar -zxvf mongodb-linux-x86_64-rhel7-3.6.4.tar.gz command under the /opt/netbraintemp directory to extract installation files.

[root@localhost netbraintemp]# tar -zxvf mongodb-linux-x86_64-rhel7-3.6.4.tar.gz
MongoDB/
MongoDB/fix_releaseinfo.json
MongoDB/install.sh
MongoDB/install_mongodb.conf
MongoDB/mongodbconfig-7-1.noarch.rpm
MongoDB/mongodbconfig.sh
MongoDB/preinstallcomponents/
MongoDB/preinstallcomponents.sh
MongoDB/preinstallcomponents/CentOS7/
MongoDB/preinstallcomponents/CentOS7/lsof-4.87-4.el7.x86_64.rpm
MongoDB/preinstallcomponents/RedHat7/
MongoDB/preinstallcomponents/RedHat7/lsof-4.87-4.el7.x86_64.rpm

5)Run the cd MongoDB command to navigate to the MongoDB directory.

6)Modify the parameters in the install_mongodb.conf file located under the MongoDB 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.

#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.
DBServiceName      mongod
DBSystemUser       netbrain
DBSystemGroup      netbrain
ConfPath           /home/mongodb
DataPath           /home/mongodb/data
LogPath            /home/mongodb/log
BindIp             10.10.3.142
#The port must be between 1025 and 65535.
DBPort             27017
#Replica set name can only contain letters and numbers and start with a letter.
ReplicaSetName     rs
UseSSL             no
Certificate        /etc/ssl/cert.pem
PrivateKey         /etc/ssl/key.pem
#either dbuser or dbpassword was set to empty,mongodbconfig.sh will not add user and password
#The DBUser or DBPassword should not contain: {}[]:",'|<>@&^%\ or a space.
#The length of DBUsername or DBPassword should not be more than 64 characters.
DBUser             mongodb
DBPassword         mongodb
#CGroups config
CPULimit           55%
MemoryLimit        55%
#CPULimit            35%
#MemoryLimit         35%
#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.0 is not allowed. For example:
#ReplicaSetMembers  192.168.1.1:27017 192.168.1.2:27017 192.168.1.3:27017
ReplicaSetMembers  10.10.3.142:27017 10.10.3.143:27017 10.10.3.144:27017
...

7)Run the ./preinstallcomponents.sh command under the MongoDB directory to install the components that must be installed before installing MongoDB.

8)Run the rpm -ivh mongodbconfig-7-1.noarch.rpm command under the MongoDB directory to install MongoDB.

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

10) Restart the machine manually by entering the reboot command.

3.On the primary MongoDB node, run the ./mongodbconfig.sh command under the MongoDB directory to initialize the MongoDB service as well as the login username and password.

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

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

1)Run the following command at the command line to connect to the MongoDB Server:

If SSL is not enabled:

mongo --host <hostname> -u <username> -p <password> --authenticationDatabase <database_name>

If SSL is enabled:

mongo --host <hostname> -u <username> -p <password> --authenticationDatabase <database_name> --ssl --sslAllowInvalidCertificates

<hostname> — the IP address and port number of the primary or secondary MongoDB node.

<username> the admin username used to log in to each MongoDB node.

<password> the admin password used to log in to each MongoDB node.

<database_name> — the name of the database to include authentication credentials in the form of username:password@.

Example for non-SSL connections:
mongo --host 10.10.3.142:27017 -u mongodb -p mongodb --authenticationDatabase admin

Example for SSL encrypted connections:
mongo --host 10.10.3.142:27017 -u mongodb -p mon godb --authenticationDatabase admin --ssl --sslAllowInvalidCertificates

2)If the connection is successfully established, you can browse the replica set information as follows. This is an example:

rsnetbrain:PRIMARY> rs.status()
{
    "set" : "rsnetbrain",
    "date" : ISODate("2017-08-24T03:28:26.402Z"),
    "myState" : 1,
    "term" : NumberLong(8)
    "heartbeatIntervalMillis" : NumberLong(2000)
    "optimes" : {
       "lastCommittedOpTime" : {
       "ts" : Timestamp(1534355692, 1),
       "t" : NumberLong(5)
       }, 
       "appliedOpTime" : {
       "ts" : Timestamp(1534355692, 1),
       "t" : NumberLong(5)
       },
       "durableOpTime" : {
       "ts" : Timestamp(1534355692, 1),
       "t" : NumberLong(5)
       }
     },
    "members" : [
       {
              "_id" : 0,
              "name" : "10.10.3.142:27017",
              "health" : 1,
              "state" : 1,
              "stateStr" : "PRIMARY",
              "uptime" : 665185,
              "optime" : {
                  "ts" : Timestamp(1503545304, 2),
                  "t" : NumberLong(8)
                },
                "optimeDate" : ISODate("2017-08-24T03:28:24Z"),
                "electionTime" : Timestamp(1502880141,1),
                "electionDate" : ISODate("2017-08-16T10:42:21Z"),
                "configVersion" : 1
                "self" : true
        },
        {
              "_id" : 0,
              "name" : "10.10.3.143:27017",
              "health" : 1,
              "state" : 2,
              "stateStr" : "SECONDARY",
              "uptime" : 665175,
              "optime" : {
                  "ts" : Timestamp(1503545304, 2),
                  "t" : NumberLong(8)
               },
               "optimeDate" : ISODate("2017-08-24T03:28:24Z"),
               "lastHeartbeat" : ISODate("2017-08-24T03:28:25.272Z"),
               "lastHeartbeatRecv" : ISODate("2017-08-24T03:28:25.277Z"),
               "pingMs" : NumberLong(0)
               "syncingTo" : "10.10.3.142:27017",
               "configVersion" : 1
          },
          {
              "_id" : 2,
              "name" : "10.10.3.144:27017",
              "health" : 1,
              "state" : 7,
              "stateStr" : "ARBITER",
              "uptime" : 79953,
              "lastHeartbeat" : ISODate("2017-08-24T03:28:25.364Z"),
              "lastHeartbeatRecv" : ISODate("2017-08-24T03:28:22.822Z"),
              "pingMs" : NumberLong(0)
              "configVersion" : 1
           }
       ],
       "ok" : 1
}

Note: When your disk space is insufficient for large amounts of logs, you can modify the log settings in the mongod.conf file under the directory configured in the ConfPath argument. For example, to reduce the log size, you can add the verbosity argument to set the log level, or add the logRotate argument to overwrite the log file. See the documentation on MongoDB official website for reference.

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

MongoDB Server Parameters

Refer to the following table for the parameters of MongoDB Server.

Parameter

Default Value

Description

DBServiceName

mongod

The service name of the MongoDB Server. Keep the default name as it is.

Note: The DBServiceName, DBSystemUser, and DBSystemGroup parameters can only contain letters and numbers.

DBSystemUser

netbrain

The system user created on the Linux server to run the MongoDB service. Keep the default name as it is.

Note: If you modified the default umask value, make sure this system user has the read and execute permissions to all the directories (including their parent directories) where the MongoDB Server is installed. These directories include that you specified in ConfPath, DataPath, LogPath, CertPath, and KeyPath.

DBSystemGroup

netbrain

The system group created on the Linux server to run the MongoDB service. Keep the default name as it is.

ConfPath

/opt/mongodb

The storage path for the MongoDB configuration file.

Note: If you modified the default umask value, you must run the chmod o+rx /opt/mongodb command to assign the read and execute permissions of the /opt/mongodb directory to the system user that you specify in the DBSystemUser parameter. This note also applies to DataPath, LogPath, CertPath, and KeyPath.

Note: The ConfPath parameter has the following requirements. The same rules apply to the DataPath, LogPath, CertPath, and KeyPath parameters.

- Cannot be placed under any personal directories. For example, /root is not allowed.

- Cannot contain "/var/lib". For example, /var/lib/mogodb/log is not allowed.

- Cannot contain any special characters or spaces, except slashes (/).

DataPath

/opt/mongodb/data

The storage path for all MongoDB data files.

Note: Make sure the destination directory has more than 50GB free space to save all the data files on the primary and secondary nodes, and has more than 30GB free space on the arbiter node.

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

LogPath

/opt/mongodb/log

The storage path for all MongoDB log files.

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

BindIp

192.168.1.1

The IP address of this MongoDB Server.

Note: Don't use 127.0.0.1.

Note: Don't use 0.0.0.0 if you want to deploy a replica set.

Note: If you modify the parameter to 0.0.0.0, you must use the fully qualified domain name (FQDN) in the ReplicaSetMembers parameter and the MongoDB connection information in the installation of other system components.

DBPort

27017

The port number that the MongoDB service listens to. Keep the default value as it is.

ReplicaSetName

rs

The replica set name used for replication.

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, DBUser, and DBPassword.

UseSSL

no

Whether to encrypt the connections to this MongoDB Server with SSL. To enable SSL, replace no with yes.

For detailed requirements of SSL certificates and keys, see Appendix.

Certificate

/etc/ssl/cert.pem

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

Note: It is required only if UseSSL is enabled.

PrivateKey

/etc/ssl/key.pem

The name and storage path of the private key file.

Note: It is required only if UseSSL is enabled.

DBUser

mongodb

The admin username used to log into the MongoDB Server.

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

DBPassword

mongodb

The admin password used to log into the MongoDB Server.

CPULimit

55%

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

MemoryLimit

55%

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

ReplicaSetMembers

192.168.1.1:27017

Enter the IP address or FQDN, and port number of the server.

Note: Enter those of the cluster members in order if you deploy a replica set.

 

See also:

Connecting to MongoDB Server