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"