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-X509 enabled
GSSAPI(Kerberos) enabled
PLAIN enabled

 
这哪有什么SCRAM-SHA-1

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

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

你可能感兴趣的:(php连接MongoDB用户认证错误)