Installing and Configuring MongoDB on CentOS 7
Installing and Configuring MongoDB
MongoDB is a NoSQL database that provides high performance, high availability, and automatic scaling. NoSQL database means that, unlike MySQL or PostgreSQL, it does not support SQL (Structured Query Language) to retrieve or manipulate the stored data.
MongoDB does not store data in tables, instead, it stores data in a “document” structure similar to JSON (in MongoDB called BSON). MongoDB was first introduced in 2009, six years ago. Currently developed by the company MongoDB MongoDB Inc.
In this tutorial, we will guide you to install and configure MongoDB on a CentOS 7.3 server. We will add and configure the administrator user for MongoDB and configure the authentication for MongoDB services.
Prerequisites
- CentOS 7.3 (Operating system used here)
- root privileges.
Step 1: Adding MongoDB Repository in CentOS
After logged into your server’s ssh as root user, go to the ‘yum.repos.d’ directory in order to add the new CentOS repository. Go to that directory and create new repository file ‘mongodb-org-3.2.repo’
[root@webhostingchennai ~]# cd /etc/yum.repos.d/ [root@webhostingchennai ~]# nano mongodb-org-3.2.repo
Paste MongoDB repository configuration as below
[mongodb-org-3.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
And now save the file and exit.
Step 2 : Installing MongoDB
Now check the repository list on the server to make sure that the MongoDB repository is available in the list.
Run the below command
You should see results below. The MongoDB repository is on the list.
[root@webhostingchennai yum.repos.d]# yum repolist Loaded plugins: fastestmirror base | 3.6 kB 00:00:00 centosplus | 3.4 kB 00:00:11 epel/x86_64/metalink | 7.6 kB 00:00:00 epel | 4.7 kB 00:00:00 extras | 3.4 kB 00:00:00 mongodb-org-3.2 | 2.5 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/3): mongodb-org-3.2/7/primary_db | 82 kB 00:00:01 (2/3): epel/x86_64/updateinfo | 917 kB 00:00:01 (3/3): epel/x86_64/primary_db | 6.3 MB 00:00:14 Loading mirror speeds from cached hostfile * base: centos.excellmedia.net * centosplus: centos.excellmedia.net * epel: mirror.nes.co.id * extras: centos.excellmedia.net * updates: centos.excellmedia.net repo id repo name status base/7/x86_64 CentOS-7 - Base 9,566+25 centosplus/7/x86_64 CentOS-7 - Plus 71+21 epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,509 extras/7/x86_64 CentOS-7 - Extras 448 mongodb-org-3.2/7 MongoDB Repository 100 updates/7/x86_64 CentOS-7 - Updates 2,323+93 repolist: 25,017 [root@webhostingchennai yum.repos.d]#
Next, we need to install MongoDB with the yum command.
yum -y install mongodb-org
After completion of the installation, start MongoDB with this systemctl command
systemctl start mongod
Check that MongoDB is running by checking that the port ‘27017’ is open.
netstat -plntu
And make sure the mongodb service is active.
systemctl status mongod
[root@webhostingchennai yum.repos.d]# systemctl start mongod [root@webhostingchennai yum.repos.d]# netstat -plntu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 28670/mongod tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 21442/monitorix-htt tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 19700/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 745/master tcp6 0 0 :::22 :::* LISTEN 19700/sshd udp 0 0 103.14.121.94:123 0.0.0.0:* 19862/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 19862/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 19862/ntpd udp6 0 0 fe80::216:3eff:fed0:123 :::* 19862/ntpd udp6 0 0 ::1:123 :::* 19862/ntpd udp6 0 0 :::123 :::* 19862/ntpd [root@webhostingchennai yum.repos.d]# systemctl status mongod ● mongod.service - SYSV: Mongo is a scalable, document-oriented database. Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled) Active: active (running) since Wed 2018-04-25 13:54:15 EEST; 20s ago Docs: man:systemd-sysv-generator(8) Process: 28655 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS) CGroup: /system.slice/mongod.service └─28670 /usr/bin/mongod -f /etc/mongod.conf Apr 25 13:54:14 webhostingchennai.com systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database.... Apr 25 13:54:15 webhostingchennai.com runuser[28666]: pam_unix(runuser:session): session opened for user mongod ...d=0) Apr 25 13:54:15 webhostingchennai.com runuser[28666]: pam_unix(runuser:session): session closed for user mongod Apr 25 13:54:15 webhostingchennai.com mongod[28655]: Starting mongod: [ OK ] Apr 25 13:54:15 webhostingchennai.com systemd[1]: Started SYSV: Mongo is a scalable, document-oriented database.. Hint: Some lines were ellipsized, use -l to show in full. [root@webhostingchennai yum.repos.d]#
Step 3 : Fix a MongoDB Error
MongoDB is installed. Now we can access the mongodb shell by using the command below:
mongo
You will probably see this error about ulimit configuration on the server.
** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000…
[root@webhostingchennai yum.repos.d]# mongo MongoDB shell version: 3.2.19 connecting to: test 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 Server has startup warnings: 2018-04-25T13:54:15.753+0300 I CONTROL [initandlisten] 2018-04-25T13:54:15.753+0300 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files. >
Ulimits or User limits define how much of a system-wide resource a user may use.
In order to resolve this problem, we need to increase the ulimit configuration of the user mongod.
On CentOS 7 server, the MongoDB database is running under the user ‘mongod’.
Go to the /etc/security/ limits.conf configuration file.
cd /etc/security/ nano limits.conf
Increase the limits of the mongod user to ‘64000’ – number of processes or nproc and the number of open files or nofile to 64000.
Paste new configuration below to the end of the file:
mongod soft nproc 64000 mongod hard nproc 64000 mongod soft nofile 64000 mongod hard nofile 64000
Save the file and exit
Run the sysctl command below to apply the changed limits to the system:
sysctl -p
Now, restart the MongoDB service and try again to access the mongo shell, the error is gone now.
systemctl restart mongod mongo
[root@webhostingchennai security]# systemctl restart mongod [root@webhostingchennai security]# mongo MongoDB shell version: 3.2.19 connecting to: test >
Step 4 : Create a MongoDB Administrator User
In this step, we will create a new user “admin” for MongoDB with the role ‘UserAdminAnyDatabase’ from the mongo shell.
Open the mongodb shell using “mongo” command
Here, I’m creating a new user administrator named ‘admin’ with password ‘mongoadmin’. Use a secure password on your server. Then we will configure the role of the user to be ‘UserAdminAnyDatabase’.
Switch to the database ‘admin’.
use admin
Type in the MongoDB query below to create the new administrator user
db.createUser( { user: "admin", pwd: "mongoadmin", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
Next, ensure that the user has been created with the MongoDB query below.
show users
[root@webhostingchennai ]# mongo MongoDB shell version: 3.2.19 connecting to: test > use admin switched to db admin > db.createUser( ... { ... user: "admin", ... pwd: "admin123", ... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] ... } ... ) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > show users { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > ^C bye [root@webhostingchennai ]#
The MongoDB administrator user has been created.
Step 5 : Enable User Authentication in MongoDB
Here, we will enable authentication for users to prevent that another user without sufficient privileges is able to see the data on the database.
On our CentOS 7 server, MongoDB is running under systemd with an init script in the ‘/etc/init.d/’ dirctory. We will edit that script to force the mongodb service to run with the ‘–auth’ option.
Go to the ‘/etc/init.d/’ directory and edit the “mongod” file
cd /etc/init.d/ nano mongod
On line 15 you will find the “OPTION” variable, there we will add the “mongod” option.
OPTIONS=” –auth -f $CONFIGFILE”
Save the file.
Reload the systemd service and restart MongoDB.
systemctl daemon-reload systemctl restart mongod
Next, we have to test the configuration by logging into the mongo shell and switch to the admin database, then try to see the admin users.
mongo use admin show users
You will see an error about the unauthorized execution of the command in the database admin. Now we need to use the command ‘db.auth()’ for the authentication.
db.auth('admin', 'admin123')
Now you can see the users with their roles and the privileges.
[root@webhostingchennai init.d]# mongo MongoDB shell version: 3.2.19 connecting to: test > use admin switched to db admin > show users 2018-04-25T14:20:22.249+0300 E QUERY [thread1] Error: not authorized on admin to execute command { usersInfo: 1.0 } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.getUsers@src/mongo/shell/db.js:1523:1 shellHelper.show@src/mongo/shell/utils.js:764:9 shellHelper@src/mongo/shell/utils.js:671:15 @(shellhelp2):1:1 > db.auth('admin', 'mongoadmin') 1 > show users { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } >
Now, MongoDB 3.2 has been installed and configured on a CentOS 7 Server.
For installing LAMP on Centos 7, click here