As a database administrator and technology enthusiast, I‘m excited to walk you through installing the latest version of MongoDB on RHEL/CentOS 8. MongoDB is one of the most popular open source NoSQL databases, known for its flexibility, scalability, and high performance. Let‘s get started!
Why MongoDB?
Before we dive into the installation, I wanted to share some perspective on why MongoDB is a great database choice.
-
Document model – MongoDB stores data in flexible JSON-like documents, making it easy to represent complex data structures. This is less restrictive than the tabular approach of SQL databases.
-
High scalability – MongoDB is designed for horizontal scaling across many servers. It automatically shards data and balances load, making it easy to scale capacity.
-
Rich queries – The MongoDB query language supports advanced queries like aggregations, geospatial queries, and text search. This makes it more powerful than many NoSQL databases.
-
High performance – MongoDB is optimized for high throughput and low latency. Features like in-memory caching, indexing, and native replication provide great performance.
-
Wide language support – MongoDB has official drivers for most major languages and frameworks like Node, Python, Java, and .NET. This makes application development very accessible.
Now that you know some of MongoDB‘s key strengths, let‘s get it up and running on CentOS 8!
Preparing CentOS 8
The first step is making sure your CentOS 8 system is ready. Log in as root or a user with sudo privileges and update the system:
sudo yum update -y
This applies the latest security patches and fixes. Always keep your system fully updated!
Adding the MongoDB Repository
Now we need to add the official MongoDB repository to CentOS. This gives us access to the newest versions of MongoDB directly from the vendor.
Create a repo file at /etc/yum.repos.d/mongodb-org-4.2.repo
with these contents:
[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
This repo definition will install MongoDB 4.2.x, the latest stable series. MongoDB supports multiple major versions being installed side-by-side, so you could also add repos for older releases like 4.0 or 3.6 if needed.
Installing MongoDB Packages
With the repository configured, installing MongoDB is a breeze with yum:
sudo yum install -y mongodb-org
This downloads all the necessary MongoDB packages, including:
- mongodb-org – The meta package for MongoDB
- mongodb-org-server – The mongod database server daemon
- mongodb-org-mongos – The mongos sharding service daemon
- mongodb-org-shell – The mongosh interactive shell
- mongodb-org-tools – Utilities like mongodump and mongorestore
In total, it‘s over 100MB of packages. The full installation takes 1-2 minutes on a typical broadband connection.
Once complete, verify that MongoDB installed successfully:
mongo --version
On my CentOS box, this prints MongoDB shell version v4.2.2
, so we know it worked!
Configuring and Running MongoDB
The main configuration file for mongod is located at /etc/mongod.conf
. The defaults are a good starting point, but here are some common tweaks:
- Binding to 127.0.0.1 prevents remote connections for extra security in a testing environment.
- Enabling access control prevents unauthorized users from accessing data.
- Setting a custom dbPath changes the on-disk storage location.
For example:
net:
bindIp: 127.0.0.1
security:
authorization: enabled
storage:
dbPath: /custom/storage/path
Check out the docs for more details on configuration options.
After updating the config file, restart mongod to load the changes:
sudo systemctl restart mongod
To check that the MongoDB server is running:
sudo systemctl status mongod
This completes the installation! MongoDB is ready for use on your CentOS 8 system.
Creating Users and Databases
Now you can connect with the mongo shell and get started using MongoDB:
mongo
> use myNewDatabase
> db.createUser({
user: "myUser",
pwd: passwordPrompt(),
roles: [ "readWrite" ]
})
This connects to MongoDB, switches to a new database, and creates a user with permissions.
Refer to the authentication docs for more on setting up users and roles. The shell reference has examples of CRUD operations and other commands.
Final Thoughts
We‘ve covered the basics of installing MongoDB, configuring it for security and performance, and managing the database service. Some next steps might include:
- Setting up replication and sharding for fault tolerance and scalability
- Automating backups for disaster recovery
- Monitoring key metrics and logs for visibility
- Integrating MongoDB with applications like Node, Python, Java, and more!
I hope this guide has demystified setting up MongoDB on CentOS 8. Let me know if you have any other questions! I‘m always happy to chat databases and help fellow technologists master new skills.
Happy coding!