mongos> use admin
switched to db admin
mongos> show dbs
admin 0.000GB
config 0.000GB
mongos> db.createUser({user:’madmin’,pwd:’Sztvis@789′,roles:[{role:’userAdminAnyDatabase’,db:’admin’}]})
Successfully added user: {
“user” : “madmin”,
“roles” : [
{
“role” : “userAdminAnyDatabase”,
“db” : “admin”
}
]
}
mongos> db.auth(‘madmin’,’Sztvis@789′)
1
mongos> sh.addShard(“myreplset01/192.168.1.233:27217,192.168.1.233:27218,192.168.1.233:27219”)
{
“code” : 13,
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases: 1, nameOnly: true }”
}
userAdminAnyDatabase对local与config没有权限,需要改成dbAdminAnyDatabase
db.getSiblingDB(‘admin’).updateUser(‘madmin’,{roles:[{role:”dbAdminAnyDatabase”,db:”admin”},{role:”clusterAdmin”,db:”admin”}]})
权限不够导致,你需要clusteradmin权限来。
db.updateUser(‘madmin’,{roles:[{role:”userAdminAnyDatabase”,db:”admin”},{role:”clusterAdmin”,db:”admin”}]})
由于版本不一样,报错不一样,主要还是权限问题,
mongos> sh.addShard(“myreplset01/192.168.1.233:27217,192.168.1.233:27218,192.168.1.233:27219”)
{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { addShard: \”myreplset01/192.168.1.233:27217,192.168.1.233:27218,192.168.1.233:27219\”, lsid: { id: UUID(\”0af413b2-47ff-48ce-88ae-a7bc28ebb5fa\”) }, $clusterTime: { clusterTime: Timestamp(1622615393, 1), signature: { hash: BinData(0, 2B78D894A0133C5DCE95A881A65B04744BD7B03B), keyId: 6925300564804763650 } }, $db: \”admin\” }”,
“code” : 13,
}
mongos> db.updateUser(‘madmin’,{roles:[{role:”userAdminAnyDatabase”,db:”admin”},{role:”clusterAdmin”,db:”admin”}]})
mongos> sh.addShard(“myreplset01/192.168.1.233:27217,192.168.1.233:27218,192.168.1.233:27219”)
{
“shardAdded” : “myreplset01”,
“ok” : 1,
“operationTime” : Timestamp(1622615435, 4),
“$clusterTime” : {
“clusterTime” : Timestamp(1622615435, 4),
“signature” : {
“hash” : BinData(0,”lHuBReFb4LKOPpvaj8Luz7IQr9A=”),
“keyId” : NumberLong(“6925300564804763650”)
}
}
}
use admin
db.system.users.find().pretty()
验证你这个用户roles
更新成功没,如果没有成功,执行如下操作
–登录mongos的用户必须有权限。
db.getSiblingDB(‘admin’).updateUser(‘madmin’,{roles:[{role:”userAdminAnyDatabase”,db:”admin”},{role:”clusterAdmin”,db:”admin”}]})
mongos> db.updateUser(‘madmin’,{roles:[{role:”userAdminAnyDatabase”,db:”admin”},{role:”clusterAdmin”,db:”admin”}]})
mongos> sh.addShard(“myreplset01/192.168.1.233:27217,192.168.1.233:27218,192.168.1.233:27219”)
{
“code” : 13,
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases: 1, nameOnly: true }”
}
mongos> db.auth(‘madmin’,’Sztvis@789′)
1
mongos> sh.addShard(“myreplset01/192.168.1.233:27217,192.168.1.233:27218,192.168.1.233:27219”)
{
“code” : 13,
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases: 1, nameOnly: true }”
}
mongos>
似乎仍任不起作用