错误信息:
mongos> use admin
switched to db admin
mongos> show dbs
2019-03-13T07:37:33.086+0000 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
./mongo连接服务后,切换到admin,使用show dbs查看数据库提示没有权限,原因为没有认证登录,解决步骤如下:
1. 创建管理员用户,如果已经有用户,直接操作第2步进行认证登录
mongos> use admin
switched to db admin
mongos> db.createUser({user:"admin",pwd:"password",roles:["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
可以在创建用户时,指定用户角色和管理的数据库
mongos> db.createUser({user:"ycsbtest",pwd:"ycsbtest",roles:[{ role:"dbOwner", db:"ycsb"}]});
Successfully added user: {
"user" : "ycsbtest",
"roles" : [
{
"role" : "dbOwner",
"db" : "ycsb"
}
]
}
2. 使用用户密码认证登录
mongos> db.auth('admin', 'password')
3. 认证后再次查看ok
mongos> show dbs
admin 0.000GB
config 0.001GB
ycsb 0.210GB