What is MongoDB? MongoDB is a document database designed for ease of application development and scaling. This guide is all about ‘Install MongoDB 6.0 on Linux’ – Rocky Linux, Red Hat Enterprise Linux, CentOS Stream, Alma Linux or Oracle Linux using the dnf/yum package manager.
Supported RPM based Distros
- RHEL / CentOS / Oracle / Rocky / AlmaLinux 9 (Starting in MongoDB 6.0.4)
- RHEL / CentOS / Oracle / Rocky / AlmaLinux 8
- RHEL / CentOS / Oracle 7
Install MongoDB 6.0 on Rocky Linux, Red Hat Enterprise Linux, CentOS Stream, Alma Linux or Oracle Linux
Let’s follow the steps below to install MongoDB Community Edition
Prepare the MongoDB Repo
Create a /etc/yum.repos.d/mongodb-org-7.0.repo
repo file for the installation of MongoDB using using the package manager, dnf or yum. You can run the command below as one:
sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo<<EOF [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 EOF
Verify that the repo has been added:
[root@rocky9 ~]# cat /etc/yum.repos.d/mongodb-org-7.0.repo [mongodb-org-7.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/7.0/x86_64/repodata/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
Install MongoDB on Rocky Linux, Red Hat Enterprise Linux, CentOS Stream, Alma Linux or Oracle Linux
Since the repo is ready, you can now install the latest stable MongoDB 7.0.x using the following command:
$ sudo dnf install -y mongodb-org .... Total download size: 122 M Installed size: 501 M Downloading Packages: (1/10): mongodb-org-6.0.8-1.el9.x86_64.rpm 2.0 kB/s | 9.3 kB 00:04 (2/10): mongodb-database-tools-100.7.5.x86_64.rpm 5.2 MB/s | 27 MB 00:05 (3/10): mongodb-org-database-6.0.8-1.el9.x86_64.rpm 20 kB/s | 9.4 kB 00:00 (4/10): mongodb-org-database-tools-extra-6.0.8-1.el9.x86_64.rpm 47 kB/s | 14 kB 00:00 (5/10): mongodb-org-server-6.0.8-1.el9.x86_64.rpm 18 MB/s | 30 MB 00:01 (6/10): mongodb-org-tools-6.0.8-1.el9.x86_64.rpm ... Installed: cyrus-sasl-2.1.27-21.el9.x86_64 cyrus-sasl-plain-2.1.27-21.el9.x86_64 mongodb-database-tools-100.7.5-1.x86_64 mongodb-mongosh-1.10.3-1.el8.x86_64 mongodb-org-6.0.8-1.el9.x86_64 mongodb-org-database-6.0.8-1.el9.x86_64 mongodb-org-database-tools-extra-6.0.8-1.el9.x86_64 mongodb-org-mongos-6.0.8-1.el9.x86_64 mongodb-org-server-6.0.8-1.el9.x86_64 mongodb-org-tools-6.0.8-1.el9.x86_64 Complete!
Note: You can use yum for the lower versions of the Linux releases(version 7) .
As another option, you can install a specific release of MongoDB, specify each component package individually and append the version number to the package name:
dnf install mongodb-org-<VERSION> mongodb-org-database-<VERSION> mongodb-org-server-<VERSION> mongodb-mongosh-<VERSION> mongodb-org-mongos-<VERSION> mongodb-org-tools-<VERSION>
Run MongoDB
Prerequisites
-
Adjust the ulimit
Most Linux systems limit the system resources that processes use. ulimit
refers to the per-user limitations for various resources. This limitation could impact the performance of MongoDB hence needs to be adjusted. To check these limitations, use the command below:
# ulimit -a real-time non-blocking time (microseconds, -R) unlimited core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 30486 max locked memory (kbytes, -l) 8192 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 30486 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
Please refer to the recommended ulimit values here.
On you Linux System, you can change ulimit settings by issuing a command in the following form:
ulimit -n <value>
After changing the ulimit settings, you must restart the services to take advantage of the modified settings. For the Mongo, use the command below:
systemctl start mongod.service
-
Directory Paths
The default directories for MongoDB are:
/var/lib/mongo
– For Data/var/log/mongodb
– For Logs
mongod is the default MongoDB User.
If you intend to use custom Directories for Data and Logs, you need to edit /etc/mongod.conf to modify the following fields:
dbPath
e.g /datasystemLog.path
e.g /mongoDB_logs
Give the two paths the necessary permission to the mongd use by using the command below:
chown -R mongod:mongod /data chown -R mongod:mongod /mongoDB_logs
-
Start MongoDB
You can use systemd to enable and start MongoDB
sudo systemctl enable --now mongod
Lets verify whether the Mongo service started successfully:
# systemctl status mongod.service ● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: disabled) Active: active (running) since Tue 2023-08-08 20:47:13 EAT; 2min 44s ago Docs: https://docs.mongodb.org/manual Main PID: 119871 (mongod) Memory: 81.4M CPU: 2.355s CGroup: /system.slice/mongod.service └─119871 /usr/bin/mongod -f /etc/mongod.conf Aug 08 20:47:13 rocky9 systemd[1]: Started MongoDB Database Server. Aug 08 20:47:13 rocky9 mongod[119871]: {"t":{"$date":"2023-08-08T17:47:13.355Z"},"s":"I", "c":"CONTROL", "id":7484500, "ctx":"-","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK == 1, overriding \"proc> lines 1-12/12 (END)
Verify MongDB version
We can test the successful installtion and the running MongDB version.
# mongod --version db version v6.0.8 Build Info: { "version": "6.0.8", "gitVersion": "3d84c0dd4e5d99be0d69003652313e7eaf4cdd74", "openSSLVersion": "OpenSSL 3.0.7 1 Nov 2022", "modules": [], "allocator": "tcmalloc", "environment": { "distmod": "rhel90", "distarch": "x86_64", "target_arch": "x86_64" } }
-
Use MongoDB
We can access MongDB through the mongosh
.
# mongosh Current Mongosh Log ID: 64d299c19402adbd5b9afc42 Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.3 Using MongoDB: 6.0.8 Using Mongosh: 1.10.3 For mongosh info see: https://docs.mongodb.com/mongodb-shell/ To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy). You can opt-out by running the disableTelemetry() command. ------ The server generated these startup warnings when booting 2023-08-08T20:47:13.923+03:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2023-08-08T20:47:13.923+03:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2023-08-08T20:47:13.923+03:00: vm.max_map_count is too low ------ test>
By default, MongDB is configured not to use authentication. For hardening purposes, we need to enable authentication. Using your favorite text editor e.g vim, edit /etc/mongod.conf
and add the following:
security: authorization: "enabled"
For the changes to take effect, restart MongDB.
$ sudo systemctl restart mongod
5. Create an Admin user
We need to create an admin user to manage the Mongo Databases. Through mongosh
, connect to mongodb.
From the shell, create a database called admin using using the following command:
# mongosh Current Mongosh Log ID: 64d29d7aa1f860eb7b283fa8 Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.3 Using MongoDB: 6.0.8 Using Mongosh: 1.10.3 For mongosh info see: https://docs.mongodb.com/mongodb-shell/ test> use admin switched to db admin
The next step is to create the admin user and set the password using the following command:
db.createUser( { user: "mongoadmin", pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } )
You’ll be prompted to input your password through the standard input.
admin> db.createUser( { user: "mongoadmin", pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } ) Enter password Password01 **********{ ok: 1 }
Exit from the mongosh though the exit
command.
Lastly, restart mongdb to apply the changes.
systemctl restart mongod
You can now test the connection to MongoDB using the admin credentials that we have just created.
# mongosh --port 27017 --authenticationDatabase "admin" -u "mongoadmin" -p Enter password: ********** Current Mongosh Log ID: 64d2a069d94aba53bd328ebd Connecting to: mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+1.10.3 Using MongoDB: 6.0.8 Using Mongosh: 1.10.3 For mongosh info see: https://docs.mongodb.com/mongodb-shell/
Conclusion
Congratulations! You have successfully installed MongoDB on your Rocky/RHEL/Alma/CentOS system.
hello there and thank you for your information – I have certainly picked up anything new from right here.
I did however expertise a few technical points using this site, since I experienced to reload the site many times previous to I could get it to
load properly. I had been wondering if your web host is OK?
Not that I am complaining, but slow loading instances times will often affect your placement in google and
can damage your high quality score if ads and marketing with Adwords.
Well I’m adding this RSS to my email and could look out for much more
of your respective intriguing content. Make sure you
update this again soon.
Thank you.
Heya this is kinda of off topic but I was wondering if blogs use WYSIWYG editors
or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I
wanted to get advice from someone with experience.
Any help would be greatly appreciated!
Please reach out to me on [email protected] for further help.
Thanks for another great article. The place else may just anybody get that kind of information in such a perfect manner of writing? I have a presentation next week, and I’m at the search for such info.
Thank you for visiting.
Heya terrific website! Does running a blog similar to this
require a large amount of work? I’ve virtually
no knowledge of computer programming but I was hoping to start my own blog soon. Anyhow, if
you have any ideas or techniques for new blog owners please share.
I know this is off topic however I simply had to ask.
Many thanks!
Thanks. Please write to me on: [email protected]
I do agree with all of the ideas you’ve presented to your post.
They’re really convincing and will definitely work.
Nonetheless, the posts are very quick for starters. May
just you please extend them a little from next time? Thanks for the post.
My family members every time say that I am killing my time here
at web, except I know I am getting familiarity daily by reading thes pleasant articles
or reviews.
Please let me know if you’re looking for a author
for your blog. You have some really good posts and I think
I would be a good asset. If you ever want to take some of
the load off, I’d love to write some content for your blog in exchange for a link back
to mine. Please shoot me an e-mail if interested. Regards!
Hello there. Lets talk. Please reach out to me on [email protected]
You are so awesome! I don’t think I’ve read through something
like this before. So great to discover somebody with
a few unique thoughts on this subject matter. Seriously..
thank you for starting this up. This website is something that is needed on the internet,
someone with a bit of originality!
Thank you for the compliment mate.