Unable to access mongo terminal

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 :)

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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”


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

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.