4

The last couple of days, my error log has been filled with this error:

send_package: error reading from socket: The socket is closed

I really don't know where this is coming from. It would seem that my MongoDB server is not opening the TCP socket, but I really am just guessing.

Has anyone seen this error before or know how to handle it?

The line generating the error is:

$mongo = new Mongo("mongodb://user:pwd@host/db",array('timeout'=>6000));

I'm also occasionally getting in from within the Pimple DIC:

    class HurstDI extends \Pimple
    {
        public function __construct(){
            $this['mongoUser'] = 'user';
            $this['mongoPwd'] = 'pwd';
            $this['mongoHost'] = "host/db";
            $this['mongoTimeout'] = 6000;
            $this['mongodb'] = function($c){
            return new \MongoClient("mongodb://{$c['mongoUser']}:{$c['mongoPwd']}@{$c['mongoHost']}");
        };
    }
    }
3
  • What are the logs from mongodb saying? does it restart from time to time? Commented Jan 10, 2013 at 2:10
  • Well, I'd looked at the logs, but guess I didn't go back far enough. I'm seeing a lot of codeWed Jan 9 18:18:11 [initandlisten] connection accepted from 10.158.26.40:52664 #17298 Wed Jan 9 18:18:11 [initandlisten] connection refused because too many open connections: 819 Wed Jan 9 18:18:11 [initandlisten] connection accepted from 10.29.133.149:56104 #17299 Wed Jan 9 18:18:11 [initandlisten] connection refused because too many open connections: 819 Commented Jan 10, 2013 at 2:15
  • This SO post may be helpful stackoverflow.com/questions/7693989/… Commented Jan 10, 2013 at 2:20

2 Answers 2

7

There is a known issue with PHP/mongoclient + Apache + MongoDB where invalid persistent connections are held open by the Apache process.

Try to restart your Apache web server.

What happens is:

  • Apache opens a connection to your MongoDB server during a normal request.
  • Presumably, at some point you have restarted your MongoDB server
  • Apache/PHP never recognize that the connection was closed during the MongoDB restart and hold on to the persistent connections opened previously

The only way to get past this issue is to restart Apache (forcing it to kill all of the worker threads and create new connections).

Let me know if this works for you.

Sign up to request clarification or add additional context in comments.

5 Comments

Other today when the issue really started happening I haven't restarted the mongo server. Is this a cumulative issue where apache/php aren't releasing the connections? Or does this only come up if the mongo server is restarted?
@adear11 It starts when the MongoDB server is restarted, but it continues until the Apache service is restarted
I just restarted my apache servers, and it seems to have corrected itself. Is the solution to have this not happen again to not restart the MongoDB server? Would it better to not use persistent connections (if that is an option in the PHP driver)?
@adear11 It's not an option in the PHP driver anymore, but I believe, based on this link that this is fixed in driver versions > 1.30 - so maybe see if you can upgrade? If not, you can gracefully restart apache after MongoDB is restarted. Although it allows for a possibility that some connections are still broken.
I believe I still meet same issue in 1.4.5. I meet same error message and problems resolved after restart apache.
0

Increasing timeouts may help.

  • "socketTimeoutMS" : How long a send or receive on a socket can take before timing out.
  • "wTimeoutMS" : It controls how many milliseconds the server waits for the write concern to be satisfied.
  • "connectTimeoutMS" : How long a connection can take to be opened before timing out in milliseconds.

    $m = new MongoClient("mongodb://127.0.0.1:27017", array("connect"=>TRUE, "connectTimeoutMS"=>10, "socketTimeoutMS"=>10, "wTimeoutMS"=>10));
    
        $db= $m->mydb;
        $coll = $db->testData;
        $coll->insert($paramArr);
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.