pdo

dsn链接参数中的其中dbname参数可以忽略,创建连接之后执行一个单独的sql。

$dsn = sprintf("mysql:host=%s;port=%s;dbname=;charset=%s",
    $conf['host'],
    $conf['port'],
    $conf['db_name'],
    $conf['db_charset']
);
$pdo->query("use `dbname`;");

设置读超时

修改 php.ini

mysqlnd.net_read_timeout	3
mysqlnd.net_read_timeout. mysqlnd.net_read_timeout gets used by any extension (ext/mysql, ext/mysqli, PDO_MySQL) that uses mysqlnd. mysqlnd tells PHP Streams to use mysqlnd.net_read_timeout

http://php.net/manual/zh/mysqlnd.config.php

断开连接

主动断开连接

$pdoConnection = null;

According to documentation you're correct (http://php.net/manual/en/pdo.connections.php):

The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

Note that if you initialise the PDO object as a persistent connection it will not automatically close the connection.

参考链接