I\'ve created an admin user for mongo using these directions:
http://docs.mongodb.org/manual/tutorial/add-user-administrator/
From the mongo client it looks
This is kind of a specific case, but in case anyone gets here with my problem:
In MongoHQ, it'll show you a field called "password", but it's actually just the hash of the password. You'll have to add a new user and store the password elsewhere (because MongoHQ won't show it to you).
You may need to upgrade your mongo shell. I had version 2.4.9 of the mongo shell locally, and I got this error trying to connect to a mongo 3 database. Upgrading the shell version to 3 solved the problem.
Check for the mongo version of the client from where we are connecting to mongo server.
My case, mongo server was of version Mongo4.0.0 but my client was of version 2.4.9. Update the mongo version to update mongo cli.
Authentication is managed at a database level. When you try to connect to the system using a database, mongo actually checks for the credentials you provide in the collection <database>.system.users
. So, basically when you are trying to connect to "test", it looks for the credentials in test.system.users
and returns an error because it cannot find them (as they are stored in admin.system.users
). Having the right to read and write from all db doesn't mean you can directly connect to them.
You have to connect to the database holding the credentials first. Try:
mongo admin -u admin -p SECRETPASSWORD
For more info, check this http://docs.mongodb.org/manual/reference/privilege-documents/
This fixed my issue:
Go to terminal shell and type mongo
.
Then type use db_name
.
Then type:
db.createUser(
{
user: "mongodb",
pwd: "dogmeatsubparflavour1337",
roles: [ { role: "dbOwner", db: "db_name" } ]
}
)
Also try: db.getUsers()
Quick sample:
const MongoClient = require('mongodb').MongoClient;
// MongoDB Connection Info
const url = 'mongodb://mongodb:dogmeatsubparflavour1337@stackoverflow.com:27017/?authMechanism=DEFAULT&authSource=db_name';
// Additional options: https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options
// Use Connect Method to connect to the Server
MongoClient.connect(url)
.then((db) => {
console.log(db);
console.log('Casually connected correctly to server.');
// Be careful with db.close() when working asynchronously
db.close();
})
.catch((error) => {
console.log(error);
});
I know this may seem obvious but I also had to use a single quote around the u/n and p/w before it worked
mongo admin -u 'user' -p 'password'