Имеем:
1. MongoDB Atlas Cloud кластер Cluster0; (Free)
2. Arch Linux для разработки
PHP 8.0.3 (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 (теститировать, как минимум на Node данной проблемы нет)
Задача.
Собрать backend на laravel resfull api и mongodb cloud.
Проблема.
Невозможно подключиться к кластеру.
cloud.mongodb.com
Network Access
0.0.0.0/0
(includes your current IP address)
Database Access
user_test
Создан пользователь с правами Read and write to any database
Clusters Collections
Создали базу к которой подключаемся
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
Все ок.
app.php
Jenssegers\Mongodb\MongodbServiceProvider::class,
Все ок.
database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'cluster0.kfhlz.mongodb.net'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', '*********'),
'username' => env('DB_USERNAME', '****'),
'password' => env('DB_PASSWORD', '*******'),
'driver' => 'mongodb',
'dsn' => '
mongodb://cluster0.kfhlz.mongodb.net:27017',
'options' => ['database' => env('*********')],
],
Сомневаюсь по поводу dns и порта очень.
.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.net']
В гуугл не отправлять… Тихий ужас.
Через Mongo shell все ок.
Проблема решена. Правильные конфигурационные файлы .
'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:viriga@****-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=*****