標籤:mongodb
Mongodb使用者分為三種
1、全域使用者
2、資料庫對應使用者
3、唯讀使用者
查看所有的資料庫
> show dbsadmin 0.078GBbook_blog 0.078GBlocal 0.078GBmydb 0.078GBnewdb 0.078GBtest 0.078GB
查看現有所有的有哪些使用者,要切換到admin資料庫中
> 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" } ] }>
建立一個全域使用者global_user密碼為global123
建立全域使用者要切換到admin資料庫中
> use adminswitched to db admin> db.addUser("global_user","global123")WARNING: The ‘addUser‘ shell helper is DEPRECATED. 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" } ] }>
開啟驗證許可權
停止mongodb
[[email protected] bin]# /usr/local/mongodb/bin/mongod --dbpath=/data/mongodb_data/data/ --logpath=/data/mongodb_data/logs/mongodb.log --auth --fork
重新登入
[[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>
建立對應資料庫的使用者
> 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" ] }>
從以下可以看出,在沒有授權驗證前,是無法訪問的
[[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使用者管理簡單記錄