1

I am planning to use MongoDb along with my Laravel project. I am using the jensseger/mongodb extension for it. I have configured everything. I am getting an error saying mongodb not configured. This is how my database.php file looks like:

'connections' => [
        # Our primary database connection
        'mongodb' => [
            'driver'    => 'mongodb',
            'host'      => env('DB_HOST'),
            'database'  => env('DB_DATABASE'),
            'username'  => env('DB_USERNAME'),
            'password'  => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,

        ],

4 Answers 4

4

Double check your configuration in config/database.php

Also make sure that you fire php artisan config:cache after doing the changes.

Sign up to request clarification or add additional context in comments.

2 Comments

php artisan cache:clear
Also in my instance the models included: protected connection = "mongodb"
0

Be sure to have DB_CONNECTION=mongodb set in your .env file or just replace 'default' => env('DB_CONNECTION', 'mysql') with 'default' => 'mongodb'. I would go with the former, as it's safer to rely only on the .env, rather than hardcoding your default database connection.

2 Comments

That is already done. I tries a lot more things and the connection is now established. The problem now is that I have a authentication enabled in mongo, but when I am trying to access the db through Laravel it is showing error while passing user name and password but without it, it is able to access the db
Have you tried php artisan cache:reset? The error you are having with the authentication now seems to be a cache issue, otherwise post your mongodb configuration.
0
   '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'  => ['database' => 'Database_Name']
    ],

After clear cache "php artisan config:cache"

Comments

-1

go to .env Add these lines:

MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=your database name
MONGO_DB_USERNAME=your database username if you have
MONGO_DB_PASSWORD=your database password if you have

1 Comment

Please explain your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.