MongoDB is an open-source, cross-platform, and distributed NoSQL (Non-SQL or Non-Relational) database system. Unlike traditional SQL databases that store data in tables, MongoDB uses flexible, JSON-like documents. MongoDB stores data in BSON format, which is a binary representation of JSON documents.
MongoDB is a distributed NoSQL database with built-in high availability, automatic failover, data redundancy, and horizontal scaling through sharding across distributed clusters. It supports multi-region geographic deployment and provides a query API that covers CRUD operations, data aggregation, text search, and geospatial queries.
Many leading companies, including Forbes, Toyota, SEGA, EA, Vodafone, and Verizon, leverage MongoDB’s capabilities.
This tutorial will guide you through installing MongoDB on a Rocky Linux 9 server, optimize the system for MongoDB deployment, and acquaint you with basic MongoDB queries. By the end of this guide, you’ll have MongoDB installed, and you’ll understand basic operations like user management, database creation, data insertion, retrieval, updates, and deletion.
Prerequisites
Ensure the following before proceeding:
- A Rocky Linux 9 server setup with the hostname ‘mongodb-rocky‘.
- A non-root user with sudo/root privileges.
Adding MongoDB Repository
Since MongoDB is not available through Rocky Linux’s default repositories, add the official MongoDB repository. The latest MongoDB version at the time of writing is v6.0.
Create a new repository file ‘/etc/yum.repos.d/mongodb-org-6.0.repo‘ with Nano using the command below:
sudo nano /etc/yum.repos.d/mongodb-org-6.0.repo
Add the following repository details:
[mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
Save and close the file.
Verify the repositories on your system with the command:
sudo dnf repolist
Installing MongoDB Server and Mongosh
Install the MongoDB server and client on your Rocky Linux server. The ‘mongodb-org’ package contains the server components, while ‘mongodb-mongosh’ is the command-line client.
sudo dnf install mongodb-org mongodb-mongosh
Confirm your installation by accepting the GPG key.
Start the MongoDB service and ensure it runs automatically on startup:
sudo systemctl start mongod sudo systemctl status mongod
Setting up System
Optimize your Rocky Linux server for MongoDB deployment by disabling Transparent Huge Pages (THP), increasing the ulimit, and setting max virtual memory.
Disable THP permanently using systemd by creating the following service:
sudo nano /etc/systemd/system/disable-thp.service
[Unit] Description=Disable Transparent Huge Pages (THP) [Service] Type=simple ExecStart=/bin/sh -c "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled && echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag" [Install] WantedBy=multi-user.target
Reload systemd and enable the service:
sudo systemctl daemon-reload sudo systemctl enable disable-thp sudo systemctl start disable-thp
Adjust the ulimit by creating a configuration file:
sudo nano /etc/security/limits.d/mongodb.conf
mongod soft nproc 64000 mongod hard nproc 64000 mongod soft nofile 64000 mongod hard nofile 64000
Modify the ‘/etc/sysctl.conf‘ file to set max virtual memory:
sudo nano /etc/sysctl.conf
fs.file-max = 2097152 vm.max_map_count = 262144 vm.swappiness = 1
Apply the changes by rebooting the system:
sudo reboot
Setting up Admin for MongoDB
Secure your MongoDB installation by creating an admin user and enabling authentication. Log into MongoDB:
mongosh
Disable free monitoring:
db.disableFreeMonitoring()
Create an admin user in the ‘admin’ database:
use admin db.createUser( { user: "myAliceAdmin", pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "readWriteAnyDatabase", db: "admin" } ] } )
Modify the ‘/etc/mongod.conf‘ file to enable authorization:
sudo nano /etc/mongod.conf
security: authorization: enabled
Restart MongoDB to apply changes:
sudo systemctl restart mongod
Creating User and Database on MongoDB
Create a new MongoDB user and verify users on the server:
use testdb db.createUser( { user: "myTestUser", pwd: passwordPrompt(), roles: [ { role: "readWrite", db: "testdb" }, { role: "read", db: "reporting" } ] } )
Check users in the ‘admin’ database:
use admin db.system.users.find()
Inserting and Querying Data in MongoDB
Create collections and insert data. Use query operators for data retrieval:
use testdb db.movies.insertOne( { title: "The Hobbit", genres: [ "Adventure", "Fantasy" ], runtime: 172, rated: "R", year: 2012, directors: [ "Peter Jackson" ], cast: [ "Martin Freeman", "Ian McKellen", "Richard Armitage" ], type: "movie" } )
db.movies.find({})
Insert multiple documents and query with conditions:
db.movies.insertMany([ { title: "The Lord of the Rings", ... }, { title: "Harry Potter", ... } ])
db.movies.find({ directors: "Peter Jackson" }) db.movies.find({ genres: { $in: ["Action", "Family"] } })
Updating Data in MongoDB
Update and replace data in MongoDB:
db.movies.updateOne({ title: "Transformers" }, { $set: { rated: "R" }})
db.movies.replaceOne( { title: "Transformers" }, { title: "Transformers: Dark of the Moon", ... } )
Deleting Data in MongoDB
Remove documents, users, and databases:
db.movies.deleteMany({ title: "Transformers: Dark of the Moon" })
use testdb db.dropDatabase()
use admin db.system.users.find()
Conclusion
You’ve successfully installed MongoDB Community Edition on Rocky Linux 9 and learned to enable authentication. The guide also covered basic MongoDB commands and optimization tips. For advanced features like sharding, refer to the MongoDB documentation.
FAQ
- Q: What is MongoDB?
A: MongoDB is a NoSQL database program that uses JSON-like documents with optional schemas. - Q: What are the advantages of using MongoDB?
A: MongoDB provides high availability, automatic failover, scalability, and supports geographic deployment. - Q: Can I use MongoDB for large-scale enterprise applications?
A: Yes, MongoDB is designed to scale horizontally and can handle large volumes of data across distributed systems. - Q: How does MongoDB store data?
A: MongoDB stores data in BSON format, which is a binary representation of JSON documents. - Q: Where can I find more information on MongoDB queries?
A: MongoDB’s official documentation provides detailed information on various query types and database operations.