[2016-11-07 11:59:41] local.ERROR: MongoDBDriverExceptionAuthenticationException: Authentication failed. in /data/www/cloudclass-api/vendor/mongodb/mongodb/src/Operation/Find.php:180
Stack trace:
Same problem after upgrading this package from v3.1.1 to v3.1.3 using composer update (on production and on localhost)
Any use of Moloquent throw a AuthenticationException 'Authentication failed', while still using correct mongodb credentials.
I did not changed anything in my database.php configuration, nor in my .env.
Reverting to v3.1.1 fixed the problem.
Stack:
PHP 5.6.27 (cli)
mongodb extension version 1.1.9
MongoDB version 3.2.9
Same error here.
I solved with franquis comment: https://github.com/jenssegers/laravel-mongodb/issues/1028#issuecomment-259247035
Change composer.json
this:
"jenssegers/mongodb": "^3.1"
to this:
"jenssegers/mongodb": "3.1.1"
other solved:
add the
'database' => 'admin'
to the options subsets!!
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
],
I worked that way
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3
]
],
Upgrade mongo driver dependence mongodb/mongodb 1.0.3 to 1.0.4 and update database.php
```php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3
]
],
update database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => [env('MONGODB_SERVER01'), env('MONGODB_SERVER02'), env('MONGODB_SERVER03')],
'database' => env('MONGODB_DATABASE'),
'username' => env('MONGODB_USERNAME'),
'password' => env('MONGODB_PASSWORD'),
'options' => [
'database' => env('MONGODB_DATABASE'),//不加导致auth fail
'replicaSet' => env('MONGODB_REPLICASET')//'yunpanrs'
]
],
because in 'jenssegersmongodbsrcJenssegersMongodbConnection.php' line 176
protected function getDsn(array $config)
{
// Check if the user passed a complete dsn to the configuration.
if (!empty($config['dsn'])) {
return $config['dsn'];
}
// Treat host option as array of hosts
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];
foreach ($hosts as &$host) {
// Check if we need to add a port to the host
if (strpos($host, ':') === false && !empty($config['port'])) {
$host = $host . ':' . $config['port'];
}
}
// Check if we want to authenticate against a specific database.
$auth_database = isset($config['options']) && !empty($config['options']['database']) ? $config['options']['database'] : null;
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
}
@lokitold uriel2707 it should be added to the docs.
Instead of 'database' => admin
I have still facing same issue..
stack:
php 7.2
mongo 3.2
jenssegers 3.6
laravel 6.2
database.php:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => env('DB_DATABASE'), // required with Mongo 3+
],
]
log:
local.ERROR: Authentication failed. {"exception":"[object] (MongoDB\Driver\Exception\AuthenticationException(code: 11): Authentication failed. at /var/www/html/partnerApi/vendor/mongodb/mongodb/src/Operation/Find.php:322)
[stacktrace]
I have solved my issue myself...
cheers
solution:
Issue was in laravel .env file
->don't use hash(#) in password, .env file are read hash as comment
@AbhijeetSalunkhe (Y)
I have solved my issue myself...
cheers
solution:
Issue was in laravel .env file
->don't use hash(#) in password, .env file are read hash as comment
That can be fixed by quoting values that need to use that symbol
Most helpful comment
I worked that way
'mongodb' => [ 'driver' => 'mongodb', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), 'options' => [ 'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3 ] ],