DEV Community

Cover image for How to install MongoDB on Ubuntu 22.0 LTS
ihtesham510
ihtesham510

Posted on

How to install MongoDB on Ubuntu 22.0 LTS

Install the Dependencies

sudo apt update
apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
Enter fullscreen mode Exit fullscreen mode

Add MongoDB GDB key

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Enter fullscreen mode Exit fullscreen mode

Create a list for MongoDB

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Enter fullscreen mode Exit fullscreen mode

update the local package Datebase

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

install the mongodb using the following command

sudo apt-get install -y mongodb-org
Enter fullscreen mode Exit fullscreen mode

if you run into an error like this

Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mongodb-org-mongos : Depends: libssl1.1 (>= 1.1.1) but it is not installable
 mongodb-org-server : Depends: libssl1.1 (>= 1.1.1) but it is not installable
E: Unable to correct problems, you have held broken packages.
Enter fullscreen mode Exit fullscreen mode

Run the folloing commands:

echo "deb http://security.ubuntu.com/ubuntu focal-security main" | sudo tee /etc/apt/sources.list.d/focal-security.list

sudo apt-get update

sudo apt-get install libssl1.1
Enter fullscreen mode Exit fullscreen mode

Start the MongoDB service and enable it to start automatically after rebooting the system.

systemctl start mongod
systemctl enable mongod
Enter fullscreen mode Exit fullscreen mode

Now, check the status of the MongoDB service.

systemctl status mongod
Enter fullscreen mode Exit fullscreen mode

Output:

root@crown:~# systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
     Active: active (running) since Wed 2022-03-23 18:54:34 UTC; 51min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 3619 (mongod)
     Memory: 172.3M
        CPU: 18.403s
     CGroup: /system.slice/mongod.service
             └─3619 /usr/bin/mongod --config /etc/mongod.conf
Enter fullscreen mode Exit fullscreen mode

To verify whether the installation has completed successfully by running the following command.

mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Enter fullscreen mode Exit fullscreen mode

Output:

root@crown:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9722b711-f1f0-43f2-aec6-f6172da9d237") }
MongoDB server version: 5.0.6
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}
Enter fullscreen mode Exit fullscreen mode

Creating Administrative MongoDB user

First, access the MongoDB shell.

mongo
Enter fullscreen mode Exit fullscreen mode

Output:

root@crown:~#  mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("08890b80-d543-47c1-8523-57ac3c66cf73") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
Enter fullscreen mode Exit fullscreen mode

Connect to the admin Database:

use admin
Enter fullscreen mode Exit fullscreen mode

Output:

> use admin
switched to db admin
Enter fullscreen mode Exit fullscreen mode

List the users and see if you can list the created user.

show users
Enter fullscreen mode Exit fullscreen mode

Output:

> show users
{
        "_id" : "admin.mongoAdmin",
        "userId" : UUID("861b55a3-eaf2-4617-ac4e-34f5284f8a87"),
        "user" : "mongoAdmin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}
Enter fullscreen mode Exit fullscreen mode

This concludes the installation and setup of MongoDB on your Ubuntu 22.04 server.

Top comments (0)