Connect laravel jenssegers to mongodb atlas cluster

Viewed 10277

I'am starting with Mongodb atlas and i trying to connect my laravel/jenssegers project to the cluster i config my conf/database

'mongodb' => [
  'driver'   => 'mongodb',
  'host'     => env('DB_HOST'),
  '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
  ]
  ],

And my .env file

DB_HOST="xxxx-shard-00-00-uggj0.mongodb.net"
DB_PORT=27017
DB_DATABASE=xxx
DB_USERNAME=xxx
DB_PASSWORD=xxx

And i get this error

No suitable servers found (serverSelectionTryOnce set): [connection closed calling ismaster on 'xxxx-shard-00-00-uggj0.mongodb.net:27017'

I cold conect with Mongodb Compass without problem.

My Atlas Ip Whitelist is open (0.0.0.0/0).

Am I missing something?

4 Answers

Following works for me, MongoDB Atlas and Laravel 5.7:

update config/database.php:

'mongodb' => [
            'driver' => 'mongodb',
            'dsn' => 'mongodb+srv://<username>:<password>@cluster0.kxxi7.mongodb.net/mydatabase?retryWrites=true&w=majority',
            'database' => 'mydatabase',
        ],

Set your connection string at dsn and db name at database.

By default, when assembling the connection string, the Jenssegers\Mongodb package assembles a DSN that starts with mongodb://. In order to connect to a mongoDB atlas instance, the DSN needs to start with mongodb+srv://.

The simplest and most flexible way to handle this is to add the following entry to your .env:

MONGO_DB_BASE_URI=mongodb+srv://

Then, in your database.php, your mongodb entry should look like the following:

    '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' => [],
        'dsn' => env('MONGO_DB_BASE_URI','mongodb://').env('MONGO_DB_HOST', 'localhost')
    ],

This gives you control over whether you're connecting to a local instance - in which case you can use a DSN of mongodb://, or a cloud instance such as Atlas - in which case you use a DSN of mongodb+srv://.

 'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => [
                       'abc-00-00.mongodb.net',
                        'abc-00-01.mongodb.net',
                        'abc-00-02.mongodb.net'
                    ],
        'port'     => env('MONGO_DB_PORT', 27017),
        'database' => env('MONGO_DB_DATABASE'),
        'username' => env('MONGO_DB_USERNAME'),
        'password' => env('MONGO_DB_PASSWORD'),
        'options'  => [
            'ssl'        => 'true',
            'replicaSet' => 'abc-0',
            'authSource' => 'admin',
            'retryWrites' => 'true', 
           'w' => 'majority'
       ]
    ],

This works for laravel 6 and MongoDB 1.2 or later with MongoDB atlas

Related