doctrine / DoctrineMongoDBBundle

Integrates Doctrine MongoDB ODM with Symfony
http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html
MIT License
382 stars 230 forks source link

Wrongly connecting to localhost, by default #495

Closed Djyp closed 6 years ago

Djyp commented 6 years ago

Hello everyone,

I was charged with putting a project in production. The project is using this bundle (so it's using doctrine/mongodb-odm and doctrine/mongodb as well). The project is under Symfony 4.1 and doesn't use much bundles. Just this one, OldSoundRabbitMqBundle and GuzzleHttp.

The problem

I had a hard time to make it work properly. Every launch of our command was throwing a "Connection refused" error, saying it was unable to reach mongodb://localhost:27017... of course ! I've configured my doctrine.mongodb.yaml with a connection on a different server ! There were no way localhost should have been called, ever. It took me a while to understand what happened...

What happened

The code creates a new Connection() object and the use the DocumentManager to store documents. I discovered that the Connection class at line 267 was forcing the connection to localhost:27017 if nothing was specified to the constructor. I managed to solve the problem by creating the object like this : new Connection(getenv(MONGODB_URL)).

While the developer was using MongoDB on his machine, he never figured there was any problem.

I browsed documentations, github repos, blogs,... you name it, it was Googled and read twice or more... I checked the configurations, made sur the .env vars were loaded, the YAML config files were read and correctly formed. I figured out the situation by testing the whole process line by line, through our code and the bundles'.

My questions are...

Did the developer used the proper way to connect to MongoDB through this Bundle ? Don't you think it's a concern that the Connection class uses localhost by default ? (I know, it's on another repo).

Thanks for you answers

malarzm commented 6 years ago

The code creates a new Connection() object and the use the DocumentManager to store documents.

What code creates the connection? Is this bundle's code?

FWIW the default mongodb://localhost:27017 is also used by underlying ext/mongo so Doctrine's Connection is only mimicking lower level defaults.

EDIT: link to \MongoClient docs I forgot to paste earlier

Djyp commented 6 years ago

It's our project that uses the Connection class. And I'm wondering if it's wrong to connect to MongoDb like this.

malarzm commented 6 years ago

It's your bug then. You should explicitly specify where you want to connect (so new Connection(getenv(MONGODB_URL)) as per your comment). The Connection can't know anything about env variables on its own. Now to answer your questions:

Did the developer used the proper way to connect to MongoDB through this Bundle ?

I'm not sure why did the developer did new Connection on his own, but due to this I'm inclined to say "No". One should specify connection options through bundle's configuration and then get connections from DIC, not create them manually.

Don't you think it's a concern that the Connection class uses localhost by default ? (I know, it's on another repo).

No, as I said previously we're mimicking what the lower level driver does.

Djyp commented 6 years ago

Thanks a lot for your answers ! :+1: Really helpful !