Ubuntu 18.04 and MongoDB 4.2

First, remove MongoDB from previous if installed:

sudo apt remove --autoremove mongodb-org

Remove any mongodb repo list files:

sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update

Now, add the new key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 4B7C549A058F8B6B

Add the new repository:

echo "deb [arch=amd64] http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Install MongoDB

sudo apt update
sudo apt install mongodb-org

Enable and start the mongod server service:

systemctl enable mongod.service
systemctl start mongod.service

Check your installation:

~$ mongo --version
MongoDB shell version v4.2.2
git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64

You can also check your service has started:

~$ systemctl status mongod.service 
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: e
   Active: active (running) since Mon 2019-12-30 08:05:48 MST; 1min 7s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 7941 (mongod)
   CGroup: /system.slice/mongod.service
           └─7941 /usr/bin/mongod --config /etc/mongod.conf

Ubuntu 20.04 and MongoDB 4.4 or MongoDB 5.0

First, remove MongoDB from previous if installed:

systemctl stop mongod.service
systemctl disable mongod.service
sudo apt remove --autoremove mongodb-org

Remove any mongodb repo list files:

sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update

Now, add the new key for MongoDB 4.4:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 656408E390CFB1F5

The new key for MongoDB 5.0:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv B00A0BD1E2C63C11

Add the new repository for MongoDB 4.4:

echo "deb [arch=amd64] http://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org.list

Or the new repository for MongoDB 5.0:

echo "deb [arch=amd64] http://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org.list

Install MongoDB

sudo apt update
sudo apt install mongodb-org

Enable and start the mongod server service:

systemctl enable mongod.service
systemctl start mongod.service

Check your installation:

~$ mongo --version
MongoDB shell version v4.4.3
Build Info: {
    "version": "4.4.3",
    "gitVersion": "913d6b62acfbb344dde1b116f4161360acd8fd13",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Or

$ mongo --version
MongoDB shell version v5.0.2
Build Info: {
    "version": "5.0.2",
    "gitVersion": "6d9ec525e78465dcecadcff99cce953d380fedc8",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

You can also check your service has started:

~$ systemctl status mongod.service 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
     Active: active (running) since Sun 2021-01-24 07:52:50 MST; 27s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 2353629 (mongod)
     Memory: 59.2M
     CGroup: /system.slice/mongod.service
             └─2353629 /usr/bin/mongod --config /etc/mongod.conf

Jan 24 07:52:50 terrance-ubuntu systemd[1]: Started MongoDB Database Server.

Ubuntu 22.04 and MongoDB 6.0

The MongoDB repos are not 100% ready for 22.04 yet, so you will have to use the Focal repository from them to install it. Also, the repo doesn’t have the key ready for GPG since apt-key is now deprecated, but these instructions still work for now.

First, remove MongoDB from previous if installed:

systemctl stop mongod.service
systemctl disable mongod.service
sudo apt remove --autoremove mongodb-org

Remove any mongodb repo list files:

sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update

You might need to install gpg before doing the next step.

sudo apt install gpg

Now, add the new key for MongoDB 6.0:

curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb60.gpg

Add the new repository for MongoDB 6.0 (You have to use the Focal repo as the Jammy repo does not contain the mongodb-org install for the mongod server):

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/mongodb60.gpg] http://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org.list

Install MongoDB

sudo apt update
sudo apt install mongodb-org

Enable and start the mongod server service:

systemctl enable mongod.service
systemctl start mongod.service

Check your installation:

mongod --version
db version v6.0.2
Build Info: {
    "version": "6.0.2",
    "gitVersion": "94fb7dfc8b974f1f5343e7ea394d0d9deedba50e",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

systemctl status mongod.service 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
     Active: active (running) since Thu 2022-10-13 08:13:21 MDT; 39s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 44048 (mongod)
     Memory: 68.1M
        CPU: 1.169s
     CGroup: /system.slice/mongod.service
             └─44048 /usr/bin/mongod --config /etc/mongod.conf

Oct 13 08:13:21 terrance-ubuntu systemd[1]: Started MongoDB Database Server.

Sửa lỗi System has not been booted with systemd as init system (PID 1). Can’t operate. Failed to connect to bus: Host is down

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig
sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME
snap version

Leave a Reply

Your email address will not be published. Required fields are marked *