In this tutorial we will discuss how to uninstall/remove MongoDB Community Edition from Rocky Linux 8 operating system. To completely uninstall/remove MongoDB from a system, we have to remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. in this tutorial, an example will be given of how to uninstall mongoDB on a Linux operating system, Rocky Linux 8.
Removing MongoDB
This process will completely remove MongoDB, its configuration, and all databases and the process is not reversible, so we have to ensure that all configuration and data is backed up before proceeding. The following are the steps to remove MongoDB Community Edition from Rocky Linux 8 Linux operating system.
- Stop MongoDB service
- Remove MongoDB packages
- Remove data directories
The detailed explanation is as follows.
1. Stop MongoDB Service
The first step to remove MongoDB from our system is to stop its service. To stop the mongod process, it can be done by issuing the following command:
$ sudo systemctl status mongod $ sudo service mongod stop
Output :
[ramansah@otodiginet ~]$ sudo systemctl status mongod [sudo] password for ramansah: ● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2023-01-01 09:15:32 WIB; 1min 36s ago Docs: https://docs.mongodb.org/manual Process: 1519 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS) Process: 1513 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 1489 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 1459 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS) Main PID: 1686 (mongod) Memory: 249.4M CGroup: /system.slice/mongod.service └─1686 /usr/bin/mongod -f /etc/mongod.conf Jan 01 09:15:23 otodiginet systemd[1]: Starting MongoDB Database Server... Jan 01 09:15:27 otodiginet mongod[1519]: about to fork child process, waiting until server is ready for connect> Jan 01 09:15:27 otodiginet mongod[1519]: forked process: 1686 Jan 01 09:15:32 otodiginet mongod[1519]: child process started successfully, parent exiting Jan 01 09:15:32 otodiginet systemd[1]: Started MongoDB Database Server.
[ramansah@otodiginet ~]$ sudo service mongod stop Redirecting to /bin/systemctl stop mongod.service
2. Remove MongoDB packages
At this step, we will remove any MongoDB packages that you had previously installed. For this purpose, we will submit the following command line :
$ sudo dnf erase $(rpm -qa | grep mongodb-org)
Output :
[ramansah@otodiginet ~]$ sudo dnf erase $(rpm -qa | grep mongodb-org) Dependencies resolved. ================================================================================================================ Package Architecture Version Repository Size ================================================================================================================ Removing: mongodb-org x86_64 5.0.4-1.el8 @mongodb-org-5.0 0 mongodb-org-database x86_64 5.0.4-1.el8 @mongodb-org-5.0 0 mongodb-org-database-tools-extra x86_64 5.0.4-1.el8 @mongodb-org-5.0 15 k mongodb-org-mongos x86_64 5.0.4-1.el8 @mongodb-org-5.0 72 M mongodb-org-server x86_64 5.0.4-1.el8 @mongodb-org-5.0 104 M mongodb-org-shell x86_64 5.0.4-1.el8 @mongodb-org-5.0 56 M mongodb-org-tools x86_64 5.0.4-1.el8 @mongodb-org-5.0 0 Removing unused dependencies: mongodb-database-tools x86_64 100.5.1-1 @mongodb-org-5.0 125 M mongodb-mongosh x86_64 1.6.1-1.el8 @mongodb-org-5.0 210 M Transaction Summary ================================================================================================================ Remove 9 Packages Freed space: 566 M Is this ok [y/N]: Y Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Running scriptlet: mongodb-org-5.0.4-1.el8.x86_64 1/1 Erasing : mongodb-org-5.0.4-1.el8.x86_64 1/9 Erasing : mongodb-org-database-5.0.4-1.el8.x86_64 2/9 Erasing : mongodb-org-tools-5.0.4-1.el8.x86_64 3/9 Erasing : mongodb-org-database-tools-extra-5.0.4-1.el8.x86_64 4/9 Erasing : mongodb-org-mongos-5.0.4-1.el8.x86_64 5/9 Running scriptlet: mongodb-org-server-5.0.4-1.el8.x86_64 6/9 Removed /etc/systemd/system/multi-user.target.wants/mongod.service. Erasing : mongodb-org-server-5.0.4-1.el8.x86_64 6/9 warning: /var/log/mongodb/mongod.log saved as /var/log/mongodb/mongod.log.rpmsave Running scriptlet: mongodb-org-server-5.0.4-1.el8.x86_64 6/9 Erasing : mongodb-org-shell-5.0.4-1.el8.x86_64 7/9 Erasing : mongodb-database-tools-100.5.1-1.x86_64 8/9 Running scriptlet: mongodb-database-tools-100.5.1-1.x86_64 8/9 Erasing : mongodb-mongosh-1.6.1-1.el8.x86_64 9/9 Running scriptlet: mongodb-mongosh-1.6.1-1.el8.x86_64 9/9 Verifying : mongodb-database-tools-100.5.1-1.x86_64 1/9 Verifying : mongodb-mongosh-1.6.1-1.el8.x86_64 2/9 Verifying : mongodb-org-5.0.4-1.el8.x86_64 3/9 Verifying : mongodb-org-database-5.0.4-1.el8.x86_64 4/9 Verifying : mongodb-org-database-tools-extra-5.0.4-1.el8.x86_64 5/9 Verifying : mongodb-org-mongos-5.0.4-1.el8.x86_64 6/9 Verifying : mongodb-org-server-5.0.4-1.el8.x86_64 7/9 Verifying : mongodb-org-shell-5.0.4-1.el8.x86_64 8/9 Verifying : mongodb-org-tools-5.0.4-1.el8.x86_64 9/9 Installed products updated. Removed: mongodb-database-tools-100.5.1-1.x86_64 mongodb-mongosh-1.6.1-1.el8.x86_64 mongodb-org-5.0.4-1.el8.x86_64 mongodb-org-database-5.0.4-1.el8.x86_64 mongodb-org-database-tools-extra-5.0.4-1.el8.x86_64 mongodb-org-mongos-5.0.4-1.el8.x86_64 mongodb-org-server-5.0.4-1.el8.x86_64 mongodb-org-shell-5.0.4-1.el8.x86_64 mongodb-org-tools-5.0.4-1.el8.x86_64 Complete!
3. Remove Data Directories
The final step is to remove remove MongoDB databases and log files.
$ sudo rm -r /var/log/mongodb $ sudo rm -r /var/lib/mongo
Conclusion
Remove MongoDB can be done easily, but we have to make sure the data is backed up before the removing process is carried out. We can do a backup first to secure the data in MongoDB before uninstalling MongoDB.