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 10.
Add MongoDB Repository
By default, the MongoDB package is not included in the Rocky Linux 10 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-7.0.repo
Add the following lines:
[mongodb-org-7.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/7.0/x86_64/ enabled=1 gpgcheck=0 repo_gpgcheck=0
Save and close the file when you are finished.
Install MongoDB on Rocky Linux 10
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 v7.0.25 Build Info: { "version": "7.0.25", "gitVersion": "96dce3da49b8d2e9e0d328048cb56930eb1bdb2b", "openSSLVersion": "OpenSSL 3.2.2 4 Jun 2024", "modules": [], "allocator": "tcmalloc", "environment": { "distmod": "rhel90", "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:
mongosh
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: ******{ ok: 1 }
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:
mongosh --port 27017 --authenticationDatabase "admin" -u "mongoadmin" -p
If you get any error, then remove the socket file and restart MongoD service.
rm -f /tmp/mongodb-*.sock systemctl restart mongod
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" : "10", "Alma Linux" : "9" } )
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" : "10", "Alma Linux" : "9" }
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 10. 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!