Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb "Remote server has closed the connection" on first connection after restart

Tags:

php

mongodb

After restart of the mongod service I get a connection error for every database when I try to connect with PHP:

Failed to connect to: localhost:27017: Remote server has closed the connection
500 Internal Server Error - MongoConnectionException

After one or two refeshes the connection works again. It doesn't mind if the connection is made immediately after restart or after some time.

MongoDB version: 2.6.4
PHP MongoDB driver version: 1.5.5
Configuration: PHP-FPM with Apache2

I think the persistent connecting of the old mongo instance is used, but I can't find out how to fix this. Is there any way to force the Mongo driver to start a new connection when the first one fails?

like image 599
Marijn Otte Avatar asked Aug 12 '14 10:08

Marijn Otte


People also ask

Does MongoDB close connection automatically?

The answer is no. DB connections don't gracefully shut down when you exit (or crash).

Why is my MongoDB not connecting?

Ensure Your MongoDB Instance is Running Compass must connect to a running MongoDB instance. Make sure you have installed MongoDB and have a running mongod process. You should also check that the port where your MongoDB instance is running matches the port you provide in the Compass connect dialog.

How do I allow remote connections to MongoDB?

To allow remote connections, you must edit the MongoDB configuration file — /etc/mongod. conf — to additionally bind MongoDB to an IP address which can be reached by your trusted remote computer. This way, your MongoDB installation will be able to listen to connections made to your MongoDB server from remote machines.

What is connection pooling in MongoDB?

Definition. A connection pool is a cache of open, ready-to-use database connections maintained by the driver. Your application can seamlessly get connections from the pool, perform operations, and return connections back to the pool. Connection pools are thread-safe.


1 Answers

The solution we now use is that we restart Apache after we restart MongoDB. Not the best solution, but it works :).

like image 63
Marijn Otte Avatar answered Sep 27 '22 16:09

Marijn Otte