laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

No suitable servers found (`serverSelectionTryOnce` set)

Open MichelBras opened this issue 3 years ago • 1 comments

Hi people,

I try to connect with a mongoDB database server hosted on Digital Ocean.

Context

I have a Laravel app that has a MongoDB connection. I develop locally and code is pushed to the testenvironment that is hosted on Digital Ocean. This testenvironment has a connection with an external MongoDB server. I want to connect with this database server for my local development, because i'm only fetching data from the MongoDB. The thing is that my local environment can connect with my local MongoDB and my testenvironment with the external MongoDB server, but when tying to connect my local environment with the external DB server I don't get the connection working.

Specs:

  • This MongoDB server version is 4.4.11.
  • I use Laravel valet 2.18.4
  • PHP 8.0
  • mongo CLI version 5.0.6
  • mongorestore version 100.5.2
  • Laravel framework version 8.6.11
  • Jenssegers Mongo db package version 3.8.4
  • Macbook pro 2021 m1

I can connect via the following

I can connect with my macbook using the mongo cli:

mongo "mongodb+srv://doadmin:<my-secret-password>@<my-database-host>/admin?authSource=admin&replicaSet=db-doorgevert" --tls --tlsCAFile=ca-certificate.crt

I also can connect with MongoDb Compass with the connection params that I fetched from the Digital Ocean GUI.

username = doadmin
password = <replace-with-your-password>
host = <my-database-host>
port = 27017
database = admin
protocol = mongodb+srv

I also can connect with the same database I installed locally via mongorestore --gzip --archive=<file.gz.partaa> with the following Laravel configuration:

my config/database.php file:

        'mongodb' => [
            'driver' => 'mongodb',
            'dsn' => env('DB_URI', 'mongodb+srv://username:password@<atlas-cluster-uri>/myappdb?retryWrites=true&w=majority'),
            'database' => 'configurator',
        ],

my .env file:

DB_URI="mongodb://127.0.0.1:27017/?readPreference=primary&serverSelectionTimeoutMS=2000&appname=mongosh%201.2.3&directConnection=true&ssl=false"

But I need to connect to the database server

But when using my Laravel application I just can't get it connected to the database server. This is my Laravel configuration:

my config/database.php file:

        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('MONGO_HOST', '127.0.0.1'),
            'port' => env('MONGO_PORT', 27017),
            // 'dsn' => env('DB_URI', 'mongodb+srv://username:password@<atlas-cluster-uri>/myappdb?retryWrites=true&w=majority'),
            'database' => env('MONGO_DATABASE', 'forge'),
            'username' => env('MONGO_USERNAME', 'root'),
            'password' => env('MONGO_PASSWORD', 'root'),
            'options' => [
                'replicaSet' => 'db-doorgevert',
                'tls' => 'true',
                'tlsCAFile' => storage_path('ca-certificate.crt'),
            ],
        ],

You can see that i tried to use dsn as well, using that variable resulted into the same error that I received.

my .env file:

DB_CONNECTION_MONGO=mongodb
MONGO_HOST=<my-database-host>
MONGO_PORT=27017
MONGO_DATABASE=admin
MONGO_USERNAME=doadmin
MONGO_PASSWORD=<my-secret-password>

This gave me the following error:

   MongoDB\Driver\Exception\ConnectionTimeoutException

  No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve '<my-database-host>]

Conclusion

I hope you guys can help me with this configuration.

Hope you also have a nice day ofc!

MichelBras avatar Mar 25 '22 12:03 MichelBras

@MichelBras I had the same issue & struggling to connect with the Atlas instance. After lots of debugging, including this package, I noticed that the package does not use the defined username & password from DSN URI but the env/config values.

The default DSN URL will throw this No suitable servers error for the atlas instance connection.

I was able to fix those problems by including the replica hosts in DSN and username, and password in the env.

Here's my config looks like

DB_CONNECTION=mongodb DB_DSN="mongodb://{SET_OF_HOSTS}/?ssl=true&replicaSet={REPLICA_NAME}&readPreference=primary&connectTimeoutMS=10000&authSource=admin" DB_DATABASE=YOURS DB_USERNAME=YOURS DB_PASSWORD=YOURS

You can generate the DSN URL from atlas using the attached config

Hope it helps.

mahmudz avatar Jun 25 '22 10:06 mahmudz

You can setup in file app/database.php with infomation blow (please change your info): 'mongodb' => [ 'driver' => 'mongodb', 'host' => [ 'ac-xxxx.mongodb.net:27017', 'ac-xxxx.mongodb.net:27017', 'ac-xxxx.mongodb.net:27017', ], 'port' => env('MONGO_DB_PORT', 27017), 'database' => env('MONGO_DB_DATABASE'), // your database 'username' => env('MONGO_DB_USERNAME'), // your usename 'password' => env('MONGO_DB_PASSWORD'), // your password 'options' => [ 'ssl' => 'true', 'replicaSet' => 'axxxxxx-0', //alias replica 'authSource' => 'admin', 'retryWrites' => 'true', 'w' => 'majority' ] ],

image

chauanthuan avatar Oct 13 '23 11:10 chauanthuan