2016.10.13 工作遇到的一些mongo问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016.10.13 工作遇到的一些mongo问题相关的知识,希望对你有一定的参考价值。
一、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"}) 删除指定用户
二、mongo 3.2 用户管理操作
zayhurs1:PRIMARY> db.createUser({
... user:"rootrw",
... pwd:"rootrwpwd",
... roles:[
... {role:"dbAdmin",db:"zayhu"},
... {role:"readWrite",db:"zayhu"}
... ]
... })
zayhurs1:PRIMARY> use pushmgr
switched to db pushmgr
zayhurs1:PRIMARY> db.createUser({
... user:"pushmgrrw",
... pwd:"pushmgrrwpwd",
... roles:[
... {role:"dbAdmin",db:"pushmgr"},
... {role:"readWrite",db:"pushmgr"}
... ]
... })
Successfully added user: {
"user" : "pushmgrrw",
"roles" : [
{
"role" : "dbAdmin",
"db" : "pushmgr"
},
{
"role" : "readWrite",
"db" : "pushmgr"
}
]
}
zayhurs1:PRIMARY>
mongodb3.2 回收权限:
zayhurs1:PRIMARY> use pushmgr
switched to db pushmgr
zayhurs1:PRIMARY> db.getUsers()
[
{
"_id" : "pushmgr.pushmgrrw",
"user" : "pushmgrrw",
"db" : "pushmgr",
"roles" : [
{
"role" : "dbAdmin",
"db" : "pushmgr"
},
{
"role" : "readWrite",
"db" : "pushmgr"
}
]
}
]
zayhurs1:PRIMARY> db.revokeRolesFromUser("pushmgrrw",
... [{"role":"dbAdmin","db" : "pushmgr"}])
zayhurs1:PRIMARY> db.getUsers()
[
{
"_id" : "pushmgr.pushmgrrw",
"user" : "pushmgrrw",
"db" : "pushmgr",
"roles" : [
{
"role" : "readWrite",
"db" : "pushmgr"
}
]
}
]
zayhurs1:PRIMARY>
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")
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
} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/query.js:370:11
[email protected]/mongo/shell/collection.js:1713:12
[email protected]/mongo/shell/utils.js:1143:1
@(shell):1:1
zayhurs2:PRIMARY>
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
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
} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/mongo.js:62:1
[email protected]/mongo/shell/utils.js:761:19
[email protected]/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()
{
"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"} ]
... })
Successfully added user: {
"user" : "news",
"roles" : [
{
"role" : "readWrite",
"db" : "news"
},
{
"role" : "dbAdmin",
"db" : "news"
}
]
}
zayhurs2:PRIMARY>
本文出自 “11462293” 博客,谢绝转载!
以上是关于2016.10.13 工作遇到的一些mongo问题的主要内容,如果未能解决你的问题,请参考以下文章