on AWS ubuntu 12。

# vi /etc/mongodb.conf
auth = true

# service mongodb restart

# mongo
MongoDB shell version: 2.4.3
connecting to: test

> show dbs

> use admin
switched to db admin

新增全域 admin 資料庫中的使用者帳號、密碼後才可新增其它資料庫使用者帳號,
若沒有設定最高權限,直接幫個別資料庫新增用戶,則沒有任何效果
> db.addUser('root','abc123')
{
"user" : "root",
"readOnly" : false,
"pwd" : "e014bfea4a9c3c27ab34e50bd1ef0955",
"_id" : ObjectId("513718c47aaf796aef9c3feb")
}

查詢該 DB 使用者帳號
> use admin
switched to db admin
> show users
{
"_id" : ObjectId("50a096f516ee96353cfc210f"),
"user" : "root",
"readOnly" : false,
"pwd" : "9dff430ff5d0f55d8c623f7cff342668"

查詢該 DB 使用者帳號
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : ObjectId("50a094931eb110a1d34af0bb"),"user" : "root","readOnly" : false,"pwd" : "9dff430ff5d0f55d8c623f7cff342668" }


建立唯讀使用者
> db.addUser("user","abc123",true)
{
"user" : "user",
"readOnly" : true,
"pwd" : "dbd7a1d8b273c06354ff4abe7cc7ae0b",
"_id" : ObjectId("5192028f94cc7c82ecf484a4")
}

 

強制使用認證登入 DB
> use admin
> db.auth('root','abc123')
1

刪除 DB
> use admin
switched to db admin
> db.dropDatabase()
{ "dropped" : "admin","ok" : 1 }

刪除 DB 帳號
> use admin
switched to db admin
> db.removeUser('root')

刪除 DB 帳號
> use admin
switched to db admin
> db.system.users.remove({"user":"root"})

變更使用者密碼,指令和建立時一樣
> db.addUser("root", "abc234", true)
{
"_id" : ObjectId("5191ffbdc2022d9a85dbf09c"),
"user" : "root",
"readOnly" : true,
"pwd" : "8da2398f86d268a8b5db7d94ec2c5c6b"
}


未使用帳號、密碼登入 mongo 使用指令則會出現錯誤
> show users
Wed May 15 15:42:57.717 JavaScript execution failed: error: { "$err" : "not authorized for query on pcat.system.users", "code" : 16550 } at src/mongo/shell/query.js:L128

> show tables
Wed May 15 15:43:06.019 JavaScript execution failed: error: {
"$err" : "not authorized for query on pcat.system.namespaces",
"code" : 16550
}

flyfox 發表在 痞客邦 PIXNET 留言(0) 人氣()