A password containing special characters, especially the dollar sign, has to be put in single quotes to protect them from the command shell:
$ mongo admin -u uname -p 'password'
Answer from ronasta on Stack OverflowA password containing special characters, especially the dollar sign, has to be put in single quotes to protect them from the command shell:
$ mongo admin -u uname -p 'password'
You have to use the --authenticationDatabase to indicate mongodb where to find the user you have created. For example:
mongo admin -u uname -p 'password' --authenticationDatabase admin
Videos
Wow so many complicated/confusing answers here.
This is as of v3.4.
Short answer.
Start MongoDB without access control (
/data/dbor where your db is).mongod --dbpath /data/dbConnect to the instance.
mongoCreate the user.
use some_db db.createUser( { user: "myNormalUser", pwd: "xyz123", roles: [ { role: "readWrite", db: "some_db" }, { role: "read", db: "some_other_db" } ] } )Stop the MongoDB instance and start it again with access control.
mongod --auth --dbpath /data/dbConnect and authenticate as the user.
use some_db db.auth("myNormalUser", "xyz123") db.foo.insert({x:1}) use some_other_db db.foo.find({})
Long answer: Read this if you want to properly understand.
It's really simple. I'll dumb the following down https://docs.mongodb.com/manual/tutorial/enable-authentication/
If you want to learn more about what the roles actually do read more here: https://docs.mongodb.com/manual/reference/built-in-roles/
Start MongoDB without access control.
mongod --dbpath /data/dbConnect to the instance.
mongoCreate the user administrator. The following creates a user administrator in the
adminauthentication database. The user is adbOwnerover thesome_dbdatabase and NOT over theadmindatabase, this is important to remember.use admin db.createUser( { user: "myDbOwner", pwd: "abc123", roles: [ { role: "dbOwner", db: "some_db" } ] } )
Or if you want to create an admin which is admin over any database:
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Stop the MongoDB instance and start it again with access control.
mongod --auth --dbpath /data/dbConnect and authenticate as the user administrator towards the
adminauthentication database, NOT towards thesome_dbauthentication database. The user administrator was created in theadminauthentication database, the user does not exist in thesome_dbauthentication database.use admin db.auth("myDbOwner", "abc123")
You are now authenticated as a dbOwner over the some_db database. So now if you wish to read/write/do stuff directly towards the some_db database you can change to it.
use some_db
//...do stuff like db.foo.insert({x:1})
// remember that the user administrator had dbOwner rights so the user may write/read, if you create a user with userAdmin they will not be able to read/write for example.
More on roles: https://docs.mongodb.com/manual/reference/built-in-roles/
If you wish to make additional users which aren't user administrators and which are just normal users continue reading below.
Create a normal user. This user will be created in the
some_dbauthentication database down below.use some_db db.createUser( { user: "myNormalUser", pwd: "xyz123", roles: [ { role: "readWrite", db: "some_db" }, { role: "read", db: "some_other_db" } ] } )Exit the mongo shell, re-connect, authenticate as the user.
use some_db db.auth("myNormalUser", "xyz123") db.foo.insert({x:1}) use some_other_db db.foo.find({})
Last but not least due to users not reading the commands I posted correctly regarding the --auth flag, you can set this value in the configuration file for mongoDB if you do not wish to set it as a flag.
You need to start mongod with the --auth option after setting up the user.
From the MongoDB Site:
Run the database (mongod process) with the
--authoption to enable security. You must either have added a user to the admin db before starting the server with--auth, or add the first user from the localhost interface.
MongoDB Authentication
mongo --shell
will open the shell interface.
https://docs.mongodb.com/manual/reference/program/mongo/
Both the mongod (database server) and mongo (database client shell) programs are command line programs and each expects to be run in its own command line session. So, after starting the server (as you did with "./mongod") you should open a second command line session and run "./mongo" in it to give you a command line shell for talking to the server.