Welcome to our comprehensive guide on installing MongoDB 7.0 on Ubuntu Linux LTS. MongoDB is a popular NoSQL database that offers scalability and flexibility for modern applications. In this tutorial, we'll walk you through the steps to install MongoDB Community Edition on your Ubuntu LTS system.
This tutorial covers the installation of MongoDB 7.0 Community Edition. Please note that MongoDB frequently releases updates, so ensure you're using the latest stable version available at the time of installation.
cat /etc/lsb-release
First, ensure gnupg
and curl
are installed:
sudo apt-get install gnupg curl
Then, import the MongoDB public GPG key:
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Create the list file /etc/apt/sources.list.d/mongodb-org-7.0.list
for your version of Ubuntu.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Issue the following command to reload the local package database:
sudo apt-get update
You can install either the latest stable version of MongoDB or a specific version of MongoDB.
sudo apt-get install -y mongodb-org
sudo apt-get install -y mongodb-org=7.0.5 mongodb-org-database=7.0.5 mongodb-org-server=7.0.5 mongodb-mongosh=7.0.5 mongodb-org-mongos=7.0.5 mongodb-org-tools=7.0.5
If you only install mongodb-org=7.0.5
and do not include the component packages, the latest version of each MongoDB package will be installed regardless of what version you specified.
To prevent unintended upgrades, you can pin the package at the currently installed version:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
Starting in MongoDB 4.4, a startup error is generated if the ulimit
value for number of open files is under 64000
.
By default, MongoDB creates data directory /var/lib/mongodb
and log directory /var/log/mongodb
. Ensure appropriate permissions for these directories.
The official MongoDB package includes a configuration file (/etc/mongod.conf
). These settings (such as the data directory and log directory specifications) take effect upon startup. That is, if you change the configuration file while the MongoDB instance is running, you must restart the instance for the changes to take effect.
To run and manage your mongod
process, you will be using your operating system's built-in init system. Recent versions of Linux tend to use systemd (which uses the systemctl
command), while older versions of Linux tend to use System V init (which uses the service
command).
If you are unsure which init system your platform uses, run the following command:
ps --no-headers -o comm 1
systemd
- go for the systemd (systemctl) code below.init
- go for the System V Init (service) code below.You can start the mongod
process by issuing the following command:
sudo systemctl start mongod
If you receive an error similar to the following when starting mongod
:
Failed to start mongod.service: Unit mongod.service not found.
Run the following command first:
sudo systemctl daemon-reload
Then run the start command above again.
sudo systemctl status mongod
You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:
sudo systemctl enable mongod
As needed, you can stop the mongod
process by issuing the following command:
sudo systemctl stop mongod
sudo systemctl restart mongod
Start a mongosh
session on the same host machine as the mongod
. You can run mongosh
without any command-line options to connect to a mongod
that is running on your localhost with default port 27017.
mongosh
For more information on connecting using mongosh
, such as to connect to a mongod
instance running on a different host and/or port, see the mongosh documentation.
You can start the mongod
process by issuing the following command:
sudo service mongod start
sudo service mongod status
As needed, you can stop the mongod
process by issuing the following command:
sudo service mongod stop
sudo service mongod restart
Start a mongosh
session on the same host machine as the mongod
. You can run mongosh
without any command-line options to connect to a mongod
that is running on your localhost with default port 27017.
mongosh
For more information on connecting using mongosh
, such as to connect to a mongod
instance running on a different host and/or port, see the mongosh documentation.
To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.
Stop the mongod
process by issuing the following command:
sudo service mongod stop
Remove any MongoDB packages that you had previously installed.
sudo apt-get purge "mongodb-org*"
Remove MongoDB databases and log files.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Congratulations, you've successfully installed MongoDB 7.0 Community Edition on your Ubuntu Linux LTS system, empowering your projects with a robust NoSQL database solution. Stay updated with MongoDB's latest releases and best practices for optimal performance, and feel free to explore further documentation and community resources for continued support and learning.
Happy Coding !
Technical Author with a passion for translating the complexities of software, computers, and emerging technologies into accessible and engaging content. Armed with a background in computer science, I blend technical expertise with a flair for effective communication in my writing. Join me on this tech-savvy journey as we explore coding languages, unravel the nuances of software architecture, and stay informed about the latest tech trends. Let's navigate the digital frontier together!