mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

why project connected the mongo Authentication failed?but client connected succ... #1028

Closed yuchao86 closed 7 years ago

yuchao86 commented 7 years ago

[2016-11-07 11:59:41] local.ERROR: MongoDB\Driver\Exception\AuthenticationException: 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): MongoDB\Driver\Server->executeQuery('cloudclass-test...', Object(MongoDB\Driver\Query), Object(MongoDB\Driver\ReadPreference))

1 /data/www/cloudclass-api/vendor/mongodb/mongodb/src/Collection.php(437): MongoDB\Operation\Find->execute(Object(MongoDB\Driver\Server))

2 [internal function]: MongoDB\Collection->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): Jenssegers\Mongodb\Collection->__call('find', Array)

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

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

7 /data/www/cloudclass-api/app/Http/Controllers/LessonController.php(743): Illuminate\Database\Query\Builder->first()

8 [internal function]: App\Http\Controllers\LessonController->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): Illuminate\Routing\Controller->callAction('show', Array)

11 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9466): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\LessonController), Object(Illuminate\Routing\Route), 'show')

12 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing{closure}(Object(Illuminate\Http\Request))

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

14 /data/www/cloudclass-api/vendor/tymon/jwt-auth/src/Middleware/GetUserFromToken.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request))

15 [internal function]: Tymon\JWTAuth\Middleware\GetUserFromToken->handle(Object(Illuminate\Http\Request), Object(Closure))

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

17 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request))

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

19 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request))

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

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

22 /data/www/cloudclass-api/bootstrap/cache/compiled.php(9454): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\LessonController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'show')

23 /data/www/cloudclass-api/bootstrap/cache/compiled.php(8524): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\Http\Contro...', 'show')

24 /data/www/cloudclass-api/bootstrap/cache/compiled.php(8511): Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))

25 /data/www/cloudclass-api/bootstrap/cache/compiled.php(8225): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))

franquis commented 7 years ago

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

sefsinalas commented 7 years ago

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"

yuchao86 commented 7 years ago

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

lokitold commented 7 years ago

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
    ]
],
uriel2707 commented 7 years ago

Its works

Upgrade mongo driver dependence mongodb/mongodb 1.0.3 to 1.0.4 and update database.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
    ]
],
Alamo19 commented 6 years ago

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 'jenssegers\mongodb\src\Jenssegers\Mongodb\Connection.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 : '');
    }
ydaniels commented 6 years ago

@lokitold uriel2707 it should be added to the docs.

Instead of 'database' => admin

AbhijeetSalunkhe commented 4 years ago

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))

AbhijeetSalunkhe commented 4 years ago

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

LizRuelas commented 4 years ago

@AbhijeetSalunkhe (Y)

cPlayIt commented 3 years ago

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