Log on to MongoDB shell and switch to the admin database
Mongo
Use admin
Create administrator user
Db. createUser (
{
User: "yourusername ",
Pwd: "password ",
Roles: [{role: "userAdminAnyDatabase", db: "admin"}]
}
)
Modify/etc/init. d/mongod and restart MongoDB.
OPTIONS = "-f $ CONFIGFILE"
Change
OPTIONS = "-- auth-f $ CONFIGFILE"
/Etc/init. d/mongod restart
Log on and perform user authentication
Mongo
Use admin
Db. auth ("yourusername", "password ")
You can also log on
Mongo -- port 27017-u yourusername-p password -- authenticationDatabase admin
After the authentication is successful, create a common user and use this user to log on to the specified database.
Db. createUser (
{
User: "123 ",
Pwd: "123123 ",
Roles :[
{Role: "readWrite", db: "firstdb"}, # read/write permission granted to the firstdb database
{Role: "read", db: "products"}, # read permission for the products database
]
}
)
Use PyMongo to connect to a user-authenticated mongodb instance
From pymongo import MongoClient
Dedicated client ('mongodb: // user: '+ 'password' +' @ 127.0.0.1 ')
If the password contains symbols such as %, \,/, you must use urllib for escape.
Import urllib
From pymongo import MongoClient
Password = urllib. quote_plus ('pass/word % &\/')
MongoClient ('mongodb: // user: '+ password +' @ 127.0.0.1 ')
Change the password of a specified user
Use admin
Db. changeUserPassword ("username", "newpassword ")
Delete a specified user
Use admin
Db. db. system. users. remove ({user: "username "})