MongoDB is a free, open-source, and cross-platform document-oriented database system that uses JSON-like documents with optional schemas. It is written in C++ and used for developing modern dynamic apps. In MongoDB, data objects are stored as separate documents in a collection, unlike in traditional relational databases where rows and columns are used. It provides an aggregation framework and offers easy querying and indexing functionality that helps developers to query complex document-based data sets easily.
In this post, we will explain how to install and use MongoDB on Rocky Linux 8.
Add MongoDB Repository
By default, the MongoDB package is not included in the Rocky Linux 8 default repo, so you will need to add the MongoDB official repo to your system.
You can create it using the following command:
nano /etc/yum.repos.d/mongodb-org-4.4.repo
Add the following lines:
[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.asc
Save and close the file when you are finished.
Also Read
How to Install MySQL 8.0 on Rocky Linux 8
Install MongoDB on Rocky Linux 8
You can now install the MongoDB package by just running the following command:
dnf install mongodb-org -y
Once MongoDB is installed, start and enable the MongoDB service using the following command:
systemctl start mongod systemctl enable mongod
You can also verify the MongoDB version using the following command:
mongod --version
You will get the following output:
db version v4.4.13 Build Info: { "version": "4.4.13", "gitVersion": "df25c71b8674a78e17468f48bcda5285decb9246", "openSSLVersion": "OpenSSL 1.1.1k FIPS 25 Mar 2021", "modules": [], "allocator": "tcmalloc", "environment": { "distmod": "rhel80", "distarch": "x86_64", "target_arch": "x86_64" } }
Configure MongoDB
By default, MongoDB is configured to connect without any authentication. For security purposes, it is a good idea to secure MongoDB with a password.
First, edit the MongoDB configuration file and enable authentication:
nano /etc/mongod.conf
Add the following lines:
security: authorization: enabled
Save and close the file when you are finished.
Create an Admin User for MongoDB
Next, you will need to create an admin user to manage the MongoDB databases.
First, connect to the MongoDB instance with the following command:
mongo
Once you are connected, create a database named admin using the following command:
use admin
Next, create an admin user and set a password with the following command:
db.createUser( { user: "mongoadmin", pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } )
You will be asked to set a password as shown below:
Enter password: Successfully added user: { "user" : "mongoadmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, "readWriteAnyDatabase" ] }
Next, exit from the MongoDB shell with the following command:
exit
Finally, restart the MongoDB service to apply the changes:
systemctl restart mongod
You can now connect to the MongoDB instance using the administrator credentials:
mongo --port 27017 --authenticationDatabase "admin" -u "mongoadmin" -p
Create a Database in MongoDB
To create a database named testdb, run the following command:
use testdb
Next, add some data to the testdb database using the following command:
db.linux.insertOne( { "Debian" : "11", "Rocky Linux" : "8", "Alma Linux" : "8" } )
You can now list available databases using the following command:
db
You will get the following output:
testdb
To show documents in your database, run the following command:
show collections
You will get the following output:
linux
To show the contents of your database collection, run the following command:
db.linux.find()
You will get the following output:
{ "_id" : ObjectId("6245b6be2dff2ecebfbe59e0"), "Debian" : "11", "Rocky Linux" : "8", "Alma Linux" : "8" }
To switch the database to admin, use the following command:
use admin
To list all users, run the following command:
db.getUsers()
You will get a list of all users in the following output:
[ { "_id" : "admin.mongoadmin", "userId" : UUID("2b632052-c1ca-4a26-bc1c-e883f24fc7f0"), "user" : "mongoadmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "readWriteAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } ]
Also Read
How to Delete or Remove Databases in MySQL
Conclusion
In this post, we explained how to install the MongoDB database on Rocky Linux 8. We also explained how to secure the MongoDB instance and interact with a MongoDB database. Try managing MongoDB databases on dedicated hosting from Atlantic.Net!