Use Mongodb from another machine on local network ubuntu 16

I want to setup Mongodb for remote access on local network.

I have a Meteor server and currently using Mongodb from same machine where the Meteor server process is hosted. But I want to host Mongodb on different machine for distributed load.

I am using Mongodb version 4.0.5.
My OS version is Ubuntu 16.04.

What I have tried so for on another machine is:

I have a database called: myappdb

First I applied authentication for it:

I typed: mongo

and then: use myappdb

After that I created:

db.createUser({ user: 'stormtrooper', pwd: '123456', roles: [{role: 'readWrite', db: 'myappdb'}] })

After that I edited this file:

sudo vim /etc/mongod.conf

# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1  <- commented out this line

security:
  authorization: 'enabled'  <- uncommented this line

Now when I try to connect to my database like this:

mongo -u stormtrooper -p 123456 192.168.100.27:27017/myappdb

I get this error:

MongoDB shell version v4.0.5
connecting to: mongodb://192.168.100.27:27017/myappdb?gssapiServiceName=mongodb
2019-01-23T10:44:40.903+0500 E QUERY    [js] Error: couldn't connect to server 192.168.100.27:27017, connection attempt failed: SocketException: Error connecting to 192.168.100.27:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed

Can anyone guide me how to fix this?

Thanks in advance.

Hello,

have you tried to ping the other IP adress? Sometimes you have also to open the port to allow access from outside.

Yes I am able to ping that machine’s ip from parent server machine:

ping 192.168.100.27
PING 192.168.100.27 (192.168.100.27) 56(84) bytes of data.
64 bytes from 192.168.100.27: icmp_seq=1 ttl=64 time=1.65 ms
64 bytes from 192.168.100.27: icmp_seq=2 ttl=64 time=2.35 ms
64 bytes from 192.168.100.27: icmp_seq=3 ttl=64 time=1.72 ms
64 bytes from 192.168.100.27: icmp_seq=4 ttl=64 time=3.24 ms
64 bytes from 192.168.100.27: icmp_seq=5 ttl=64 time=1.63 ms

have you tried to comment that in:

bindIp: 127.0.0.1  <- commented out this line

and then reload mongo config

I restarted mongod after config changes like this:

sudo service mongod restart

is the bind ip commented out?
try to execute netstat -aon on your mongo machine

Try this:

bindIp: 192.168.100.27

@minhna Tried:
bindIp: 192.168.100.27

Getting this error:

MongoDB shell version v4.0.5
connecting to: mongodb://192.168.100.27:27017/myappdb?gssapiServiceName=mongodb
2019-01-24T14:35:02.707+0500 E QUERY    [js] Error: couldn't connect to server 192.168.100.27:27017, connection attempt failed: SocketException: Error connecting to 192.168.100.27:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed

@fabianki Tried netstat -aon with bindIp commented out, still getting same issue.

could you post the result of netstat -nl command here?

Mine looks like this;

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

@minhna Here is the result of netstat -nl:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State 
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 
tcp6 0 0 :::8880 :::* LISTEN 
tcp6 0 0 ::1:631 :::* LISTEN 
tcp6 0 0 :::7780 :::* LISTEN 
udp 0 0 127.0.1.1:53 0.0.0.0:* 
udp 0 0 0.0.0.0:68 0.0.0.0:* 
udp 0 0 0.0.0.0:631 0.0.0.0:* 
udp 0 0 0.0.0.0:5353 0.0.0.0:* 
udp 0 0 0.0.0.0:46691 0.0.0.0:* 
udp 0 0 0.0.0.0:38617 0.0.0.0:* 
udp6 0 0 :::48354 :::* 
udp6 0 0 :::45886 :::* 
udp6 0 0 :::5353 :::* 
raw6 0 0 :::58 :::* 7 
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 23911 /run/user/1000/systemd/private
unix 2 [ ACC ] SEQPACKET LISTENING 11188 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 23365 /tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 26819 /run/user/1000/keyring/control
unix 2 [ ACC ] STREAM LISTENING 26820 /run/user/1000/keyring/ssh
unix 2 [ ACC ] STREAM LISTENING 24833 /run/user/1000/keyring/pkcs11
unix 2 [ ACC ] STREAM LISTENING 26041 /tmp/.ICE-unix/1518
unix 2 [ ACC ] STREAM LISTENING 25099 /run/user/1000/pulse/native
unix 2 [ ACC ] STREAM LISTENING 26040 @/tmp/.ICE-unix/1518
unix 2 [ ACC ] STREAM LISTENING 26893 @/tmp/dbus-v7o4OKXNIp
unix 2 [ ACC ] STREAM LISTENING 40042 /tmp/.org.chromium.Chromium.wmK9pe/SS
unix 2 [ ACC ] STREAM LISTENING 23364 @/tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 11159 /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 11163 /run/systemd/fsck.progress
unix 2 [ ACC ] STREAM LISTENING 11165 /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 16171 /var/run/avahi-daemon/socket
unix 2 [ ACC ] STREAM LISTENING 16172 /run/snapd.socket
unix 2 [ ACC ] STREAM LISTENING 16173 /run/snapd-snap.socket
unix 2 [ ACC ] STREAM LISTENING 16174 /run/acpid.socket
unix 2 [ ACC ] STREAM LISTENING 16175 /var/run/cups/cups.sock
unix 2 [ ACC ] STREAM LISTENING 16176 /run/uuidd/request
unix 2 [ ACC ] STREAM LISTENING 16177 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 24710 @/com/ubuntu/upstart-session/1000/1101
unix 2 [ ACC ] STREAM LISTENING 23916 /var/run/NetworkManager/private-dhcp
unix 2 [ ACC ] STREAM LISTENING 24752 @/tmp/dbus-g7k3d9b9Iv
unix 2 [ ACC ] STREAM LISTENING 24859 /home/benoit/.gnupg/S.gpg-agent
unix 2 [ ACC ] STREAM LISTENING 26856 @/tmp/ibus/dbus-ckND5p1L

can you access the mongo instance from your mongo-machine?
and show your complete mongo.conf

because for me it looks like the mongo instance is not running or binding to any outgoing port

@fabianki I am unable to access mongo either after changing config.

When I type mongo, i get the same error.

Here is my /etc/mongod.conf:

# 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: 192.168.100.27


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
 authorization: 'enable'

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Your mongo is not running.

@minhna You are right when I type:sudo service mongod status , i get this:

â—Ź mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since 2019-01-24 16:48:03 PKT; 7s ago
     Docs: https://docs.mongodb.org/manual
  Process: 14843 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=1/FAILURE)
 Main PID: 14843 (code=exited, status=1/FAILURE)

24 16:48:03 StormTrooper-Server systemd[1]: Started MongoDB Database Server.
24 16:48:03 StormTrooper-Server systemd[1]: mongod.service: Main process exited, code=exited, status=1/FAILURE
24 16:48:03 StormTrooper-Server systemd[1]: mongod.service: Unit entered failed state.
24 16:48:03 StormTrooper-Server systemd[1]: mongod.service: Failed with result 'exit-code'.

Is there something wrong with my mongod.conf?

# 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: 192.168.100.27


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
 authorization: 'enable'

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

I dont know that OS you is using, but you can see this tutorial to help you:

try bindIp: 127.0.0.1 and restart and netstat and post the results

Alright I figured it out. There was a spelling mistake with:

authorization: 'enable'

Changed to:

authorization: enabled

Anyways thanks for all the help.

1 Like