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!
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!
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.