1. 添加用戶
MongoDB shell version: 1.8.2
connecting to: 127.0.0.1:30000/test
>
>
>
> use admin
switched to db admin
> db.addUser('admin','admin')
{
"user" : "admin",
"readOnly" : false,
"pwd" : "7c67ef13bbd4cae106d959320af3f704"
}
>
2. 帶上--auth參數(shù)后重啟
$MONGODB_HOME/bin/mongod --fork --logpath=$MONGODB_HOME/logs/mongodb.log --dbpath=$DATA_PATH --port=$MONGODB_PORT --auth
3. 測(cè)試
MongoDB shell version: 1.8.2
connecting to: 127.0.0.1:30000/test
>
> use admin
switched to db admin
> show collections
Sat Aug 6 11:16:45 uncaught exception: error: {
"$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",
"code" : 10057
}
switched to db admin
> db.auth('admin','admin')
1
> show collections
system.indexes
system.users
>
沒有執(zhí)行db.auth('admin','admin')之前會(huì)報(bào)錯(cuò),說是未授權(quán)
4. Java客戶端調(diào)用也需要使用用戶名和密碼,只需簡(jiǎn)單地加上一句
db.authenticate("admin", "admin".toCharArray());
5. 在admin下添加的用戶是超級(jí)管理員,可以訪問任何數(shù)據(jù)庫,用超級(jí)管理員可以為單個(gè)數(shù)據(jù)庫添加用戶,這個(gè)用戶只能訪問他所在的數(shù)據(jù)庫
> use test
switched to db test
> db.addUser("test","test")
{
"user" : "test",
"readOnly" : false,
"pwd" : "a6de521abefc2fed4f5876855a3484f5"
}
6. 在未授權(quán)的情況下,依然可以執(zhí)行
db.shutdownServer()停掉節(jié)點(diǎn)
所以運(yùn)行mongodb的
服務(wù)器端口一定要嚴(yán)格授權(quán)訪問
分享標(biāo)題:MongoDB權(quán)限
文章路徑:
http://weahome.cn/article/pdcdjs.html