Home > Back-end >  Connecting Laravel to MongoDB Atlas
Connecting Laravel to MongoDB Atlas

Time:10-06

So I had a school project I made with Laravel/jenssegers and it worked fine locally, but now I want to connect it to the Atlas cluster as I want to deploy the project with Heroku.

The thing is, I can't manage to get it working. I'm losing my mind. I get this:

No suitable servers found (serverSelectionTryOnce set): [TLS handshake failed: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed calling ismaster on 'cluster0-shard-00-00.mhfxu.mongodb.net:27017']

In my config/database.php, I have this:

 'mongodb' => [
        'driver' => 'mongodb',
        'dsn' => env('DB_URI', 'mongodb srv://username:[email protected]/database?retryWrites=true&w=majority'),
        'database' => env('DB_DATABASE'),
  ],

My cluster also accepts all IPs. What should I do?

CodePudding user response:

Try following

in your .env

MONGO_DB_URI=mongodb srv://mongodbatlasstring (whatever is yours)

in your database.php

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

CodePudding user response:

You can also try following where you can specify replica and all nodes

 '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'
   ]
],
  • Related