作者:qq2304944703 | 来源:互联网 | 2023-05-19 04:56
Whativedone:我所做的:Enabledauthenticationinetcmongod.conf:在etcmongod.启用身份验证配置:autht
What i've done:
我所做的:
Enabled authentication in /etc/mongod.conf
:
在/etc/mongod.启用身份验证配置:
auth = true
Created the first user from the shell as stated in the doc :
如doc所述,从shell中创建第一个用户:
db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
Using the admin
user, i've created a root
user to access mongodb:
使用admin用户,我创建了一个根用户来访问mongodb:
db.createUser(
{
user: "root",
pwd: "root",
roles: [ "root" ]
})
Until this point all works fine, as i can authenticate from the mongo shell with:
在此之前,一切正常,我可以从mongo shell通过以下身份验证:
mongo --port 27017 -u root -p root admin
and it works perfectly, as i can make all operations in the db.
它工作得很好,因为我可以在db中进行所有操作。
The problem:
存在的问题:
when i try to authenticate from PHP using the same root
user:
当我尝试使用相同的根用户从PHP进行身份验证时:
$client = new MongoClient("mongodb://root:root@localhost:27017/admin");
It gives the error:
这给了错误:
Failed to connect to: localhost:27017: Authentication failed on database 'admin' with username 'root': auth failed
未能连接到:localhost:27017:用户名“root”的数据库“admin”的身份验证失败
Why can't PHP authenticate on mongodb if authenticating with the same credentials works fine from the mongo shell?
如果在mongo shell中使用相同的凭证进行身份验证,为什么PHP不能在mongodb上进行身份验证呢?
Other notes:
另注:
- If I disable authentication PHP connects perfectly, and the mongo classes work fine
- 如果我禁用身份验证,PHP就能完美连接,mongo类也能正常工作
- I've tried to create different users, but the response is the same
- 我尝试过创建不同的用户,但是响应是相同的
- I am using Mongo 3.0.1, PHP 5.5.9 and Ubuntu 14.04.2 LTS 64bit
- 我正在使用Mongo 3.0.1, PHP 5.5.9和Ubuntu 14.04.2 LTS 64位
3 个解决方案