vendredi 23 juillet 2021

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!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire