MongoDB User Management simple record

Source: Internet
Author: User
Tags auth

MongoDB users are divided into three types

1. Global Users

2. Database corresponding user

3. Read-Only users


View all databases

> Show dbsadmin 0.078gbbook_blog 0.078GBlocal 0.078GBmydb 0.078GBnewdb 0.078GBtest 0.078GB


View all existing users, to switch to the admin database

> use adminswitched to db admin> db.system.users.find () {  "_id"  :   "Admin.root",  "user"  :  "root",  "db"  :  "admin",  "credentials"  :  {  "MONGODB-CR"  :  "1a0f1c3c3aa1d592f490a2addc559383"  },  "Roles"  : [  {  "Role"  :  "root",  "db"  :  "admin"  } ] }{  "_id"  :   "Test.test_user",  "user"  :  "test_user",  "DB"  :  "test",  "credentials"  : {  "MONGODB-CR"  :  "6076B96FC3FE6002C810268702646EEC"  },  "Roles"  :  [ {  "Role"  :  "Dbowner",  "DB"  :  "test"  } ] }{  "_id " : " test.read_only ", " user " : " read_only ", " DB " : " test ", " Credentials " : { " MONGODB-CR " : " F497e180c9dc0655292fee5893c162f1 " }, " Roles " : [ {  " Role " : " read ", " DB " : " test " } ] }>


Create a global user global_user password of global123

Create a global user to switch to the admin database

> Use adminswitched to DB admin> db.adduser ("Global_user", "global123") warning:the ' addUser ' shell Helper is Depreca TED. Please use ' createUser ' insteadsuccessfully added User: {"user": "Global_user", "Roles": ["Root"]}
> db.system.users.find () {  "_id"  :  "Admin.root",  "user"  :  "root",  " DB " : " admin ", " credentials " : { " MONGODB-CR " : " 1a0f1c3c3aa1d592f490a2addc559383 " }, " Roles " : [ { " role " : " root ",   "DB"  :  "admin"  } ] }{  "_id"  :  "Test.test_user",  "user"  :  "Test_user",  "DB"  :  "test",  "credentials"  : {  "MONGODB-CR"  :  "6076B96FC3FE6002C810268702646EEC"  },  "Roles"  : [ {  "role"  :   "Dbowner",  "DB"  :  "test"  } ] }{  "_id"  :  "Test.read_only",   "User"  :  "read_only",  "DB"  :  "test",  "credentials"  : {  " MONGODB-CR " : " F497e180c9dc0655292fee5893c162f1 " }, " Roles " : [ { " Role " : " read ", " DB " : " test " } ] }{  "_id"  :  "Admin.global_user",  "user"  :  "Global_user",   "DB"  :  "admin",  "credentials"  : {  "MONGODB-CR"  :  " cad9c3ca71940e1e57c49dcca9e36f7a " }, " Roles " : [ { " role " : " root ",   "DB"  :  "admin"  } ] }>

Turn on authentication permissions

Stop MongoDB

[Email protected] bin]#/usr/local/mongodb/bin/mongod--dbpath=/data/mongodb_data/data/--logpath=/data/mongodb_ Data/logs/mongodb.log--auth--fork


Log in again

[[Email protected] ~]# mongodbmongodb shell version: 2.6.3connecting to:  test> show dbs2014-07-23t15:20:16.161+0800 listdatabases failed:{"OK"  :  0, "errmsg"  :  "not authorized on admin to execute command {  listdatabases: 1.0 } "," Code " : 13} at src/mongo/shell/mongo.js:47>  use adminswitched to db admin> show dbs2014-07-23t15:20:41.848+0800  listdatabases failed:{"OK"  : 0, "errmsg"  :  "not authorized on  admin to execute command { listdatabases: 1.0 } "," Code " : 13}  at src/mongo/shell/mongo.js:47> db.auth ("Global_user", "global123") 1> show  dbsadmin      0.078gbbook_blog  0.078gblocal       0.078GBmydb       0.078GBnewdb      0.078GBtest        0.078GB>

Create a user for the corresponding database

> Use newdbswitched to DB newdb> db.adduser ("New_user", "new123") warning:the ' addUser ' shell Helper is DEPRECATED. Please use ' createUser ' insteadsuccessfully added User: {"user": "New_user", "Roles": ["Dbowner"]}>


It can be seen from the following that it is inaccessible until authorization is verified.

[[Email protected] ~]# mongodbmongodb shell version: 2.6.3connecting to:  test> show dbs2014-07-23t15:28:15.546+0800 listdatabases failed:{"OK"  :  0, "errmsg"  :  "not authorized on admin to execute command {  listdatabases: 1.0 } "," Code " : 13} at src/mongo/shell/mongo.js:47>  use adminswitched to db admin> show dbs2014-07-23t15:28:24.734+0800  listdatabases failed:{"OK"  : 0, "errmsg"  :  "not authorized on  admin to execute command { listdatabases: 1.0 } "," Code " : 13}  at src/mongo/shell/mongo.js:47> db.auth ("New_user", "new123") Error: 18 { ok:  0.0, errmsg:  "auth failed",  code: 18 }0> use newdbswitched  to db newdb> db. Auth ("New_user", "new123") 


MongoDB User Management simple record

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.