0

I have setup mongodb on a Google cloud compute instance and am trying to connect to it remotely. My mongod.conf 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: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
#processManagement:
security:
  authorization: 'enabled'

I have setup a firewall rule in my Google Cloud console that tags the instance and opens tcp:27017 for ip ranges 0.0.0.0/0.

Checking on the port 27017 it looks like mongo is listening:

sudo netstat -tulpn | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      3781/mongod 

Overall it also seems like port 27017 is open:

netstat --listen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 *:27017                 *:*                     LISTEN   
...  

On the instance I setup an admin database with admin user:

>use admin
>db.createUser(
  {
    user: 'admin',
    pwd: 'somepass',
    roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
  }
)

I also setup a user for a test database:

>use test
>db.createUser(
  {
    user: 'tester',
    pwd: 'apassword',
    roles: [{ role: "readWrite", db: "test" },
            { role: "read", db: "reporting" }]
  }
)

This user works locally:

>use test
>db.auth('tester', 'apassword')
1

However when I try to connect remotely it fails:

$ mongo -u tester -p apassword 12.345.67.890/test
MongoDB shell version v3.4.1
connecting to: mongodb://12.345.67.890/test
2017-11-14T12:47:07.369-0700 W NETWORK  [main] Failed to connect to 12.345.67.890:27017 after 5000ms milliseconds, giving up.
2017-11-14T12:47:07.370-0700 E QUERY    [main] Error: couldn't connect to server 12.345.67.890:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:234:13
@(connect):1:6
exception: connect failed

I'm not a networking expert so I've pretty much exhausted my knowledge at this point and don't know how to proceed.

Am I missing something in mongod.conf? Did i setup the firewall incorrectly? Any help is appreciated.

1 Answer 1

0

I came up with a solution for this, but I'm unclear why it works and not my original approach.

My instance has http/https enabled. This uses the firewall rules default-allow-http and default-allow-https. These rules enable connections from anywhere (0.0.0.0/0) through tcp ports 80 and 443 respectively. I edited the http rule and added tcp port 27017.

Now I can connect to the server.

As a test, I reset the http rule and added another rule applied to all instances opening up tcp:27017 to 0.0.0.0/0. Essentially everything is the same as the http rule save for the name and the target tags. With this change I cannot connect to the server.

It seems rather strange and it doesn't feel like an intended behavior, unless my understanding of the firewall rules in incomplete.

In the end it looks like either setting up mongo to use one of the http/https ports or adding port 27017 to those rules is the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.