只是放
ini_set("default_socket_timeout", 2);
之前你的PDO()连接字符串。
(在Windows上测试,在Linux上也应该很好。)
为什么?
通过手册追逐:
mysqlnd驱动程序使用套接字进行底层连接,而设置超时则需要使用套接字(stream)超时功能。 (参考:http://php.net/manual/en/mysqlnd.notes.php)
Using mysqlnd means using PHP streams for underlying connectivity. For mysqlnd, the PHP streams documentation (Streams) should be consulted on such details as timeout settings, not the documentation for the MySQL Client Library.
如果你想要更多的控制,那么你可能能够更具体地控制实际的套接字:我还没有测试过,因为它只是unix。要设置套接字mysqlnd使用,可以使用ini设置指定套接字(参考:http://php.net/manual/en/ref.pdo-mysql.connection.php)
If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.
但可能更容易设置默认值,然后重新设置完成后…