- Learn how to install MongoDB on Ubuntu 22
- How To Secure MongoDB on Ubuntu 22.04
As a developer navigating the intricate landscape of modern databases, I recently found myself in a situation where I needed to set up a MongoDB instance on a Digital Ocean droplet running Ubuntu 22.
MongoDB uses JSON-like documents with flexible schemas, meaning you can add data to a database without defining the schema in advance. You can also change the schema at any time, without having to create a new database.
In this tutorial, you will learn how to install, test, and manage MongoDB on an Ubuntu 22.04 server as a
Before diving into the installation process, ensure you have the following:
- A running Ubuntu 22 server. You can sign up for a Digital Ocean account using my referral link: https://m.do.co/c/a1a514164811.
- SSH access to your server with sudo privileges.
Step 1: Updating Packages
First, let’s make sure everything’s up to date. We do this by updating the list of software we can get and upgrading what we’ve got:
sudo apt update sudo apt upgrade
Step 2: Installing MongoDB
Ubuntu’s official package repositories include a stable version of MongoDB. However, the latest stable release is 7.0, while the version available from the default Ubuntu repositories is 5.6. To get the latest version, you need to add MongoDB’s dedicated package repository to your APT sources. This will allow you to install mongodb-org, a meta-package that always points to the latest version of MongoDB.
To do this, first import the public GPG key for the latest stable version of MongoDB by running the following command:
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \ sudo gpg -o /etc/apt/trusted.gpg.d/mongodb-server-7.0.gpg \ --dearmor
Once the key is imported, you can add the MongoDB repository to your APT sources by running the following command:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Now you can update your APT sources by running the following command:
sudo apt update
Now you can install either the latest stable version of MongoDB or a specific version of MongoDB
To install the latest stable version, run the following command:
sudo apt-get install -y mongodb-org
To install a specific version, run the following command:
sudo apt-get install -y mongodb-org=7.0 mongodb-org-database=7.0 mongodb-org-server=7.0 mongodb-mongosh=7.0 mongodb-org-mongos=7.0 mongodb-org-tools=7.0
Step 3: Starting the MongoDB Service and Testing the Database
Once MongoDB is installed, you need to start the MongoDB service. This can be done by running the following command:
sudo systemctl start mongod
You can check the status of the MongoDB service by running the following command:
sudo systemctl status mongod
The output of this command should show that the service is running and enabled.
To test the MongoDB database, you can connect to it using the mongo shell. The mongo shell is a command-line tool that allows you to interact with MongoDB databases.
To connect to the MongoDB database, run the following command:
You should see the MongoDB prompt.
At the MongoDB prompt, you can run various commands to interact with the database. For example, you can create a database, create a collection, and insert documents into the collection.
To exit the mongo shell, type
Here are some additional things to keep in mind when starting the MongoDB service and testing the database:
- The MongoDB service is typically configured to start automatically when the server boots. However, if you have not configured it to start automatically, you will need to start it manually each time you reboot the server.
- The MongoDB database is typically stored in the
- You can use the
mongodcommand to configure various aspects of the MongoDB service, such as the port number, the data directory, and the authentication settings.
In this tutorial, you learned how to install MongoDB on an Ubuntu 22.04 server. You also tested the MongoDB to make sure it properply installed and running.
As a next step, it is important to secure your MongoDB installation. You can do this by following our guide on How to Secure and Harden MongoDB on Ubuntu 22.04 Once your MongoDB installation is secure, you can configure it to accept remote connections.