Question

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

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!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
July 28, 2021
Accepted 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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel