MongoDB is a free, open-source, document-oriented database engine that provides access to non-relational databases. MongoDB stores data in JSON-like documents along with a dynamic schema, offering better performance than other databases. MongoDB is a NoSQL database, which means it does not support SQL to manipulate the stored data. MongoDB offers a wide range of features such as ad hoc queries, indexing, replication, load balancing, file storage, aggregation, transactions, and more. MongoDB is one of the most popular database engines for all systems and has been used in a number of large-scale production environments.
This tutorial will show you how to install and use MongoDB on a CentOS 8 server.
Prerequisites
- A server running CentOS 8.
- A root password set up on your server.
Install MongoDB
By default, MongoDB is not available in the CentOS 8 default repository. Therefore, you need to add the MongoDB repository to your system. You can add it by creating the mongodb.repo file in the /etc/yum.repos.d/ directory:
nano /etc/yum.repos.d/mongodb.repo
Add the following lines:
[mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
Save and close the file. Then install MongoDB with the following command:
dnf install mongodb-org
After you have installed MongoDB, start the MongoDB service and enable it so that it starts after a system reboot:
systemctl start mongod systemctl enable mongod
Now you can check the status of MongoDB with the following command:
systemctl status mongod
You should get the following output:
systemctl status mongod
Output
? mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-10-28 03:59:12 EDT; 5min ago Docs: https://docs.mongodb.org/manual Process: 737 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS) Process: 735 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 732 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 726 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS) Main PID: 914 (mongod) Memory: 216.1M CGroup: /system.slice/mongod.service ??914 /usr/bin/mongod --auth -f /etc/mongod.conf Oct 28 03:58:14 centos8 systemd[1]: Starting MongoDB Database Server... Oct 28 03:58:28 centos8 mongod[737]: about to fork child process, waiting until server is ready for connections. Oct 28 03:58:28 centos8 mongod[737]: forked process: 914 Oct 28 03:59:12 centos8 mongod[737]: child process started successfully, parent exiting Oct 28 03:59:12 centos8 systemd[1]: Started MongoDB Database Server.
Next, you can access the MongoDB shell with the following command:
mongo
You should get the following output:
MongoDB shell version v4.2.1-rc0-5-g87a606d connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("e8e052c8-7c47-4908-9a29-d7825bac037a") } MongoDB server version: 4.2.1-rc0-5-g87a606d Server has startup warnings: 2019-10-28T04:07:55.106-0400 I CONTROL [initandlisten] 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() ---
Create a MongoDB admin user
Next, create an admin user for MongoDB with administrative privileges. First, open the MongoDB shell with the following command:
mongo
Next, switch the database to admin as shown below:
>use admin
Next, create a new MongoDB user with the following command:
> db.createUser( { user: "mongodadmin", pwd: "password123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
You should get the following output:
Successfully added user: { "user" : "mongodadmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
Next, you’ll specify the admin user with the following command:
>show users
You should see the following output:
{ "_id" : "admin.mongodadmin", "userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"), "user" : "mongodadmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
Configure MongoDB authentication.
By default, MongoDB allows all users to access the MongoDB shell and execute any commands. Therefore, it is recommended to configure MongoDB authentication to prevent other users from running commands without sufficient permissions.
First, you must enable MongoDB authentication by editing the /lib/system/system/mongod.service file.
nano /lib/systemd/system/mongod.service
Change the following line:
Environment="OPTIONS= --auth -f /etc/mongod.conf"
Save and close the file. Then reload the systemd daemon and restart the MongoDB service with the following command:
systemctl --system daemon-reload systemctl restart mongod
Now log into the MongoDB shell and try to see the users without authentication:
mongo > use admin > show users
You should see the following error message:
2019-10-28T04:13:15.346-0400 E QUERY [js] uncaught exception: Error: command usersInfo requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.getUsers@src/mongo/shell/db.js:1638:15 shellHelper.show@src/mongo/shell/utils.js:883:9 shellHelper@src/mongo/shell/utils.js:790:15 @(shellhelp2):1:1
Now run the following command to authenticate MongoDB:
> db.auth('mongodadmin', 'password123') > show users
In the following output, you should see the admin user with its roles:
{ "_id" : "admin.mongodadmin", "userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"), "user" : "mongodadmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
Conclusion
The tutorial above taught us how to install MongoDB and configure MongoDB user authentication. I hope you now have enough knowledge to create your own database and develop an application using MongoDB. If you still have any questions, feel free to contact me.