centos7下安裝3.2.6版本的MongoDB參考此篇文章
上述過程安裝下的mongodb银室,在命令窗口中默認(rèn)是超級用戶器罐,什么權(quán)限都有膨俐,這顯然不是我們需要的。
在3.0版本以后是不存在admin數(shù)據(jù)庫的佛呻,所以新建一個admin數(shù)據(jù)庫,并添加一個角色
use admin
db.createUser(
? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ?user: "admin",
? ? ? ? ? ? ? ? ? ? ?customData:{description:"superuser"}, ?--描述病线,非必需
? ? ? ? ? ? ? ? ? ? ?pwd: "admin",
? ? ? ? ? ? ? ? ? ? ?roles: [{role:"userAdminAnyDatabase",db:"admin"}]
? ? ? ? ? ? ? ? ? ? ?--注意這兩個參數(shù)吓著,稍后再解釋
? ? ? ? ? }
)
可以通過指令查看添加的用戶:
show users
或
db.system.users.find()
備注:可以使用操作系統(tǒng)的 kill 命令,給 mongod 進(jìn)程發(fā)送 SIGINT 或 SIGTERM 信號送挑,即 "kill -2 PID," 或者 “kill -15 PID“绑莺。建議不要使用 ”kill -9 pid“,因?yàn)槿绻?MongoDB 運(yùn)行在沒開啟日志(--journal)的情況下惕耕,可能會造成數(shù)據(jù)損失紊撕。
怎么關(guān)閉?mongoDB?
1赡突、關(guān)閉 MongoDB 服務(wù) ?use admin ? ?db.shutdownServer()
2对扶、kill ?-2 ? ?pid
3、kill ?-15 ?pid
關(guān)閉mongoDB之后
修改配置文件/usr/local/mongodb/bin/mongodb.conf惭缰,增加一行auth = true浪南,在重新啟動服務(wù)
或
啟動服務(wù)時增加參數(shù) --auth
/usr/local/mongodb/bin/mongod ?--auth ?--f /usr/local/mongodb/bin/mongodb.conf
再次進(jìn)入mongodb 的 shell,此時你操作mongodb比如show dbs 報錯如下
show dbs
2017-02-08T00:18:30.680+0800 E QUERY? ? [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:760:19
shellHelper@src/mongo/shell/utils.js:650:15
@(shellhelp2):1:1
正確操作
use admin
db.auth("admin","admin")?#認(rèn)證漱受,返回1表示成功
或
/usr/local/mongodb/bin/mongo -u admin -p admin --authenticationDatabase admin
備注:admin(authenticationDatabase) 只有用戶管理的權(quán)限,沒有其他權(quán)限络凿,下面創(chuàng)建用戶,用戶綁定數(shù)據(jù)庫昂羡,即在那個數(shù)據(jù)庫下創(chuàng)建的用戶就要在那個數(shù)據(jù)庫下認(rèn)證
use demo
db.createUser(
? ? ? ?{
? ? ? ? ? ? ? user:"ybl",
? ? ? ? ? ? ? customData:{description:"test user_1"},
? ? ? ? ? ? ? pwd:"926055",
? ? ? ? ? ? ? roles:[{"role":"readWrite","db":"demo"}]
? ? ? ?}
)
db.createUser(
? ? ? {
? ? ? ? ? ? ? user:"hqw",
? ? ? ? ? ? ? customData:{description:"test user_2"},
? ? ? ? ? ? ? pwd:"940331",
? ? ? ? ? ? ? roles:[{"role":"read","db":"demo"}]
? ? ? }
)
可自行驗(yàn)證
use? admin? ? db.auth("admin","admin") ? ?- - 用戶管理
use? demo ? ? db.auth("ybl","926055") ? ? ? - - 讀寫
use ?demo ? ? db.auth("hqw","940331") ? ? - - 只讀
名詞解釋:
Read:允許用戶讀取指定數(shù)據(jù)庫
readWrite:允許用戶讀寫指定數(shù)據(jù)庫
dbAdmin:允許用戶在指定數(shù)據(jù)庫中執(zhí)行管理函數(shù)絮记,如索引創(chuàng)建、刪除虐先,查看統(tǒng)計或訪問system.profile
userAdmin:允許用戶向system.users集合寫入怨愤,可以找指定數(shù)據(jù)庫里創(chuàng)建、刪除和管理用戶
clusterAdmin:只在admin數(shù)據(jù)庫中可用蛹批,賦予用戶所有分片和復(fù)制集相關(guān)函數(shù)的管理權(quán)限撰洗。
readAnyDatabase:只在admin數(shù)據(jù)庫中可用篮愉,賦予用戶所有數(shù)據(jù)庫的讀權(quán)限
readWriteAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的讀寫權(quán)限
userAdminAnyDatabase:只在admin數(shù)據(jù)庫中可用差导,賦予用戶所有數(shù)據(jù)庫的userAdmin權(quán)限
dbAdminAnyDatabase:只在admin數(shù)據(jù)庫中可用试躏,賦予用戶所有數(shù)據(jù)庫的dbAdmin權(quán)限。
root:只在admin數(shù)據(jù)庫中可用设褐。超級賬號颠蕴,超級權(quán)限
roles:指定用戶的角色,可以用一個空數(shù)組給新用戶設(shè)定空角色助析;在roles字段,可以指定內(nèi)置角色和用戶定義的角色犀被。role里的角色可以選:
Built-In Roles(內(nèi)置角色):
1. 數(shù)據(jù)庫用戶角色:read、readWrite;
2. 數(shù)據(jù)庫管理角色:dbAdmin貌笨、dbOwner弱判、userAdmin;
3. 集群管理角色:clusterAdmin锥惋、clusterManager昌腰、clusterMonitor、hostManager膀跌;
4. 備份恢復(fù)角色:backup遭商、restore;
5. 所有數(shù)據(jù)庫角色:readAnyDatabase捅伤、readWriteAnyDatabase劫流、userAdminAnyDatabase、dbAdminAnyDatabase
6. 超級用戶角色:root
// 這里還有幾個角色間接或直接提供了系統(tǒng)超級用戶的訪問(dbOwner 丛忆、userAdmin祠汇、userAdminAnyDatabase)
7. 內(nèi)部角色:__system
更多信息比如
修改密碼、修改密碼和用戶信息等