0

I want to connect my laravel 5.7 application(I used the 3.4 version of jenssegers/mongodb) with a mongodb in atlas, I tried in localhost(I isntalled the mongo extension) and everything is ok but with atlas i got an error message:

Failed to parse MongoDB URI: 'mongodb://root%3Acluster0.xxx.mongodb.net%3A27017%2Fhddatabase%3FretryWrites%3Dtrue%26w%3Dmajority'. Invalid host string in URI.

My env file:

DB_CONNECTION=mongodb
DB_DSN="mongodb://root:password@cluster0.xxx.mongodb.net:27017/hddatabase?retryWrites=true&w=majority"
DB_DATABASE=hddatabase

My database config:

'mongodb' => [
    'driver'   => 'mongodb',
    'dsn' => env('DB_DSN'),
    'database' => env('DB_DATABASE'),
],
SAFSAF
  • 375
  • 1
  • 4
  • 14

3 Answers3

1

TL;DR: The url is wrong. "+srv" part is missing. Please copy the url from the Atlas Connection Wizard:

enter image description here

You can open the connection from the Cluster View:

enter image description here

Details

Atlas provides a replica set cluster of 3 mongo db instances by default. Your local database is standalone.

There are 2 formats to connect to mongodb:

  • mongodb:// is the legacy one that requires all members of the replica set to be explicitly present in the url. E.g.: mongodb://<username>:<password>@cluster0-shard-00-00.xxx.mongodb.net:27017,cluster0-shard-00-01.xxx.mongodb.net:27017,cluster0-shard-00-02.xxx.mongodb.net:27017/<dbname>?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority
  • mongodb+srv:// is the "srv" format which lets the driver to retrieve replica set configuration from mongodb itself, so the url is much simpler: mongodb+srv://<username>:<password>@cluster0.xxx.mongodb.net/<dbname>?retryWrites=true&w=majority

You are using the later format and the legacy schema so the driver complains.

As a side note it is advisable to use configuration options username and password as documented in https://github.com/jenssegers/laravel-mongodb#configuration rather than pass them in the url. URL format requires special characters like @, /, etc in the password to be escaped, which unnecessarily complicates credentials management.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
0

Try with this DNS maybe :

mongodb://root:password@cluster0.xxx.mongodb.net:27017

Tahar Chibane
  • 51
  • 1
  • 7
  • Try maybe : `mongodb://root:password@cluster0.xxx.mongodb.net:27017/your_database?ssl=true&replicaSet=replicaSet&authSource=admin` – Tahar Chibane Jan 14 '21 at 09:28
0

Even with the 'srv' format I'm getting the same error, The problem is that I'm using the version 3.4.0 of jenssegers/mongodb, I upgraded to version 3.4.6 and used the 'srv' format and problem is solved now !

SAFSAF
  • 375
  • 1
  • 4
  • 14
  • I seriously doubt version of jenssergers' binding matters. The error comes from php mongodb driver library https://github.com/mongodb/mongo-php-driver/blob/5e2f7b5461a89706e0aece15e909a9b02757f7bb/php_phongo.c#L1330, triggered by the parser in libmongoc. It's likely the upgrade just cleared the config cache. – Alex Blex Jan 15 '21 at 13:27
  • I downgraded from 3.4.6 to 3.4.0 and i'm getting the error without editing the configuration – SAFSAF Jan 15 '21 at 13:36
  • It's fascinating how these few changes between 3.4.0 and 3.4.6 https://github.com/jenssegers/laravel-mongodb/compare/v3.4.0...jenssegers:v3.4.6 made such a dramatic impact. Any way glad you have the problem solved. Regardless of the issue it's always recommended to use latest version available. – Alex Blex Jan 15 '21 at 14:13
  • I guess it's the getDsnString function here: https://github.com/jenssegers/laravel-mongodb/compare/v3.4.0...jenssegers:v3.4.6#diff-8ff28ad0fe8a9483b04535d7086cad10e781c2d2798db14be7257a295bfecc97 – SAFSAF Jan 15 '21 at 18:46
  • It is indeed. It seem the dsn drama lasts for 3 years across multiple versions of the package =( Good catch! – Alex Blex Jan 18 '21 at 10:04