MongoDB on Ubuntu

I try to install ‘mongodo-org’ package on Ubuntu 18.04 with different scenarios:

  • Every thing works fine on systems without Ubuntu’s ‘mongodb’ package installed,
  • It also works fine on systems installed with ‘mongoldb-org’ 4.4 or 5.0
  • But if the system already has ‘mongodb’ installed then I uninstall it and install ‘mongodb-org’, I got connection errors.

This is what I did to uninstall ‘mongodb’:

  1. sudo apt purge mongo-tools
  2. sudo apt purge mongodb-clients
  3. sudo apt purge mongodb-server-core

This is what I did to install ‘mongodb-org’ (as shown in installations for Ubuntu at MongoDB web site)

  1. wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
  2. echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
  3. sudo apt-get update
  4. sudo apt install mongodb-org

These are the error message I get:

MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

Any help will be appreciated.

1 Like

Is your mongodb server up and running on port 27017?
Was it installed as service?
What does status of service show
Are you trying to connect locally or remotely

These are the steps I took to replace Ubuntu’s ‘mongodb’ with ‘mongodb-org’:

Remove mongodb, which is enabled and ‘mongod’ is running when installed.

  1. sudo apt purge mongo-tools
  2. sudo apt purge mongodb-clients (This also removes mongodb-server)
  3. sudo apt purge mongodb-server-core

Install mongodb-org:

  1. Following the instruction above to install.
  2. sudo apt-get update
  3. sudo apt install mongodb-org (which is ‘disabled’ when installed.)
  4. sudo systemctl enable mongod
  5. sudo systemctl start mongod
  6. systemctl status mongod (mongod is NOT running.)
    ● mongod.service - MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Thu 2021-09-09 17:50:37 PDT; 5min ago
    Docs: https://docs.mongodb.org/manual
    Process: 7153 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
    Main PID: 7153 (code=exited, status=14)

Sep 09 17:50:36 tester-virtual-machine systemd[1]: Started MongoDB Database Server.
Sep 09 17:50:37 tester-virtual-machine systemd[1]: mongod.service: Main process exited, code=exited, status=14/n
Sep 09 17:50:37 tester-virtual-machine systemd[1]: mongod.service: Failed with result ‘exit-code’.

The ‘mongod’ process is not running.

It works fine if the machine does not have mongodb and I only go through the installation of ‘mongoldb-org’ part.

Hi @Alex_Chen1

Looking at the prerm script for mongodb-server(/var/lib/dpkg/info/mongodb-server.prerm) it might not be shutting down mongod on the apt autoremove if you are using systemd. You can check that with pidof mongod, pgrep mongod or whatever other method you can think of. So a safety step might be systemctl stop mongodb before the removal/purge steps.


From the manual installation troubleshooting:

sudo apt remove mongodb
sudo apt purge mongodb
sudo apt autoremove

If you have got a mix of ubuntu repo and mongodb-org:

If your output includes a mix of mongodb-org and mongodb binaries, you may need to first apt remove, apt purge, and apt autoremove the mongodb-org package before attempting to remove and purge the Ubuntu mongodb package. After clearing all MongoDB-related packages, retry the installation procedure.

1 Like