Connect to server,

Good Evening,

I have installed mongo DB on my Ubunto version 20.04, when i run the command ‘mongo’, i get the following error

MongoDB shell version v4.4.15
connecting to: mongodb://
Error: couldn’t connect to server, connection attempt failed: SocketException: Error connecting to :: caused by :: Connection refused :
exception: connect failed
exiting with code 1

any help would be great thanks

Hi @Sherelle_Scott and welcome to the MongoDB Community forums! :wave:

It sounds like the mongod process is not running. Can you run systemctl status mongod and report the results here?

1 Like

Thank you for getting back to me @Doug_Duncan

the command returns

systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
Active: inactive (dead)

That confirms that the process is not running. Try running sudo systemctl start mongod. After a couple of seconds rerun systemctl status mongod and it should show that it’s running. If not you can show the error log and we can help troubleshoot more.

1 Like

Thank you for you help @Doug_Duncan.

I stole the below solution from stack overflow that seems to have it working.

Thank you for helping and sorry to bother you.

  1. Use mongod --version to check if it is successfully installed
  2. If you encounter any error while using mongod
sudo mkdir /data
cd /data
sudo mkdir db
sudo pkill -f mongod
  1. Then use sudo mongod command.

It seems you created a solution to a problem you didn’t have. Based on the fact that you had mongod as a service, the installer would have already created a data directory and a user with limited permissions to run the process. All you needed to do was run sudo systemctl start mongod (there are ways to get around using sudo to start a process, but every time I type sudo it makes me think about what I’m doing and why I’m needing elevated privileges) to get the service up and running.

There are limited cases where you should run a service using sudo, and mongod is not one of them. Running with sudo introduces potential security risks.

As for creating /data/db, that is where MongoDB put the data files when it was first created and for several versions afterwards, but I believe the new location on Ubuntu is something like /var/lib/mongo which is more in line with standards and best practices.


I’m still new to this,
open to listening and understanding everything you have to say

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.