How to fix: Error: couldn't connect to server <ip address>:27017, connection attempt failed: Connection refused

Viewed 6001

I'm running mongodb on a remote server and I want to access within my local machine

I've tried to remove the mongodb.lock file, also tried sudo mongod --repair but none of these worked for me.

This happens when I'm doing mongo <ip address>:27017/testDB or simply mongo <ip address>

I'm getting:

[js] Error: couldn't connect to server <ip address>:27017, connection attempt failed: SocketException: Error connecting to <ip address>:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6
exception: connect failed  

The mongo.cfg file looks like this:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: C:\Program Files\MongoDB\Server\4.0\data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  C:\Program Files\MongoDB\Server\4.0\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


#processManagement:

#security:
  authorization: 'enabled'

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:
2 Answers

Open up mongodb config file:

Ubuntu:

sudo nano /etc/mongod.conf

Window:

MongoDB_installation_Directory/Server/{version}/bin/mongod.cfg 
// MongoDB_installation_Directory could be C://MongoDB or C://Program Files/MongoDB

Edit:

# network interfaces
net:
    port: 27017
    bindIp: 0.0.0.0   #default value is 127.0.0.1

Restart MongoDB Server:

sudo service mongod restart

By default mongodb is configured to allow connections only from localhost. We need to allow remote connections. In the config file, go to the network interfaces section and change the bindIp from 127.0.0.1 to 0.0.0.0 which means allow connections from all IP addresses.

You can also enable MongoDB Auth:

Open up mongo shell:

mongo

Inside mongo shell access the admin database. Create a new admin user.

> use admin;
> db.createUser({
      user: "admin",
      pwd: "password",
      roles: [
                { role: "userAdminAnyDatabase", db: "admin" },
                { role: "readWriteAnyDatabase", db: "admin" },
                { role: "dbAdminAnyDatabase",   db: "admin" }
             ]
  });

Now create db users and grant them roles on their respective databases.

> db.createUser({
      user: "user",
      pwd: "user_password",
      roles: [
                { role: "userAdmin", db: "yourDB" },
                { role: "dbAdmin",   db: "yourDB" },
                { role: "readWrite", db: "yourDB" }
             ]
  });

In the same config file,

security:
    authorization: 'enabled'

restart MongoDB:

Ubuntu:

sudo service mongod restart

Windows:

"MongoDB_installation_Directory/Server/{version}/bin/mongod.exe" --dbpath path/to/db
// MongoDB_installation_Directory could be C://MongoDB or C://Program Files/MongoDB

To Access mongo using auth on remote:

# to access the admin database
mongo -u admin -p password {public IP}/admin

# to access the other databases
mongo -u user -p user_password {public IP}/yourDB

Thanks to @himanshu-bansal I had this problem and solved it by:

  1. opening mongo.conf (as @himanshu-bansal said)
sudo nano /etc/mongod.conf
  1. and, then adding the IP of my server:
# network interfaces
net:
  port: 27017
  bindIp: 37.151.180.40,127.0.0.1 #for mongo 4
  1. and then restarting mongo.
sudo service mongod restart

I hope it works for you too. One more thing, I preferred to bind two IPs because I wanted to keep my localhost.

Now by my cmd or mongo compass, I can connect to my mongo server with the IP 37.151.180.40.

mongo 37.151.180.40:27017

I hope it will help you and, please read the details @himanshu-bansal said in answers.

Related