MongoDB is a NoSQL database intended for storing large amounts of data in document-oriented storage with dynamic schemas. NoSQL refers to a database with a data model other than the tabular format used in relational databases such as MySQL, PostgreSQL, and Microsoft SQL. MongoDB features include: full index support, replication, high availability, and auto-sharding.
Preflight Check
- A Liquid Web Core Managed CentOS 7 node.
- We are logged in as the root user.
Step #1: Add the MongoDB Repository
First, we will use the vim text editor to create a new repo file for MongoDB.
root@host:~# vim /etc/yum.repos.d/mongodb.repoNext, add the following information to the file you’ve created, using i to insert:
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.ascThen save and exit vim using the command :wq.
Step #2: Install MongoDB
As a matter of best practice we will update our packages.
root@host:~# yum -y updateAt this point, installing MongoDB is as simple as running this command.
root@host:~# yum install -y mongodb-orgStep #3: Configure MongoDB
Prerequisites
Many Linux OSes confine the amount of system resources which a process can use. These limits sometimes can negatively effect the operation of MongoDB, and should be adjusted accordingly. The ulimit setting for CentOS 7 can be modified using the following command.
root@host:~# ulimit -n <value>Additionally, CentOS 7 enforces a distinct max process limitation using, nproc, which overrides the ulimit settings. This nproc value is defined in this configuration file
/etc/security/limits.d/20-nproc.confTo configure a nproc value, use vim to create a file called 99-mongodb-nproc.conf.
root@host:~# touch /etc/security/limits.d/99-mongodb-nproc.confNow, add the new soft nproc and hard nproc values to increase the process limit.
-f (file size): unlimited
-t (cpu time): unlimited
-v (virtual memory): unlimited [1]
-l (locked-in-memory size): unlimited
-n (open files): 64000
-m (memory size): unlimited [1] [2]
-u (processes/threads): 64000Step #4: Start MongoDB
In MongoDB 4.4 and above, a startup error is seen if the ulimit value for number of open files is under 64000.
To run and manage the mongod process, we will use our OSes default init system. Newer versions of Linux typically use systemd (systemctl), and older versions use System V init (service). To determine which init system the platform uses, run the following command.
root@host:~# ps --no-headers -o comm 1Init Systems
1. Start the mongod process.
root@host:~# systemctl start mongod
If you receive an error like:
Failed to start mongod.service: Unit mongod.service not found.
Run the following command.
root@host:~# systemctl daemon-reload
Then, run the start command above again.
2. Verify MongoDB has started.
To verify the mongod process has started successfully, issue the following command.
root@host:~# systemctl status mongod
To ensure MongoDB starts after a system reboot, issuing the following command.
root@host:~# systemctl enable mongod
Step #5: Check MongoDB Status & Info
Check MongoDB Service Status
systemctl status mongodSummary List of Status Statistics (Continuous)
mongostatSummary List of Status Statistics (5 Rows, Summarized Every 2 Seconds)
mongostat --rowcount 5 2Enter the MongoDB Command Line
mongoBy default, running this command will look for a MongoDB server listening on port 27017 on the localhost interface.
If you’d like to connect to a MongoDB server running on a different port, then use the –port option. For example, if you wanted to connect to a local MongoDB server listening on port 22222, then you’d issue the following command:
mongo --port 22222Shutdown MongoDB
systemctl stop mongodConclusion
MongoDB is an excellent NoSQL solution for many types of projects. It is resilient, robust, and durable document database that is designed for ease of development and scaling.
Our talented Support Teams are full of experienced technicians and administrators who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article. We are always available to assist with any issues related to this article, 24 hours a day, 7 days a week 365 days a year.
If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via chat or support ticket to assisting you with this process.
David Singer