Question

Connecting DigitalOcean Managed MongoDB to Laravel (connection error calling ismaster)

Posted July 23, 2021 156 views
MongoDBLaravelDigitalOcean Managed MongoDB Database

I’m attempting to set up Laravel with the Managed MongoDB provided by DigitalOcean, though for some reason the database is not connecting.

I’ve hit a wall and I think it’s something to do with authSource, but can’t replicate it via cli otherwise…

.env file

MONGO_DSN="mongodb+srv://username:password@digitaloceanhostname/databasename?authSource=admin"
MONGO_DATABASE="databasename"
MONGO_USER="username"
MONGO_PASSWORD="password"
MONGO_TLS=true 
MONGO_TLS_CERT="./mongo-db-cert.crt"

config/database.php

'connectionmethod' => [
    'driver' => 'mongodb',
    'dsn' => env('MONGO_DSN'),
    'database' => env('MONGO_DATABASE', ''),
    'username' => env('MONGO_USER', ''),
    'password' => env('MONGO_PASSWORD', ''),
    'options'  => [
        'tls' => (bool) env('MONGO_TLS', false),
        'tlsCAFile' => env('MONGO_TLS_CERT', null),
        'authSource' => 'admin',
        'db' => 'admin',
        'database' => 'admin',
    ],
],

The above causes the following error:

ERROR: No suitable servers found (`serverSelectionTryOnce` set): [connection error calling ismaster on 'digitaloceanhostname'
(MongoDB\\Driver\\Exception\\ConnectionTimeoutException(code: 13053): No suitable servers found (`serverSelectionTryOnce` set): [connection error calling ismaster on 'digitaloceanhostname:27017']

However, when using the CLI to call ismaster, it works:

# mongo "mongodb+srv://MONGO_USER:MONGO_PASSWORD@digitaloceanhostname/MONGO_DATABASE?authSource=admin" --eval 'printjson(db.runCommand({"isMaster": 1}))' --ssl --sslCAFile ./mongo-db-cert.crt

Running it without the ?authSource=admin causes an authentication error, which is making me think the connection error from laravel is the same thing.

Environment:

# mongo --version
MongoDB shell version v3.6.8

# apt list --installed | grep php | grep mongo
php7.4-mongodb/focal,now 1.9.0+1.7.5-6+ubuntu20.04.1+deb.sury.org+1 amd64 [installed]

# php artisan --version
Laravel Framework 5.8.38

I’m probably missing something obvious, but after looking at this all day any input is appreciated!

edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

I’ve just tested this and I was able to get it working by removing the ?authSource=admin from the database URL in the .env file.

Hope that this helps.

Regards,
Bobby