Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MongoDB fatal error: Read timed out

Tags:

mongodb-php

sometimes there is a bug (~30%):

Failed to connect to: IP:2333: Read timed out after reading 0 bytes, waited for 0.000000 seconds

  • Web server Apache/2.2.16
  • PHP version PHP 5.3.3-7+squeeze15
  • PHP extension mongo/1.4.0
  • MongoDB version 2.2.3

In all cases. What is this?

like image 383
nu11 Avatar asked Jul 10 '13 17:07

nu11


People also ask

How to fix the timeout error in MongoDB?

When the client takes too much time to connect to MongodB server than the preset timeout value, it can result in error. And the fix involves in raising the timeout limits on the MongoDB client. For this, we first check with the customer on the settings that they use on their client. We then compare the values set in the MongoDB server.

What to do when MongoDB client takes too long to connect?

When the client takes too much time to connect to MongodB server than the preset timeout value, it can result in error. And the fix involves in raising the timeout limits on the MongoDB client. For this, we first check with the customer on the settings that they use on their client.

Why am I getting connection drop error in MongoDB?

This error is most common with MongoDB & happens when the connection gets dropped by the firewall. So, to solve the problem our Support Engineers opened the firewall settings on the server side and allowed the corresponding IP to allow connections.

Why does my Mongo log show intermittent connection failure?

It may be intermittent and not happen all the time. But from the drivers perspective it is timing out trying to select a server. The log message just shows you the current view of the servers after selection has failed, which is it is in the process of connecting to the mongo1 node.


1 Answers

I had the exact same bug. The solution is to set a timeout for your requests.

  • You can do a $cursor->timeout(-1); on each of your cursors.

  • Or simply add MongoCursor::$timeout = -1; in your index/connection file.

-1 is to set an infinite timeout. You can also put a value in milliseconds if you don't want infinite.

like image 174
elbajo Avatar answered Sep 20 '22 03:09

elbajo