N
N
NIKA_R2021-04-13 22:17:58
MongoDB
NIKA_R, 2021-04-13 22:17:58

How to connect MongoDB Atlas Cloud in Laravel 8?

We have:
1. MongoDB Atlas Cloud cluster Cluster0; (Free)
2. Arch Linux for
PHP 8.0.3 development (cli) (built: Mar 4 2021 05:33:14
Composer version 2.0.11
MongoDB PHP driver
jenssegers/mongodb https://github.com/rykercodes/laravel- mongodb-8.0
3. https://replit.com (to test, at least on Node this problem does not exist)

Task:
Build backend on laravel resfull api and mongodb cloud

Problem:
Unable to connect to cluster

cloud.mongodb.com

Network Access
0.0 .0.0/0
(includes your current IP address)

Database Access
user_test
Created a user with the rights Read and write to any database

Clusters Collections
Created a database to connect to

php -i | grep -i mongo

Additional .ini files parsed => /etc/php/conf.d/mongodb.ini
mongodb
MongoDB support => enabled
MongoDB extension version => 1.9.0
MongoDB extension stability => stable
libmongoc bundled version => 1.17. 2
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => enabled
libmongoc ICU => enabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
libmongoc compression zstd => enabled
libmongocrypt bundled version => 1.0.4
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto
mongodb.debug => no value => no value

All ok .

app.php

Jenssegers\Mongodb\MongodbServiceProvider::class,

Everything is ok.

database.php

'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'cluster0.kfhlz.mongodb.net'),
'port' => env('DB_PORT' , 27017),
'database' => env('DB_DATABASE',
'password' => env('DB_PASSWORD', '*******'),
'driver' => 'mongodb',
'dsn' => ' mongodb://cluster0.kfhlz.mongodb.net:27017 ',
'options' => ['database' => env('*************')],
],

I doubt about dns and port very much.

.env

DB_CONNECTION=mongodb
DB_HOST=cluster0.kfhlz.mongodb.net
DB_PORT=27017
DB_DATABASE=***********
DB_USERNAME=***********
DB_PASSWORD=***********

php artisan migrate

MongoDB\Driver\Exception\ConnectionTimeoutException
No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'cluster0.kfhlz.mongodb.

6076d5384c1de157736935.png


Through Mongo shell everything is ok.
6076ed26ec3ab590488452.png

6076edbaebb9f059841478.png

Problem solved. Correct config files.

'mongodb' => [
    'driver' => 'mongodb',
    'host' => env('DB_HOST','***-shard-**-**.kfhlz.mongodb.net' ),
    'port' => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'password' => env('DB_PASSWORD'),
    'dsn'=>'mongodb://user_test:[email protected]****-shard-**-**.kfhlz.mongodb.net:27017,***-shard-**-01.kfhlz.mongodb.net:27017,***shard-****.kfhlz.mongodb.net:27017/fracsia_db?ssl=true&replicaSet=atlas-****-shard-0&authSource=admin&retryWrites=true&w=majority',
    'options' => [
        'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
        'options' => [
            'replicaSet' => 'rs0',
        ],
    ],
    ],
    ],


DB_CONNECTION=mongodb
DB_HOST="cluster0-shard-00-00.kfhlz.mongodb.net"
DB_PORT=27017
DB_DATABASE=****
DB_USERNAME=****
DB_PASSWORD=*****


6077379015890480646445.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
ZB Venom, 2021-04-13
@zb_venom

First link on google

'mongodb' => [
        'driver' => 'mongodb',
        'dsn'=>'mongodb://username:[email protected]/database?ssl=true&replicaSet=replicaSet&authSource=admin',
        'database' => 'my_data',
    ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question