mongodb 在php环境中 Read timed out 的解决方法

Read timed out after reading 0 bytes, waited for 30.000000 seconds

由于数据过大,读取超时,socket连接超时默认是30秒,超进会抛出 MongoExecutionTimeoutException 异常,改为120秒,增加等待时间

应用方法:

$mongo_server = "mongodb:///tmp/mongodb-27017.sock,mongodb://127.0.0.1:27017/";//优先连接套接字

$m = new MongoClient($mongo_server, array('connectTimeoutMS' => 120000, 'socketTimeoutMS' => 120000, 'wTimeout' => 120000));

MongoCursor::$timeout = 120000;

 

$db = $m->db;//库名

$collection = $db->collect;//集合

 

一、指针超时时间设置

$cursor = $collection->find();

$cursor->timeout(120000);//这个会被同样作用于findOne

 

二、findOne 增加超时时间,参数:'maxTimeMS' => 120000 ,单位毫秒

$result = $collection->findOne(array('id' => $id), array(), array('maxTimeMS' => 120000);//第2个array()中可以写要提取的字段,留空为提取全部

 

三、updat增加超时时间,参数:'socketTimeoutMS' => 120000, 'wTimeoutMS' => 60000

$collection->update(array('id' => $id), $data, array('upsert' => true, 'socketTimeoutMS' => 120000, 'wTimeoutMS' => 60000));