Installing MongoDB on Linux

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

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

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

Note: Do not place the installation package under any personal directories, such as /root.

3.Run the cd /opt/netbraintemp8.0.3 command to navigate to the /opt/netbraintemp8.0.3 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-8.0.3.tar.gz file from NetBrain and upload it to the /opt/netbraintemp8.0.3 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/netbraintemp8.0.3 directory to directly download the mongodb-linux-x86_64-rhel7-4.0.6-8.0.3.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 on the server.

5.Run the tar -zxvf mongodb-linux-x86_64-rhel7-4.0.6-8.0.3.tar.gz command under the /opt/netbraintemp8.0.3 directory to extract installation files.

[[email protected] netbraintemp8.0.3]# tar -zxvf mongodb-linux-x86_64-rhel7-4.0.6-8.0.3.tar.gz
MongoDB/
MongoDB/config/
MongoDB/config/setup.conf
...
MongoDB/install.sh
MongoDB/others/
MongoDB/others/install.conf
MongoDB/others/setup.conf.template
MongoDB/others/uninstall.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, refer to Editing a File with VI Editor.

[[email protected] 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 be 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

8.Run the cd .. command to navigate to the MongoDB directory.

9. Run the ./install.sh script under the MongoDB directory to install MongoDB as well as create the configured admin username and password for logging in to MongoDB.

[[email protected] MongoDB]# ./install.sh
INFO: Starting to check if rpm exists.
INFO: MongoDB was not installed. Fresh installation is required.
INFO: Creating installation log file
INFO: Preprocessing SUCCEEDED
INFO: Starting to check system
INFO: 2020-07-20 20-50-08.569: Collecting system information SUCCEEDED.
INFO: Starting to check date
INFO: Starting to check Linux OS info
INFO: Starting to check systemd.
INFO: System checking SUCCEEDED
INFO: Dependent packages checking SUCCEEDED
INFO: Username is admin
INFO: SSL enable status is no
INFO: Configuration parameters updating SUCCEEDED
INFO: Configuration parameters checking SUCCEEDED
INFO: Installing primary node of MongoDB.
INFO: Installing /opt/netbraintemp/MongoDB/sources/mongodbconfig-4.0.6-el7.x86_64.rpm
Preparing...                          ########################################
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
16261276
Updating / installing...
mongodbconfig-4.0.6-el7               ########################################
Total memory: 15
cgroup memory: 8
Cache size: 4
Redirecting to /bin/systemctl status firewalld.service
INFO: Official rpm package installing SUCCEEDED
INFO: Configuration parameters updating SUCCEEDED
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
mongod.service - MongoDB service
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-01-14 00:43:32 EST; 4min 32s ago
  Process: 39077 ExecStart=/bin/mongod -f /etc/mongodb/mongod.conf (code=exited, status=0/SUCCESS)
 Main PID: 39079 (mongod)
   CGroup: /system.slice/mongod.service
           39079 /bin/mongod -f /etc/mongodb/mongod.conf
Apr 09 22:42:09 localhost.localdomain systemd[1]: Starting MongoDB service...
Apr 09 22:42:09 localhost.localdomain mongod[39077]: about to fork child process, waiting until server is ready for connections.
Apr 09 22:42:09 localhost.localdomain mongod[39077]: forked process: 39079
Apr 09 22:42:12 localhost.localdomain mongod[39077]: child process started successfully, parent exiting
Apr 09 22:42:12 localhost.localdomain systemd[1]: Started MongoDB service.
MongoDB shell version v4.0.6
connecting to: mongodb://10.10.3.142:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("52d2a2c4-caaf-4dee-b33b-a8bc3904ce55") }
MongoDB server version: 4.0.6
bye
Successfully connected MongoDB node 10.10.3.142:27017
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a62c8af5-abd7-4287-8db3-d7a2c6a7eaca") }
MongoDB server version: 4.0.6
{ "ok" : 1 }
Successfully initialized replica set 10.10.3.142:27017 of MongoDB service mongod.
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bc8966d1-a43d-4526-b294-bc3f6837eb11") }
MongoDB server version: 4.0.6
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-256"
        ]
}
Successfully added user: "admin", password: "******" for MongoDB service: mongod
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?authMechanism=SCRAM-SHA-256&authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("dd2ecad0-cfac-457d-88bd-667d381442d7") }
MongoDB server version: 4.0.6
bye
Successfully logged in MongoDB with username: "admin", password: "******"
INFO: Backing up uninstall.sh SUCCEEDED
INFO: Successfully installed MongoDB
...

10. After MongoDB is successfully installed, run the reboot command to restart the machine.

11. After the machine starts, run the ps -ef|grep mongo or systemctl status mongod command to verify whether its service starts successfully.

[[email protected] ~]# 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

[[email protected] ~]# systemctl status mongod
  mongod.service - MongoDB service
  Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-01-14 00:43:32 EST; 4min 32s ago
 Process: 6136 ExecStart=/bin/mongod -f /etc/mongodb/mongod.conf (code=exited, status=0/SUCCESS)
 Main PID: 6375 (mongod)
  Memory: 902.3M (limit: 8.0G)
...

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 /etc/logrotate.d directory.

Tip: You do not need to initialize NetBrain data in the MongoDB because the initialization will be automatically performed when you install the Web API Server or Worker Server later.

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

Parameters

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

Parameter

Default Value

Description

DataPath

/usr/lib/mongodb

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/mongodb

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: Don't use 0.0.0.0 if you want to deploy a replica set.

FQDN

127.0.0.1

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

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

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

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

admin

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

192.168.1.1:27017

Enter the actual IP address to be found or FQDN, and the port number of MongoDB service.