MongoDB is a free and open-source document-oriented database. Unlike relational databases, which store data in tables and require predefined schemas before adding new data, documents in MongoDB are unordered collections of key/value pairs with dynamic schemas (i.e., no fixed schema). As such, the same field can hold an integer, a string, or an object — it just depends on what you need for your application. Moreover, due to its dynamic nature, MongoDB works very well for storing JSON objects, making it great as a RESTful database.
While MongoDB supports secondary indexes, they are not required for every scenario. This means that reads may be slightly slower with MongoDB than some relational databases (because you need to read more fields from disk), but writes are generally faster because there is no index maintenance involved.
MongoDB is useful for any Linux user who needs to store and manage data (relational and unstructured), especially when you need scalability or high availability. The main reason that MongoDB is growing in popularity is that it provides the ability to store JSON documents, making it a great fit for hybrid Web applications that are used by both mobile clients (e.g., iPhone) and traditional desktop browsers (e.g., Firefox). Because these applications serve multiple client types with different data storage requirements, MongoDB can be an ideal fit because its schema-less design supports the dynamic nature of the data in these modern Web apps.
Another area where MongoDB shines is social media sites like Twitter, Facebook, and LinkedIn. These sites are increasingly providing REST APIs to store your social connections (e.g., friends, likes) as JSON documents, making them a great fit for MongoDB as well.
MongoDB is also used by cloud computing providers such as Amazon’s EC2 and Heroku because its scalability and high availability match their needs to be able to handle massive data that can change on the fly.
In this guide, we will show you how to set up your own MongoDB database on a Rocky Linux 8 operating system. After installing the software, we will then create a database and do some basic administration tasks.
Prerequisite
In order to follow this guide, you should have a Rocky Linux 8 running and should be logged in as a non-root user with sudo privileges.
Updating the System
Before installing any software, you should run the following command to make sure that all of your system packages are up-to-date:
sudo dnf -y update
Reboot the server in order for the system changes to take effect, and then log in again with the same non-root user.
Adding the MongoDB Repository
The MongoDB repository must first be added before installing MongoDB on your system. You have two options: you can download and install the program’s CE version, which has limited capabilities but is free for personal use; or purchase commercial access to more powerful features.
To install the MongoDB repository on Rocky Linux 8, run the following command.
sudo tee /etc/yum.repos.d/mongodb-org-4.4.repo<<EOF [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 EOF
Installing MongoDB on Rocky Linux 8
Now that the MongoDB repository is set up, you can install MongoDB 5.0 by running the following command.
sudo dnf install mongodb-org
Once the installation is completed, start and enable the MongoDB service for automatic booting when the system reboots.
sudo systemctl start mongod sudo systemctl enable mongod
To check the version of MongoDB, run the following command:
mongo --version
To check if the MongoDB service is running, run the following command:
sudo systemctl status mongod
You should see an output that looks like the following.
To check if MongoDB has run the proper installation, run the following command. The MongoDB daemon should be ready and waiting for connections, as shown by a line in the output.
Testing the MongoDB Installation
After the installation process has been completed, you may now start using MongoDB. By default, MongoDB listens on port 27017 at your local host’s IP address. As such, all commands are expected to be run on the localhost.
For demonstration purposes, we will show you how to connect to the MongoDB shell and insert records (i.e., documents) into a test collection (i.e., table). Of course, before doing so you must make sure that the MongoDB daemon is running on your server for testing purposes.
You can log into the MongoDB shell with the following command.
mongo
This command will log you into the MongoDB shell with no authentication set up.
In the MongoDB shell, you can run any database management commands. MongoDB provides a sample database called test, which is a good place to start. To get a list of all databases available on the server, run the following command at the MongoDB shell.
db
To create a new database in MongoDB, run the following command.
use DATABASE_NAME
Where DATABASE_NAME is the name of the database you want to create. If the database with that name already exists, this command switches to the current database. If the database does not exist, it will be created automatically. If the command is successful, it will return a message “Switched to db DATABASE_NAME.”
Let’s create a database called linux_mongo
use linux_mongo
Once you have created the database, you can insert records into the database(collection) by running the following command.
db.linux.insertOne( { "ubuntu" : "20.04", "rocky linux" : "8", "debian" : "11", "alma linux" : "8", } )
Press Enter to insert the data.
To list database collections, simply type the show collections command in the MongoDB shell.
show collections
The output will be similar to this.
To show all collections content or data from a collection, use:
db.NAME-OF-COLLECTION.find().pretty()
The pretty() method in the Mongo universe will pretty-print the results, especially good for humans to read. Let’s show all data from the linux collection we created above.
db.linux.find().pretty()
The output will be similar to this.
To exit the MongoDB shell, type the following command at the prompt.
exit
Conclusion
In this tutorial, we have shown you how to install and configure MongoDB on a Rocky Linux system. We have also shown you a few basic commands to show you how to connect to the MongoDB shell, showing all databases and collections that exist within your instance of MongoDB.
This guide is just basic installation guidance. For further information, please refer to MongoDB documentation that is available here.