MongoDB, also known as Mongo, is an open-source document database used in many modern web applications. It's classified as a NoSQL database because it doesn't rely on a traditional table-based relational database structure and much different from the traditional databases like PostgreSQL and MySQL.
Data stored in MongoDB in a flexible form, like JSON documents. MongoDB doesn't require any predefined data structure or table schema.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related MongoDB queries.
In this context, we shall look into how to install and configure the MongoDB database application on CentOS 8.
Before proceeding with this installing procedure, ensure that All commands should be executed under the root user.
The MongoDB packages are not present in the default CentOS 8 repository.
You need to enable the MongoDB official repository on your system to install related packages.
While we are explaining the details of this article, the MongoDB 4.2.12 version is available as the latest version.
Therefore, you can also search for the latest MongoDB version before starting the installation.
Open the 'Terminal' window to install the MongoDB databases.
So, click on the terminal icon from the left sidebar application menu.
The following number of steps you need to perform as the root user to install and configure the MongoDB in the CentOS 8 system.
i. Create a new repository file inside the /etc/yum.repos.d/ directory with the name MongoDB-org.repo and enable the MongoDB repository:
$ sudo nano /etc/yum.repos.d/mongodb-org.repo
ii. Now, paste the following code in this file:
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
To install any other version of MongoDB, just replace the 4.2 instances with your version.
Install the following meta package by using the below-given command:
$ sudo dnf install mongodb-org
When the above command is executed, it installs the various other packages with MongoDB such as mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools.
Press 'y' to continue the installation of these packages and then hit 'Enter'.
Again, press 'y' to successfully importing the MongoDB GPG key on your system.
Once the installation of MongoDB is completed, start the MongoDB services by running the below-mentioned command and then display the current service status:
$ sudo systemctl enable mongod --now
$ sudo systemctl status mongod
To know the installed version, connect to the MongoDB server by executing the following command:
$ mongo
Then, run the following command on MongoDB shell:
$ db.version()
MongoDB installed version will be displayed on the shell.
Usually, the default configuration is sufficient.
But, in most cases, when you are working in a production environment, you need to change the following configuration in the /etc/mongod.conf file as follows:
$ sudo nano /etc/mongod.conf
Find the security section and uncomment this section. Enable the authorization option for role-based user access:
security:
authorization: enabled
After changing the configuration, type the following command to restart the MongoDB service:
$ sudo systemctl restart mongod
If you have been enabled the authorization option, access the MongoDB shell prompt as follows:
$ mongo
Now, connect with the admin database by using the MongoDB shell as follows:
> use admin
Now, create an admin user with a suitable name and set the following role:
db.createUser(
> {
user: "mongoAdmin",
pwd: "mpaswrd",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Use the following command to exit from the shell:
> quit()
You can access the MongoDB shell as an admin user which you have created above:
$ mongo -u mongoAdmin -p --authenticationDatabase admin
Enter the password and switch to the admin database as follows:
> use admin
Now, show all user through the following command:
> show users
This article covers MongoDB installation on CentOS 8 system.
Instead, it uses JSON-like documents with dynamic schemas, meaning that, unlike relational databases, MongoDB does not require a predefined schema before you add data to a database.
You can alter the schema at any time and as often as is necessary without having to set up a new database with an updated schema.
How to Start the MongoDB Service and Test the Database on Ubuntu?
1. Run the following systemctl command to start the MongoDB service:
# sudo systemctl start mongod
2. Then check the service's status:
# sudo systemctl status mongod
This command will return output like the following, indicating that the service is up and running
3. After confirming that the service is running as expected, enable the MongoDB service to start up at boot:
# sudo systemctl enable mongod
How to Managing the MongoDB Service on Ubuntu?
1. The systemctl status command checks the status of the MongoDB service:
# sudo systemctl status mongod
2. You can stop the service anytime by typing:
# sudo systemctl stop mongod
3. To start the service when it’s stopped, run:
# sudo systemctl start mongod
4. You can also restart the server when it’s already running:
# sudo systemctl restart mongod
5. If you ever wish to disable this automatic startup, type:
# sudo systemctl disable mongod
6. Then to re-enable it to start up at boot, run the enable command again:
# sudo systemctl enable mongod