Laravel-mongodb: why project connected the mongo Authentication failed?but client connected succ...

Created on 7 Nov 2016  ·  11Comments  ·  Source: jenssegers/laravel-mongodb

[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:

0 /data/www/cloudclass-api/vendor/mongodb/mongodb/src/Operation/Find.php(180): MongoDBDriverServer->executeQuery('cloudclass-test...', Object(MongoDBDriverQuery), Object(MongoDBDriverReadPreference))

1 /data/www/cloudclass-api/vendor/mongodb/mongodb/src/Collection.php(437): MongoDBOperationFind->execute(Object(MongoDBDriverServer))

2 [internal function]: MongoDBCollection->find(Array, Array)

3 /data/www/cloudclass-api/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Collection.php(42): call_user_func_array(Array, Array)

4 /data/www/cloudclass-api/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php(370): JenssegersMongodbCollection->__call('find', Array)

5 /data/www/cloudclass-api/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php(182): JenssegersMongodbQueryBuilder->getFresh(Array)

6 /data/www/cloudclass-api/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1577): JenssegersMongodbQueryBuilder->get(Array)

7 /data/www/cloudclass-api/app/Http/Controllers/LessonController.php(743): IlluminateDatabaseQueryBuilder->first()

8 [internal function]: AppHttpControllersLessonController->show()

9 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9424): call_user_func_array(Array, Array)

10 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9486): IlluminateRoutingController->callAction('show', Array)

11 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9466): IlluminateRoutingControllerDispatcher->call(Object(AppHttpControllersLessonController), Object(IlluminateRoutingRoute), 'show')

12 [internal function]: IlluminateRoutingControllerDispatcher->IlluminateRouting{closure}(Object(IlluminateHttpRequest))

13 /data/www/cloudclass-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(IlluminateHttpRequest))

14 /data/www/cloudclass-api/vendor/tymon/jwt-auth/src/Middleware/GetUserFromToken.php(46): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))

15 [internal function]: TymonJWTAuthMiddlewareGetUserFromToken->handle(Object(IlluminateHttpRequest), Object(Closure))

16 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9963): call_user_func_array(Array, Array)

17 [internal function]: IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))

18 /data/www/cloudclass-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(IlluminateHttpRequest))

19 [internal function]: IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))

20 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9948): call_user_func(Object(Closure), Object(IlluminateHttpRequest))

21 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9467): IlluminatePipelinePipeline->then(Object(Closure))

22 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9454): IlluminateRoutingControllerDispatcher->callWithinStack(Object(AppHttpControllersLessonController), Object(IlluminateRoutingRoute), Object(IlluminateHttpRequest), 'show')

23 /data/www/cloudclass-api/bootstrap/cache/compiled.php(8524): IlluminateRoutingControllerDispatcher->dispatch(Object(IlluminateRoutingRoute), Object(IlluminateHttpRequest), 'App\Http\Contro...', 'show')

24 /data/www/cloudclass-api/bootstrap/cache/compiled.php(8511): IlluminateRoutingRoute->runController(Object(IlluminateHttpRequest))

25 /data/www/cloudclass-api/bootstrap/cache/compiled.php(8225): IlluminateRoutingRoute->run(Object(IlluminateHttpRequest))

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
    ]
],

All 11 comments

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
    ]
],

Its works

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]

0 /var/www/html/partnerApi/vendor/mongodb/mongodb/src/Operation/Find.php(322): MongoDB\Driver\Server->executeQuery('test.use...', Object(MongoDB\Driver\Query), Array)

1 /var/www/html/partnerApi/vendor/mongodb/mongodb/src/Collection.php(663): MongoDB\Operation\Find->execute(Object(MongoDB\Driver\Server))

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HassanIbrahim picture HassanIbrahim  ·  3Comments

kschethan picture kschethan  ·  3Comments

lgt picture lgt  ·  3Comments

pirmax picture pirmax  ·  3Comments

geofflancaster picture geofflancaster  ·  3Comments