5 / 5
Apr 2024

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 …

Hi @Emeric_Petitgenet

Welcome to MongoDB forums!

From the error message, it seems to be a DNS issue - the SRV record resolution is failing. Please check your DNS configuration. You can try to perform a nslookup to manually check DNS resolution.

Additionally, you can cross check by connecting to your Atlas cluster with the same URI on the same machine, via mongosh or MongoDB Compass.

We also published a MongoDB Atlas + Symfony step by step tutorial that should help you as well - https://www.mongodb.com/developer/products/mongodb/mongodb-php-symfony-rental-workshop/

Hi @Rishabh_Bisht ,

thank you for responding ! here what I can say,

  • I can connect from compass it’s working fine
  • I tried to follow step by step rental tutorial and facing the same problem
  • I only struggle with the connexion within symfony project

here is the result of nslookup

me@me ~ % nslookup -type=SRV _mongodb._tcp.easyloc.w7ka7fk.mongodb.net

Server: 2a01:cb15:8020:6900:3e58:5dff:fe0d:12c1

Address: 2a01:cb15:8020:6900:3e58:5dff:fe0d:12c1#53

Non-authoritative answer:

_mongodb._tcp.easyloc.w7ka7fk.mongodb.net service = 0 0 27017 ac-0tjg0l9-shard-00-00.w7ka7fk.mongodb.net.

[…]

Authoritative answers can be found from:

no authoritative answers

regards

I’d like to confirm if it’s really a DNS issue. Can you try switching to seedlist URI and see if it works?

In the connection URI dialog in Atlas, change the driver to an older version to get the seed list URI and try using that in your Symfony application.
Screenshot 2024-04-24 at 3.55.43 PM

Hi @Rishabh_Bisht ,

tried several drivers including PHPLIB.1.1 + mongodb-1.2 or later, it didn’t help neither datas to understand what is going on

I join a screenshot, this is literally what I’m facing with each time I use connection string launching Symfony local web server. No response / error message , juste stuck somewhere between request and answer
Capture d’écran 2024-04-24 à 13.53.58

may this information help : asked someone to clone my github repository, so with the same project and same command, this guy can run the code with no difficulty

regards