Reputation: 69
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
Upvotes: 1
Views: 8722
Reputation: 140
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"
Upvotes: 1