Unable to access mongo terminal

Posted November 4, 2019 17.3k views
MongoDBUbuntu 18.04
root@Wavynn-master:~# sudo service mongod start
root@Wavynn-master:~# mongo
MongoDB shell version v4.2.1
connecting to: mongodb://
2019-11-04T18:18:00.829+0000 E  QUERY    [js] Error: couldn't connect to server, connection attempt failed: SocketException: Error connecting to :: caused by :: Connection refused :
2019-11-04T18:18:00.830+0000 F  -        [main] exception: connect failed
2019-11-04T18:18:00.830+0000 E  -        [main] exiting with code 1

Hello. I have been attempting to pair my mongoDB with compass, compass asked for a authentication database, in which I used a tutorial, where I created a db called admin, and an admin role having all permissions to all databases.

This can be seen here

use admin;
switched to db admin
> db.createUser({
... user: "admin",
... pwd: "*********",
... roles: [
... {role: "userAdminAnyDatabase", db: "admin" },
... {role: "readWriteAnyDatabase", db: "admin" },
... {role: "dbAdminAnyDatabase", db: "admin" }
... ]
... });
Successfully added user: {
        "user" : "admin",
        "roles" : [
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                        "role" : "readWriteAnyDatabase",
                        "db" : "admin"
                        "role" : "dbAdminAnyDatabase",
                        "db" : "admin"

Right after this issue started occuring.

Anyone have any suggestions as to how we fix this issue? Also a tip towards how we connect compass with mongoDB on ubuntu :)

edited by MattIPv4
1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

I was facing the same issue in ArchLinux. I just restarted my mongodb service with sudo systemctl restart mongodb.service and worked for me.

hello i have the same problem i would like to know how are you fix it

Try to uninstall and remove all directories that are created by the first installation.
Don’t forget to run: sudo apt-get autoremove
Then install it from from the beginning, check official page of MongoDB for the steps.

I see this tutorial
It has a line of code that says configure your firewall to listen to remote connection at port 27017 form specific id

sudo ufw allow from client_ip_address to any port 27017

but as I was don’t have a static IP for my remote client that needs to be connected to mongodb remotely, so I cannot anticipate what will be the IP of my client that’s why I added ANY instead of IP address like this

sudo ufw allow from any to any port 27017

which is of course expose your db to potential security risks but it worked for me.

I wont recommend it to anyone, One should add ip address of client instead of “any”


by Melissa Anderson
MongoDB is a document-oriented database that is free and open-source. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure ... In Part One of this tutorial we'll use the MongoDB Repository to install the latest version of MongoDB. In Part Two, we'll enable authentication to secure it on the local system. Finally, in Part Three, we'll show how to more securely allow remote connections if they're needed.