我使用以下命令集创建用户。这应该在admin
db以及我的目标db( c2d )中创建用户:
# mongo 127.0.0.1:27017
MongoDB shell version: 3.2.6-29-g5c19788
connecting to: 127.0.0.1:27017/test
> use admin
switched to db admin
> show collections
system.users
system.version
> db.system.users.find()
> db.createUser({user:"cd2", pwd:"cd2", roles:[{role:"dbOwner", db: "c2d"}]})
Successfully added user: {
"user" : "cd2",
"roles" : [
{
"role" : "dbOwner",
"db" : "c2d"
}
]
}
> db.system.users.find()
{ "_id" : "admin.cd2", "user" : "cd2", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "4g6t9kC+godz7k6QQOfD+A==", "storedKey" : "m3tDZBQDU2Tlb1lIjLGyTHmr2QQ=", "serverKey" : "GSA4OXSod1s8mBuZBtfmXq2tlTo=" } }, "roles" : [ { "role" : "dbOwner", "db" : "c2d" } ] }
> use c2d
switched to db c2d
> db.createUser({user:"cd2", pwd:"cd2", roles:[{role:"dbOwner", db: "c2d"}]})
Successfully added user: {
"user" : "cd2",
"roles" : [
{
"role" : "dbOwner",
"db" : "c2d"
}
]
}
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.cd2", "user" : "cd2", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "4g6t9kC+godz7k6QQOfD+A==", "storedKey" : "m3tDZBQDU2Tlb1lIjLGyTHmr2QQ=", "serverKey" : "GSA4OXSod1s8mBuZBtfmXq2tlTo=" } }, "roles" : [ { "role" : "dbOwner", "db" : "c2d" } ] }
{ "_id" : "c2d.cd2", "user" : "cd2", "db" : "c2d", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "vnMjnjfykVQS8ujQXeWaYw==", "storedKey" : "OYXivkmIwuTavlwTGfjrspT6j2E=", "serverKey" : "lw8xqzAaW8V4IQ9wOmQrG2VSp88=" } }, "roles" : [ { "role" : "dbOwner", "db" : "c2d" } ] }
如果我尝试登录,我会收到一条错误消息:
# mongo 127.0.0.1:27017/c2d -u c2d -p c2d
MongoDB shell version: 3.2.6-29-g5c19788
connecting to: 127.0.0.1:27017/c2d
2016-05-22T10:35:41.862+0100 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1441:20
@(auth):6:1
@(auth):1:2
exception: login failed
然后我在conf文件中启用安全功能并重新启动服务器:
security:
authorization: enabled
错误仍然相同:
# mongo 127.0.0.1:27017/c2d -u c2d -p c2d
MongoDB shell version: 3.2.6-29-g5c19788
connecting to: 127.0.0.1:27017/c2d
2016-05-22T10:37:43.713+0100 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1441:20
@(auth):6:1
@(auth):1:2
exception: login failed
答案 0 :(得分:57)
嗯,您需要按顺序执行几个步骤才能成功创建用户。
首先,您需要创建管理员用户。我更喜欢创建超级用户。
> use admin
> db.createUser({user: "root", pwd: "123456", roles:["root"]})
重新启动MongoDB服务器并使用--auth
标志启用身份验证。
> mongod --auth --port 27017 --dbpath /var/lib/mongodb
服务器启动后,以管理员身份连接到它
> mongo <host:port> -u "root" -p "123456" --authenticationDatabase "admin"
连接后,创建普通用户。假设您的用户数据库名称为cd2
。
> use cd2
> db.createUser({user: "cd2", pwd: "cd2", roles:["dbOwner"]})
如果您看到成功消息,请从mongo shell断开连接并重新连接新用户 凭证。
> mongo <host:port>/cd2 -u "cd2" -p "cd2"
答案 1 :(得分:6)
我们在MongoDB(3.2.4)上遇到此问题的经验是,它似乎是一个错误或无证件的功能。 如果您尝试从远程shell(而不是localhost)创建用户和角色,则会创建用户,但是一旦您离开shell,它们就会消失(它们是该会话的瞬态)并且不会真正保留。
解决方案:尝试创建用户(最初在MongoDB上未启用身份验证)并直接在数据库服务器(localhost)的控制台上执行此操作。
答案 2 :(得分:5)
尝试--authenticationDatabase=admin
选项。
例如:
由mongo 127.0.0.1:27017/c2d -u c2d -p c2d
使用:mongo 127.0.0.1:27017/c2d -u c2d -p c2d --authenticationDatabase=admin
答案 3 :(得分:0)
如果您通过Shell登录,请确保在自定义数据库下的数据库“ admin ”下的创建用户,否。在您的情况下,您切换到“ c2d”。
这是我尝试过的方法(以“管理员”身份登录)
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
// (cell as? CustomCell).animate()
}
}
$ mongo -u admin -p --authenticationDatabase "admin"
> use admin
> db.createUser(
{
user: "user007",
pwd: "YourP@ssw0rd",
roles: [
{ role: "readWrite", db: "yourdb" },
]
}
)
root@mongo-server:/# mongo -u admin -p --authenticationDatabase "admin"
MongoDB shell version v4.0.6
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
MongoDB server version: 4.0.6
----
> use admin
switched to db admin
> db.createUser(
... {
... user: "user007",
... pwd: "YourP@ssw0rd",
... roles: [
... { role: "readWrite", db: "yourdb" },
... ]
... }
... )
Successfully added user: {
"user" : "user007",
"roles" : [
{
"role" : "readWrite",
"db" : "yourdb"
}
]
}
root@mongo-server:/# mongo -u user007 -p YourP@ssw0rd
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ceabf440-c584-4518-90f5-cc845eaec3b3") }
MongoDB server version: 4.0.6
---
>
$ mongo -u admin -p --authenticationDatabase "admin"
> use yourdb
> db.createUser(
{
user: "user007",
pwd: "YourP@ssw0rd",
roles: [
{ role: "readWrite", db: "yourdb" },
]
}
)
root@mongo-server:/# mongo -u admin -p --authenticationDatabase "admin"
MongoDB shell version v4.0.6
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
MongoDB server version: 4.0.6
----
> use yourdb
switched to db yourdb
> db.createUser(
... {
... user: "user007",
... pwd: "YourP@ssw0rd",
... roles: [
... { role: "readWrite", db: "yourdb" },
... ]
... }
... )
Successfully added user: {
"user" : "user007",
"roles" : [
{
"role" : "readWrite",
"db" : "yourdb"
}
]
}
>