thinkphp5 mongodb Authentication failed 解决办法

Bulk write failed due to previous MongoDB\Driver\Exception\AuthenticationException: Authentication failed.
原因是
/vendor/topthink/think-mongo/src/Connection.php文件中connect方法(大概189行左右)在拼接dns的时候缺少参数
$config['dsn'] = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '');
dns格式是mongodb://username:passwd@localhost:3306/DbName?param1=val1¶m2=val2#utf8参数补齐就行了
$config['dsn'] = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '') . "/" . $config['database'] ;
ThinkPHP5 MongoDB认证失败解决方案

本文介绍了在使用ThinkPHP5连接MongoDB时遇到的Authentication failed问题及其解决办法,问题根源在于某方法在构建DNS时遗漏了参数,修复DNS格式即可解决。
1万+

被折叠的 条评论
为什么被折叠?



