Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect laravel jenssegers to mongodb atlas cluster

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?

like image 266
Fernando Preciado Avatar asked Jul 07 '17 21:07

Fernando Preciado


People also ask

How do I connect to a MongoDB cluster?

To connect to your MongoDB database, open MongoDB Compass, then click New Connection in the left navigation menu. In the Paste your connection string field, paste the connection string provided by DigitalOcean into the field.


1 Answers

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',
    ]
like image 167
saurabh Avatar answered Sep 23 '22 03:09

saurabh