php连接MongoDB用户认证错误

php连接MongoDB用户认证错误

主要错误信息:

authentication failed MONGODB-CR credentials missing in the user document

MongoDB在启用–auth之后就无法在php中链接mongodb了,提示用户验证失败

laravel报错:

ErrorException in 29a407fb57431b351434fab7e1c79ec5 line 5:
Failed to connect to: localhost:27017: Authentication failed on database ‘laravel’ with username ‘laravel’: auth failed

看代码,是按照php manual中标准的写的啊,库里是有这个用户的,用户名密码也没错,但是为什么验证通不过呢:

$m = new MongoClient("mongodb://${username}:${password}@localhost", array("db" => "myDatabase"));

查看mongo控制台,发现报的错是:

Failed to authenticate laravel@laravel with mechanism MONGODB-CR : authentication failed MONGODB-CR credentials missing in the user document

用户文档中没有MONGODB-CR,这是什么鬼?
经过一番折腾发现国内居然没有任何关于这个错误的解决方法?

无奈Google到一篇博客:
[https://code4foodblog.wordpress.com/2015/03/31/authenticationfailed-mongodb-cr-credentials-missing-in-the-user-document/] 这是一种解决方法

上mongo的官方文档中查找一番,关于mongodb的认证机制:

MongoDB’s default authentication method is a challenge and response mechanism (SCRAM-SHA-1). Previously, MongoDB used MongoDB Challenge and Response (MONGODB-CR) as the default.

mongo3.0之后就不使用MONGO-CR作为默认的用户认证方式了,默认的是SCRAM-SHA-1

赶快到phpinfo mongo部分看到:

Supported Authentication Mechanisms-
MONGODB-CR (default)enabled
MONGODB-X509enabled
GSSAPI(Kerberos)enabled
PLAINenabled

 
这哪有什么SCRAM-SHA-1

果然,根源在于php的mongodb 驱动没更新,汗

在[https://pecl.php.net/package/mongo]找到正确的驱动,果断通过了,一个小问题折腾了好久

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值