Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ryank30's avatar

Authentication failed on mongodb

Hi,

I just have installed jenssegers/laravel-mongodb and try to insert a test record but I am facing "Authentication failed" error. Please see my codes below:

In my controller:

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use Form; use App\Account; class ProjectController extends Controller { public function process(Account $account) { $data = array('user_id' => 1, 'title' => 'first record', 'dataset' => array('first_name' => 'Ryan', 'last_name' => 'Rivers', 'email' => 'rivers@gmail.com')); $account->create($data); return 'done'; } }

In my model:

namespace App; use Jenssegers\Mongodb\Eloquent\Model as Eloquent; use DB; class Account extends Eloquent { protected $connection = 'mongodb'; protected $collection = 'accounts'; protected $fillable = ['user_id', 'title', 'dataset', 'first_name', 'last_name', 'email']; }

In my config/database.php:

    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE2', 'database'),
        'username' => env('DB_USERNAME2', 'user'),
        'password' => env('DB_PASSWORD2', 'pass'),
        'options' => [
            'db' => 'admin' // sets the authentication database required by mongo 3
        ]
    ],  

Could you show me an example of how I can enable authentication please? I thought my database config file would take care of authentication automatically. It would be appreciated if you could help me how to go about it.

0 likes
0 replies

Please or to participate in this conversation.