Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

php - Authentication Failed when Lumen is connected to MongoDB Atlas

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:[email protected]/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

question from:https://stackoverflow.com/questions/65857716/authentication-failed-when-lumen-is-connected-to-mongodb-atlas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...