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
495 views
in Technique[技术] by (71.8m points)

php - Connect laravel jenssegers to mongodb atlas cluster

I'am starting with Mongodb atlas and i trying to connect my laravel/jenssegers project to the cluster i config my conf/database

'mongodb' => [
  'driver'   => 'mongodb',
  'host'     => env('DB_HOST'),
  'port'     => env('DB_PORT', '27017'),
  'database' => env('DB_DATABASE'),
  'username' => env('DB_USERNAME'),
  'password' => env('DB_PASSWORD'),
  'options'  => [
      'database' => 'admin' // sets the authentication database required by mongo 3
  ]
  ],

And my .env file

DB_HOST="xxxx-shard-00-00-uggj0.mongodb.net"
DB_PORT=27017
DB_DATABASE=xxx
DB_USERNAME=xxx
DB_PASSWORD=xxx

And i get this error

No suitable servers found (serverSelectionTryOnce set): [connection closed calling ismaster on 'xxxx-shard-00-00-uggj0.mongodb.net:27017'

I cold conect with Mongodb Compass without problem.

My Atlas Ip Whitelist is open (0.0.0.0/0).

Am I missing something?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

In Laravel, Use 'dsn' key in config/database.php as shown below to mention the complete cluster url.

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

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

...