laravel-mongodb: Authentication failed in Laravel 5.2

Hello all,

I received a Authentication failed message with Laravel 5.2 when I ran my following controller. My MongoDB server don’t require an authentication for a connection to any databases. How should I do to fix it?

Many thanks for your help,

My TestController controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class TestController extends Controller
{

    public function test() {
        return \App\Models\Test::all();
    }
}

My config\database.php:

'mongodb' => [
            'driver'   => 'mongodb',
            'host'     => env('DB_HOST', 'localhost'),
            'port'     => env('DB_PORT', 27017),
            'database' => env('DB_DATABASE', 'myproject'),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'options' => [
                'db' => 'admin' // sets the authentication database required by mongo 3
            ]
        ],

My .env file:

DB_HOST=127.0.0.1
DB_DATABASE=myproject
DB_USERNAME=
DB_PASSWORD=

My Test model:

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Test extends Eloquent
{
    //protected $connection = 'myproject';

}

My environment:

  • Laravel 5.2
  • jenssegers/mongodb 3.0.1
  • php_mongodb 1.1.2 in PHP 5.6

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 27 (1 by maintainers)

Most upvoted comments

I just had this same problem, solved it by remove username, password from the config array.

//            'username' => env('MONGO_USERNAME', ''),
//            'password' => env('MONGO_PASSWORD', ''),

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

I have used following config and problem solved. ‘options’ => array( ‘database’ => ‘admin’ // sets the authentication database required by mongo 3 ) for latest extension use database instead of db in options array .

‘options’ => [ ‘authSource’ => ‘admin’, ]

I have config like this that didn’t work.

‘mongodb’ => [ ‘driver’ => ‘mongodb’, ‘host’ => env(‘DB_HOST’, ‘localhost’), ‘port’ => env(‘DB_PORT’, 27017), ‘username’ => env(‘DB_USERNAME’), ‘password’ => env(‘DB_PASSWORD’), ‘database’ => env(‘DB_DATABASE’), ‘options’ => [ ‘database’ => env(‘DB_DATABASE’) // sets the authentication database required by mongo 3 ] ],

After changing it like this ‘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 ] ],

It worked

@uriel2707 I use php 7.2.0. mongodb": “^3.2”. Laravel 5.4. my .env: PP_NAME=IncallSystem APP_ENV=local APP_KEY=base64:qR7DYILfoRz0mgUWUGOEcnR9dSmScrtmfXClrEiunhE= APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=http://localhost

DB_CONNECTION=mongodb DB_HOST=127.0.0.1 DB_PORT=27017

BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379

MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null

PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET=

If i run project in local, everything’s fine. When i deploy to server that use username, password. I try debug in app/Console/Commands : $test = ScheduleSms::count(); print_r($test);die; In local will return true, In serve ( has username, password ) return Authentication failed.