一、tokumx 2.4 用户管理操作
db.isMaster()
use admin
db.addUser("root","rootpwd") 默认为管理员用户
db.auth("root","rootpwd") 验证用户
db.system.users.find() 查看用户
db.system.users.update({"user":"zayhuroot"},{"$set":{"roles":["userAdminAnyDatabase","dbAdminAnyDatabase","clusterAdmin"]}}) 更新用户的roles
db.system.users.find()
db.system.users.remove({"user":"root"}) 删除指定用户
二、mongodb3.2 管理用户操作方法总结:
db.addUser() Deprecated. Adds a user to a database, and allows administrators to configure the user’s privileges.
db.changeUserPassword() Changes an existing user’s password.
db.createUser() Creates a new user.
db.dropAllUsers() Deletes all users associated with a database.
db.dropUser() Removes a single user.
db.getUser() Returns information about the specified user.
db.getUsers() Returns information about all users associated with a database.
db.grantRolesToUser() Grants a role and its privileges to a user.
db.removeUser() Deprecated. Removes a user from a database.
db.revokeRolesFromUser() Removes a role from a user.
db.updateUser() Updates user data.
db.grantRolesToUser( "myuser" , [ { role: "dbOwner", db: "mydb" } ])
db.system.users.find()
三、在mongo 3.2上创建集群时遇到的一些问题
配置文件略...
zayhurs2:PRIMARY> rs.initiate()
zayhurs2:PRIMARY> rs.add("mongo04-mb:27027")
1
2
3
4
5
6
7
8
9
10
|
2016-10-12T17:15:03.927+0800 E QUERY [thread1] Error: count failed: {
"ok"
: 0,
"errmsg"
:
"not authorized on local to execute command { count: \"system.replset\", query: {}, fields: {} }"
,
"code"
: 13
} :
_getErrorWithCode@src
/mongo/shell/utils
.js:25:13
DBQuery.prototype.count@src
/mongo/shell/query
.js:370:11
DBCollection.prototype.count@src
/mongo/shell/collection
.js:1713:12
rs.add@src
/mongo/shell/utils
.js:1143:1
@(shell):1:1
|
zayhurs2:PRIMARY> admin.createUser(
... {
... user: "root",
... pwd: "rootpwd",
... roles: [
... { role: "userAdminAnyDatabase", db: "admin" },
... { role: "clusterAdmin", db: "admin" },
... { role: "root", db: "admin" }
... ]
... }
... )
zayhurs2:PRIMARY> show dbs
1
2
3
4
5
6
7
8
9
10
|
2016-10-12T17:21:35.797+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:761:19
shellHelper@src
/mongo/shell/utils
.js:651:15
@(shellhelp2):1:1
|
zayhurs2:PRIMARY> db.getSiblingDB("admin").auth("root","rootpwd")
1
zayhurs2:PRIMARY> show dbs
admin 0.000GB
local 0.000GB
zayhurs2:PRIMARY> rs.add("mongo04-mb:27027")
{ "ok" : 1 }
zayhurs2:PRIMARY> rs.status()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
{
"set"
:
"zayhurs2"
,
"date"
: ISODate(
"2016-10-12T09:22:53.435Z"
),
"myState"
: 1,
"term"
: NumberLong(1),
"heartbeatIntervalMillis"
: NumberLong(2000),
"members"
: [
{
"_id"
: 0,
"name"
:
"mongo03-mb:27027"
,
"health"
: 1,
"state"
: 1,
"stateStr"
:
"PRIMARY"
,
"uptime"
: 1095,
"optime"
: {
"ts"
: Timestamp(1476264168, 1),
"t"
: NumberLong(1)
},
"optimeDate"
: ISODate(
"2016-10-12T09:22:48Z"
),
"electionTime"
: Timestamp(1476263343, 2),
"electionDate"
: ISODate(
"2016-10-12T09:09:03Z"
),
"configVersion"
: 2,
"self"
:
true
},
{
"_id"
: 1,
"name"
:
"mongo04-mb:27027"
,
"health"
: 1,
"state"
: 2,
"stateStr"
:
"SECONDARY"
,
"uptime"
: 5,
"optime"
: {
"ts"
: Timestamp(1476264168, 1),
"t"
: NumberLong(1)
},
"optimeDate"
: ISODate(
"2016-10-12T09:22:48Z"
),
"lastHeartbeat"
: ISODate(
"2016-10-12T09:22:52.243Z"
),
"lastHeartbeatRecv"
: ISODate(
"2016-10-12T09:22:50.301Z"
),
"pingMs"
: NumberLong(0),
"configVersion"
: 2
}
],
"ok"
: 1
}
|
zayhurs2:PRIMARY>
zayhurs2:PRIMARY> use news
switched to db news
zayhurs2:PRIMARY> db.createUser(
... { user:"news",pwd:"newspwd",
... roles:[ {role:"readWrite",db:"news"},
... {role:"dbAdmin",db:"News"} ]
... })
zayhurs2:PRIMARY> db.getUsers()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[
{
"_id"
:
"news.news"
,
"user"
:
"news"
,
"db"
:
"news"
,
"roles"
: [
{
"role"
:
"readWrite"
,
"db"
:
"news"
},
{
"role"
:
"dbAdmin"
,
"db"
:
"News"
}
]
}
]
|
zayhurs2:PRIMARY> db.updateUser("news",
... {
... roles:[
... { "role" : "readWrite","db" : "news" },
... { "role" : "dbAdmin","db" : "news" }
... ]
... }
... )
zayhurs2:PRIMARY> db.getUsers()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[
{
"_id"
:
"news.news"
,
"user"
:
"news"
,
"db"
:
"news"
,
"roles"
: [
{
"role"
:
"readWrite"
,
"db"
:
"news"
},
{
"role"
:
"dbAdmin"
,
"db"
:
"news"
}
]
}
]
|
zayhurs2:PRIMARY> db.dropUser("news")
true
zayhurs2:PRIMARY> db.dropDatabase()
{ "ok" : 1 }