This article mainly records some of the main MongoDB's DCL operations.
MongoDB does not require a user name and password by default to log in with Mongodb.exe
First, open the MONOGODB mode of access
Modify the registration statement of the MongoDB service, modify the add--auth parameter, see the installation of MongoDB introduction
" $MONGODB _home\bin\mongod.exe " " $MONGODB _home\mongo.cfg " --install --auth
" \ "$MONGODB _home\bin\mongod.exe\"--service--auth--config=\ "$MONGODB _home\mongo.cfg\" " " MongoDB 2.6 Standard " " Auto "
Or add a configuration in Mongod.cfg
Auth=True
Second, user Rights management
- Create user
> Db.adduser ("User","abc123_") warning:the'AddUser'Shell Helper is DEPRECATED. Please use'CreateUser'insteadsuccessfully added User: {"User":"User","Roles": ["Dbowner" ] }>
You can see that when the Execute AddUser command is present wraning, although the creation was successful, MongoDB recommends using CreateUser.
>Db.createuser ({... User:"User",... pwd:"abc123_",... roles:[... {... role:"Dbowner",... DB:"MyDB"... } ... ] ... }) Successfully added User: {"User":"User", "Roles" : [ { "role":"Dbowner", "DB":"MyDB" } ]}>
If MongoDB opens the permission mode and a database does not have any users, you can create a user without verifying the permissions, and when you continue to create the second user, an error is returned and you must log in if you want to continue creating the user.
-16t10:41.904+0800 error:couldn't Add User: Not authorized-MyDB to execute command {createUser: "Test", pwd: "XXX", roles: [{role: "Dbowner", DB: "MyDB"}], Di Gestpassword:false, Writeconcern: {w: "Majority", wtimeout:30000.0}} at src/mongo/shell/db.js:1004>
Create a Super Administrator
Creating a super administrator requires that you do not have permission mode turned on. If permissions are turned on and a super administrator is created under Admin, the following error will occur
>Db.createuser ({... User:"Admin",... pwd:"Admin",... roles:[... {... role:"Useradminanydatabase",... DB:"Admin"... } ... ] ... }) the-Ten-16t10: -:01.223+0800Error:couldn'T add user:not authorized on Admin to execute command {createUser: "admin", pwd: "XXX", roles: [{role: "Useradmina Nydatabase ", DB:" admin "}], Digestpassword:false, Writeconcern: {w:" Majority ", wtimeout:30000.0}} at Src/mongo/she ll/db.js:1004>
Turn off permission mode to create a super administrator
>Use adminswitched to DB admin>Db.createuser ({... User:"Admin",... pwd:"Admin",... roles:[... {... role:"Useradminanydatabase",... DB:"Admin"... } ... ] ... }) Successfully added User: {"User":"Admin", "Roles" : [ { "role":"Useradminanydatabase", "DB":"Admin" } ]}>
If you use AddUser to create a super administrator, you must also switch to the admin database.
- Delete User
> Db.dropuser ("user")true
- Login
Anth requires the login user to have permission to the database
> use mydbswitched to db mydb> Db.auth ("user","abc123_ " )1>
MongoDB basics of getting started with DCL