> mongo --port 27017 -u usuario -p minhasenha --authenticationDatabase novoBD
Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" }
OK, it seemed the user didn't have access to the database. In MongoDB authentication is managed at a database level. When trying to connect to the MongoDB using a database, it checks for the provided credentials in the collection <database>.system.users. Solution: create the user in the database.
Well, easy solution if you have the admin credentials, but that was not the case. Workaround: as I had root access to the OS, I disabled MongoDB authentication:
> vim /etc/mongod.conf
noauth=true
#auth=true
Then logged in MongoDB and created an admin user:
> mongo --port 27017
use admin;
db.createUser(
{
user: "admin",
pwd: "minhasenha",
roles:["root"]
});
Logged out MongoDB and enabled authentication back again:
> vim /etc/mongod.conf
#noauth=true
auth=true
Logged in MongoDB back with admin user (saved its credentials for future use!) and created the user in the new database:
> mongo --port 27017 -u admin -p minhasenha --authenticationDatabase admin
use novoBD;
db.createUser(
{
user: "usuario",
pwd: "minhasenha",
roles:[{ role: "readWrite", db: "novoBD" }, { role: "dbAdmin", db: "novoBD" }]
});
After that the application happily connected to MongoDB. If the application is happy, I'm happy too!