Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Blacklisted Fatal Error

Tags:

php

mongodb

I've seen this error a couple of times, and to fix it, I just reboot my server.

Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: Previous connection attempts failed, server blacklisted' in /var/www/html/include/config.php:9 Stack trace: #0 /var/www/html/include/config.php(9): MongoClient->__construct('mongodb://local...') #1 /var/www/html/classes.php(3): include('/var/www/html/i...') #2 /var/www/html/myusers.php(8): include('/var/www/html/c...') #3 {main} thrown in /var/www/html/include/config.php on line 9

However, I can be a while without seeing it... How can I prevent the issue from happening?

update: it happened again and after several minutes of waiting, I had to reboot to make the site work again

like image 457
jSmith Avatar asked Jul 29 '13 08:07

jSmith


2 Answers

Since the 1.4 versions of the MongoDB driver for PHP we will "blacklist" servers for up to a minute if they can not be contacted to. This is so that we do not slam the server with connections, that might timeout. This is primarily done to make sure that in a replica set environment we can still proceed by just using another of the hosts, but of course if you only have one machine, this is a bit trickier.

If you use MongoLog then you can very easily spot what happens under the hood:

MongoLog::setModule(MongoLog::ALL);
MongoLog::setLevel(MongoLog::ALL);
MongoLog::setCallback('print_mongo_log');
function print_mongo_log($a, $b, $c) { echo $c, "\n"; }

This will display everything the driver is trying to do. It would be interesting to see the first dump of when something goes wrong, and also for one time it is "stuck" on the blacklist.

The above warning will go away after 60 seconds, or upon reboot of your web server software (or PHP-FPM is you use that). If you think this explanation is not correct, please file a bug/feature request at http://jira.mongodb.org/browse/PHP

like image 126
Derick Avatar answered Sep 29 '22 02:09

Derick


Apparently, this is an issue caused by a bug in MongoDB's PHP driver. Check if you're using version 1.4.0, if yes, update to a newer version and the error should be fixed.

like image 38
ciruvan Avatar answered Sep 29 '22 01:09

ciruvan