skip to Main Content

My system has 2 xampp’s one with PHP 5.6 and the other with PHP 7.3. I have correctly included both the PHP versions in environment variables. I am trying to connect my laravel v5.8 application with mongodb v4.2.

I have used jenssegers/mongodb package in my application. Also I have added MongodbServiceProvider in app.php. I am using Robo 3T for mongodb GUI. Now whenever I try to post data to mongodb collection then I get a authentication failed error.N= Below are the codes that I have used in my application.

Database.php(Config folder)

'mongodb' => [
            'driver'   =>  'mongodb',
            'host'     =>  env('MONGO_DB_HOST', 'localhost'),
            'port'     =>  env('MONGO_DB_PORT', 27017),
            'database' =>  env('MONGO_DB_DATABASE'),
            'username' =>  env('MONGO_DB_USERNAME'),
            'password' =>  env('MONGO_DB_PASSWORD'),
            'options' => []
        ],

Todo.php(Model)

namespace App;

use JenssegersMongodbEloquentModel as Eloquent;

class Todo extends Eloquent
{
    protected $connection = 'mongodb';

    protected $collection = 'todo';

    protected $fillable = [
        'title', 'desc'
    ];
}

.env file

MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=mongocrud
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=

Overview of Error message:


(1/1) AuthenticationException
Authentication failed.

in Find.php line 299
at Server->executeQuery('mongocrud.todo', object(Query), array('readPreference' => object(ReadPreference)))
in Find.php line 299
at Find->execute(object(Server))
in Collection.php line 624
at Collection->find(array(), array('typeMap' => array('root' => 'array', 'document' => 'array'), 'readPreference' => object(ReadPreference), 'readConcern' => object(ReadConcern)))

2

Answers


  1. First delete vendor folder & replace below 2 line in composer.json:

    "jenssegers/mongodb": "^3.5",

    "laravel/framework": "5.8.*",

    to

    "jenssegers/mongodb": "^3.5",

    "laravel/framework": "5.7.*",

    & update composer

    after update the composer clear the config cache.

    Login or Signup to reply.
  2. Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search