1 <?php 2 function getMongoClient($seeds = "", $options = array(), $retry = 3) { 3 try { 4 return new MongoClient($seeds, $options); 5 } catch(Exception $e) { 6 /* Log the exception so we can look into why mongod failed later */ 7 logException($e); 8 } 9 if ($retry > 0) { 10 return getMongoClient($seeds, $options, --$retry); 11 } 12 throw new Exception("I‘ve tried several times getting MongoClient.. Is mongod really running?"); 13 } 14 15 try { 16 $mc = getMongoClient("localhost", array()); 17 } catch(Exception $e) { 18 /* Can‘t connect to MongoDB! */ 19 logException($e); 20 die("Can‘t do anything :("); 21 }
解决方法;失败重试