我在localhost中有一个启用了安全性的mongo实例。用户如下,
db.createUser(
{
user: "mongouser",
pwd: "pass",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "clusterAdmin", db: "admin" }
]
});
我正在尝试使用testDatabase
连接到名为mgo
的数据库,如下所示
mongoConnectionUrl = "mongodb://mongouser:pass@localhost
if mgoSession == nil {
var err error
mgoSession, err = mgo.Dial(mongoConnectionUrl)
if err != nil {
return nil, err
}
}
但运行代码mongo日志时显示以下错误
2017-09-21T12:05:46.496+0530 I ACCESS [conn1] Unauthorized: not authorized on testDatabase to execute command { find: "metrics", filter: { type: 1 }, skip: 0 }
感谢任何帮助
答案 0 :(得分:0)
您需要提供对当前数据库的读/写访问权限,如下所示:
db.createUser(
{
user: "mongouser",
pwd: "pass",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "clusterAdmin", db: "admin" },
"readWrite"
]
});