Trying since days to connect mongodb atlas to symfony project using MongoDB php extension and MongoDB PHP Driver + mongodb-odm-bundle for Symfony,
here are the main files
.env
###> doctrine/mongodb-odm-bundle ###
MONGODB_URL='mongodb+srv://myuser:mymdp@easyloc.w7ka7fk.mongodb.net/?retryWrites=true&w=majority&appName=easyloc'
MONGODB_DB=mydb
###< doctrine/mongodb-odm-bundle ###
doctrine_mongodb.yaml
doctrine_mongodb:
auto_generate_proxy_classes: true
auto_generate_hydrator_classes: true
connections:
default:
server: '%env(resolve:MONGODB_URL)%'
options: {}
default_database: '%env(resolve:MONGODB_DB)%'
document_managers:
default:
auto_mapping: true
mappings:
App:
dir: '%kernel.project_dir%/src/Document'
prefix: 'App\Document'
mapping: true
type: attribute
is_bundle: false
alias: App
when@prod:
doctrine_mongodb:
auto_generate_proxy_classes: false
auto_generate_hydrator_classes: false
document_managers:
default:
metadata_cache_driver:
type: service
id: doctrine_mongodb.system_cache_pool
framework:
cache:
pools:
doctrine_mongodb.system_cache_pool:
adapter: cache.system
services.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
env(MONGODB_URL): ''
env(MONGODB_DB): ''
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\Service\MongoDbClient:
public: true
MongoDbClient.php
<?php
namespace App\Service;
use MongoDB\Client;
class MongoDbClient
{
private $client;
public function __construct()
{
$uri = 'mongodb+srv://myadmin:mymdp@easyloc.w7ka7fk.mongodb.net/?retryWrites=true&w=majority&appName=easyloc';
$options = [
'serverApi' => [
'version' => 1,
'strict' => true,
],
];
$this->client = new Client($uri, $options);
}
public function getClient()
{
return $this->client;
}
}
DefaultController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use App\Service\MongoDbClient;
class DefaultController extends AbstractController
{
private $mongoDBClient;
public function __construct(MongoDbClient $mongoDBClient)
{
$this->mongoDBClient = $mongoDBClient;
}
public function index(): Response
{
$client = $this->mongoDBClient->getClient();
try {
$client->selectDatabase('sample_training')->command(['ping' => 1]);
$message = "Pinged your deployment. You successfully connected to MongoDB!";
} catch (\Exception $e) {
$message = $e->getMessage();
}
return new Response($message);
}
}
got this error message on the terminal,
emericp@mbp-de-emeric monprojet % symfony server:log
Following Web Server log file (/Users/emericp/.symfony5/log/9e98f4d5ef0deaa3b24ff7a1f87a25093c924fcc.log)
Following PHP-FPM log file (/Users/emericp/.symfony5/log/9e98f4d5ef0deaa3b24ff7a1f87a25093c924fcc/53fb8ec204547646acb3461995e4da5a54cc7575.log)
[Web Server ] Apr 21 22:42:47 |ERROR | SERVER GET (500) /maroute ip="127.0.0.1"
[PHP-FPM ] [21-Apr-2024 20:42:47 UTC] [critical] Uncaught Exception: Failed to parse URI options: Failed to look up SRV record "_mongodb._tcp.easyloc.w7ka7fk.mongodb.net": A temporary error occurred on an authoritative name server. Try again later.
hope someone have a clue on what’s happening here …