PHP Mongo Error reading from socket

2019-01-26 18:43发布

问题:

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']}");
        };
    }
    }

回答1:

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.



回答2:

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);
    


标签: php mongodb