Authentication Failed when Lumen is connected to MongoDB Atlas

Viewed 183

I am new to MongoDB Atlas and am having trouble connecting Lumen with the mongoDB atlas cluster. the error message I received was Authentication failed.

enter image description here

the code I use to connect is as follows:

'mongodb_atlas' => [
      'driver'   => 'mongodb',
      'dsn'      => 'mongodb+srv://myusername:mypassword@mycluster.99xxx.mongodb.net/mydbname?retryWrites=true&w=majority',
      'database' => 'mydbname',
    ]

even though the username and password that I entered in dsn match those in MongoDB Atlas. is there something wrong with my code? how is the solution? I am very grateful if anyone can help.

note:

Lumen that I use version 8.x, and PHP version 8.x

1 Answers

I assume you are using jenssegers/laravel-mongodb package to connect to MongoDB.

You can check the structure for MongoDB DSN for standalone, shared cluster and replica set in the MongoDB docs.

To connect to MongoDB Atlas the dsn is constructed as follows

'mongodb_conn' => [
  'driver' => 'mongodb',
  'dsn'=>'mongodb://username:password@host1,host2/defaultauthdb?ssl=true&replicaSet=replicaSet&authSource=admin',
  'database' => 'my_data',
]

You're missing the ssl=true param, but that needs to be supported by your server. So have an eye on that.

I know that you're using +srv to use TLS/SSL connection but maybe the other format from above works better.

Related