0

We have a EC2 MongoDb 4.0.1 instance with NodeJs 8.11.3, when I try to get access to my remote MongoDb I get a TimeOutError.

var url = "mongodb://admin:[email protected]:27017";

MongoClient.connect(url, (err, database) => {
    if (err) return console.log(err);
    db = database.db(dbName);

    app.listen(3000, function() {
        console.log("listen 3000");
    })
});

I already set up a admin, comment out the bind_ip field in the mongo.conf file and set a inbound rule for the 27017 port in the security group.

Then I want to connect locally via "mongodb://admin:[email protected]:27017" I got a "failed to connect to server ... " Error.

Any suggestions and how to fix it ?

1 Answer 1

4

you haven't specified a database, replace

"mongodb://admin:[email protected]:27017" 

by

 "mongodb://admin:[email protected]:27017/myDatabaseName"

In addition, if you use user and password you must have an authentication database, it has to be specified in your connect line with authSource:

I assume that your authentication database name is "admin"

"mongodb://admin:[email protected]:27017/myDatabaseName?authSource=admin"

If all your information are correct you must me be able to connect directly with the shell to your authentication database with :

mongo admin -u admin -p pw --host 127.0.0.1 --port 27017

And to your database with:

mongo myDatabaseName -u admin -p pw --host 127.0.0.1 --port 27017

The standard process to connect to a remote database with user, and password is the following :

On your EC2 server :

First, ensure that your 27017 port is open.

Connect your shell

    $ mongo --port 27017

Create the administrator user, myAdmin with password Test1234:

    > db.createUser({user: "myAdmin", pwd: "Test1234", roles:[{role: "userAdminAnyDatabase", db: "admin"}]})

Log out from mongo

Enable authentication on mongod.config file and disable local binding:

security:
    authorization: enabled

#  bindIp: 127.0.0.1

Restart mongod :

sudo service mongod restart

You must be able to do the following :

Authenticate while connecting with :

 mongo --port 27017 -u "myAdmin" -p "Test1234" --authenticationDatabase "admin"

Or connect then authenticate :

 mongo --port 27017

 > use admin
 switched to db admin
 > db.auth("myAdmin", "Test1234")

Then you must be able to create user, First Authenticate as admin :

 mongo --port 27017 -u "myAdmin" -p "Test1234" --authenticationDatabase "admin"

Then create user, myUser with password Abcd1234 for your database "myDb"

db.createUser({user: "myUser", pwd: "Abcd1234", roles:[{role: "readWrite", db: "myDb"}]})

Then yo must be able to connect locally with your new user on myDb database

 mongo myDb --port 27017 -u "myUser" -p "Abcd1234" --authenticationDatabase "admin"

Then verify that you can't use mongo without authentication:

  mongo myDb --port 27017

  > show collections

    "ok" : 0,
    "errmsg" : "not authorized on myDb to execute command { listCollections: 1.0, filter: {} }",
    "code" : 13

You're now able to try remote connect :

On your local shell just add the host to your connection line :

 mongo myDb --port 27017 -u "myUser" -p "Abcd1234" --authenticationDatabase "admin" --host 164.X.X.X

On nodejs you just have to add those parameters to the connection line :

"mongodb://myUser:[email protected]:27017/myDb?authSource=admin"
Sign up to request clarification or add additional context in comments.

7 Comments

I add the databasename to the url but it don't work, do I have to set the database name in a config file or any there else ?
you must specify the authentication database too, see edit
Unfortunately I cant connect, I tried to connect via shell with my public dns and also with the 127.0.0.1 host, but the connection failed... When I connect to my EC2 instance with SSH I can connect to my mongoDB.
If you can't connect on localhost your config file is probably wrong. You say you can connect to mongodb over the ssh connection, if the line : mongo myDatabaseName -u admin -p pw --host 127.0.0.1 --port 27017 fail when you're over ssh, your configuration is wrong
Ok I add to the conf file the bindIpAll: true statement and now it works.. I don't know why because I already comment the bindIp statement..
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.