1

I'm trying to connect remotely on a mongodb server throw my local machine, but i'm having some issues.

On the remote server i modified the 'mongod.cfg' file and i changed the bindIp from 127.0.0.1 to 0.0.0.0 to allow access. In the same file i changed the security by adding Authentication: 'enabled'.

I created an admin user:

> use admin
> db.createUser({user: "root", pwd: "root", roles:["root"]})

I started mongodb with --auth flag

> mongod --auth --port 27017

Once the server was up, i connect to it as administrator

mongo 127.0.0.1:27017 -u "root" -p "root" --authenticationDatabase "admin"

Once i was connected, i created a normal user

> use base
> db.createUser({user: "base", pwd: "base", roles:["dbOwner"]})

Then i disconnected from mongo shell and reconnected with new user credentials

> mongo <127.0.0.1:27017>/base -u "base" -p "base"

It worked properly on the remote server.

On the local machine i tried the same command on my local machine and it failed

> mongo 127.0.0.1:27017/base -u "base" -p "base"
[js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6
exception: connect failed

5
  • If I understood it correctly you have 2 machines. From one machine you want to connect to mongod on other machine. If that is the case then from client machine you cannot use 127.0.0.1 ip for connecting mongod on other machine. You need to check other public/private ips available on server machine then use the ip from correct subnet. Commented Jun 28, 2019 at 11:23
  • @vk-code how can i check that and choose the right one? Commented Jun 28, 2019 at 11:54
  • assuming it's all linux, 'netstat -nap | grep mongod' will give you the IPs mongod is using. then use one of the IPs. Commented Jun 28, 2019 at 13:19
  • @vk-code the server machine is under windows10 Commented Jul 1, 2019 at 8:11
  • for windows 10 what you can do is following tasklist | findstr mongod to get the pid of the mongod then netstat -ano | findstr PID to get the ip on which mongod is running. Then use this IP as suggested earlier. Commented Jul 11, 2019 at 15:08

1 Answer 1

1

127.0.0.1 is making reference to the localhost of the host machine, you need to set the user in both machines if you are going to use one, if you want to use a db that share the data in the cloud you can use Mlab or if both ip are in the same network you can make reference from one to another by the ip address

mongo <ip_address>:27017/base -u "base" -p "base"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.