×

How to Install MongoDB on Ubuntu 24.04 – LinuxWays

How to Install MongoDB on Ubuntu 24.04 – LinuxWays


Having organized structured data is far easier to look up and maintain compared to unstructured data. To keep data organized, there are different database tools like MongoDB. It is a NoSQL document database platform designed to overcome the shortcomings of relational databases and other NoSQL solutions.

It uses a flexible schema for storing data which can be updated or altered at any time, unlike relational databases. MongoDB is supported on multiple operating systems, including the latest version of Ubuntu 24.04 and this guide will discuss its installation process in detail.

Outline:

How to Install MongoDB on Ubuntu 24.04

MongoDB works pretty well with unstructured and is capable of handling large volumes of data. This makes it suitable for Big Data systems, Map Reduce applications, news site forums, and social networking applications. On Ubuntu 24.04 there is only one way to install MongoDB by far and that is by using its official repository. So first you have to add the GPG key which will verify the authenticity of the MongoDB package so execute:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc |

sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg

–dearmor

Now add the MongoDB repository to the sources.list directory, for that the command below first gives the path for the GPG key and then saves the repository link to the sources list directory:

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

Now update the apt packages list and then use it to install MongoDB on Ubuntu 24.04:

sudo apt install -y mongodb-org

Just to be sure here I have installed the MongoDB server daemon community version whereas there is another version MongoDB shard routing service. The mongos is suitable only if you are setting up a shared cluster whereas for a standalone MongoDB server mongod is suitable. To verify the installation of MongoDB run the version command:

Note: On the official MongoDB website there is its deb file and tar file available for installation which can either be downloaded by selecting the platform and package:

Or you can simply copy the link for it and use the wget utility to download it via the command line:

wget https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/7.0/multiverse/binary-arm64/mongodb-org-server_7.0.7_arm64.deb

The fun part is that when you try to install it through the deb file you will get the error of broken packages and some dependencies issues as in the image below:

To fix this issue, I have tried installing the missing libraries via apt and adding the repository, which was a total waste of time as the dependency does not have a release file:

echo “deb http://security.ubuntu.com/ubuntu impish-security main” | sudo tee /etc/apt/sources.list.d/impish-security.list

How to Configure MongoDB on Ubuntu 24.04

When MongoDB is installed on Ubuntu 24 it is needed to be configured to make it run smoothly. It is usually inactive when installed so to verify it check its service status:

sudo systemctl status mongod

Now if it is inactive then first enable it and then start the MongoDB service:

sudo systemctl enable mongod

sudo systemctl start mongod

Now, launch this database management toll execute:

By default, there is no user added to the database nor there is authentication required for making any changes in the system. So first move to the admin user by executing:

Next, create a user by using the following syntax, here I have created a new admin user but you can grant the role based on your preference:

db.createUser({

user: “<desired-user-name>”,

pwd: “<desired-passowrd>”,

roles: [ { role: “root”, db: “admin” } ]

}

)

Just edit the above syntax paste the whole code and hit enter to execute it:

Now verify the user creation by listing MongoDB users:

To keep the database secure you need to enable the authentication for logging into the database and to make any changes. Furthermore, to give database access to specified systems enter their respective IP address separated by a comma and set the port t 27017 if not set automatically in the MongoDB configuration file:

security:

authorization: enabled

net:

Port: 27017

bindIp: <give-IPaddress>

Once done with the changes verify them and save the configuration file:

To make communication between the MongoDB server and other systems allow port 27017 through the firewall:

Now to apply the changes, restart the MongoDB service on Ubuntu and then verify if the port allowed through the firewall is communicating successfully:

sudo systemctl restart mongod

sudo lsof -i | grep mongo

How to Remove MongoDB on Ubuntu 24.04

To completely remove the MongoDB database tool from Ubuntu 24.04 first uninstall using the apt package manager:

sudo apt remove –autoremove mongodb-org

Next, remove its repository directory in the sources.list.d folder:

sudo rm /etc/apt/sources.list.d/mongodb-org-7.0.list

Lastly, remove the GPG key added in the Ubuntu keyring directory:

sudo rm mongodb-server-7.0.gpg

Conclusion

MongoDB is a NoSQL document database that is primarily used for applications that require handling a large volume of data with a flexible schema. To install MongoDB on Ubuntu 24.04 there is only one way and that is by adding the repository. For that first, add the GPG key and then add the repository in the sources directory. For MongoDB its deb and tar files are also available but while installing them some library errors are encountered which are no longer supported by Ubuntu24.04.



Source link