7

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?

KaoriYui
  • 912
  • 1
  • 13
  • 43
Fernando Preciado
  • 195
  • 2
  • 4
  • 13
  • You need the entire connection string from Atlas, containing all replica set nodes. Also, as an aside: with all of the recent mongodb hacks, and the fact Atlas sets up a mandatory IP whitelist, I'm surprised you simply opened it up to all traffic. – David Makogon Jul 07 '17 at 21:07
  • Where i need to put the atlas string? Well i'm trying first to obtain connection and then secure the cluster – Fernando Preciado Jul 07 '17 at 21:12
  • Try putting the entire host string in `DB_HOST` - remember, every Atlas deployment is on a replica set - you need all hosts. – David Makogon Jul 07 '17 at 21:17
  • i get >(1/1) AuthenticationException Authentication failed. But password and databace are corret. – Fernando Preciado Jul 07 '17 at 21:45

4 Answers4

27

In Laravel, Use 'dsn' key in config/database.php as shown below to mention the complete cluster url.

'mongodb_conn' => [
        'driver' => 'mongodb',
        'dsn'=>'mongodb://username:password@host1,host2/database?ssl=true&replicaSet=replicaSet&authSource=admin',
        'database' => 'my_data',
    ]
saurabh
  • 601
  • 6
  • 8
  • You need to have SSL enabled on your server in order for this to work. – Randall Clintsman Jul 22 '20 at 18:17
  • Prior to moving to Atlas this was not necessary for us. We used Mlab for years and the settings in the .env file were sufficient. After switching to Atlas, the Laravel project would not write to the MongoDB tables in the Atlas cluster - until I used these settings in the config/database.php file. Thanks! – Byterbit Nov 04 '20 at 12:53
  • @RandallClintsman Did you mean ssl on mongodb server or application server. ? – Mukesh Joshi Dec 13 '21 at 12:41
3

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.

Prashant
  • 5,331
  • 10
  • 44
  • 59
2

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://.

Brian Swift
  • 5,756
  • 2
  • 14
  • 8
  • 1
    I didn't expect this to work since I didn't knew I was using mongoDB atlas instance. But it did work! – buryo Apr 22 '21 at 20:42
0
 '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

RASEL RANA
  • 2,112
  • 1
  • 15
  • 17