ace
ace

Reputation: 12024

Cannot connect to remote mongodb server

I changed bindIp setting to

bindIp: 127.0.0.1, 0.0.0.0

in mongod.conf on my ubuntu server hosted on Linode and restarted mongod and status looks ok.

I opened mongodb port on ufw

sudo ufw status Status: active

To Action From -- ------ ---- 22/tcp ALLOW Anywhere 10000
ALLOW Anywhere Nginx Full
ALLOW Anywhere 3333
ALLOW Anywhere 27017
ALLOW Anywhere 22/tcp (v6)
ALLOW Anywhere (v6) 10000 (v6)
ALLOW Anywhere (v6) Nginx Full (v6)
ALLOW Anywhere (v6) 3333 (v6)
ALLOW Anywhere (v6) 27017 (v6)
ALLOW Anywhere (v6)

Connecting to it from my mac throws error:

mongo mongodb://admin:secret@ubuntuipaddress/fielddb?authSource=admin MongoDB shell version v3.6.2 connecting to: mongodb://ubuntuipaddress/fielddb?authSource=admin 2018-04-08T13:47:32.212 W NETWORK [thread1] Failed to connect to ubuntuipaddress:27017, in(checking socket for error after poll), reason: Connection refused 2018-04-08T13:47:32.214 E QUERY
[thread1] Error: couldn't connect to server ubuntuipaddress:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed

How to fix this issue?

Upvotes: 1

Views: 7887

Answers (3)

Jeremy Kwee
Jeremy Kwee

Reputation: 39

The basic steps for enabling remote access for Mongodb running on Ubuntu are:

  1. Setup a minimum of one user in Mongodb (admin with root rights)
  2. Edit the config file (i.e. sudo nano etc/mongodb.conf)
  3. Make sure that
bind_ip = 0.0.0.0 
port = 27017 
auth = true 

are set (and uncommented)

  1. Add a firewall rule in UFW to allow 27017 from your remote IP address (or anywhere)

You should be good to go.

Upvotes: 2

ace
ace

Reputation: 12024

The problem was wrong setting of bindIP in mmongod.conf. Changing to:

bindIp: 127.0.0.1,ip_address_of_host_running_mondgod

fixed the problem. Replace ip_address_of_host_running_mondgod with ip address of host running mongod like 137.142.177.4

Upvotes: 1

user8924925
user8924925

Reputation: 56

Disable your firewall and try to see if you can connect, if you can then it's your fw rules. Try this first to see if this helps.

Upvotes: 1

Related Questions