1、当 函数 mysql_connect 的前三个参数(server
username
password)相同,
mysql_connect 是会返回相同的连接。并且第四个参数(new_link)不传递时候
,重复调用
php代码
<?php $db = mysql_connect(‘localhost‘,‘root‘,‘root‘); var_dump($db); $db2 = mysql_connect(‘localhost‘,‘root‘,‘root‘); var_dump($db2); sleep(10);
页面打印信息
resource(3) of type (mysql link) resource(3) of type (mysql link)
mysql连接数
mysql> show full processlist; +----+------+-----------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+-------+-----------------------+ | 32 | root | localhost:54408 | NULL | Query | 0 | NULL | show full processlist | | 44 | root | localhost:55278 | NULL | Sleep | 3 | | NULL | +----+------+-----------------+------+---------+------+-------+-----------------------+ 2 rows in set
2、当函数 mysql_connect 传递了第四个参数(
)后,再重复调用它。new_link
php代码
<?php $db = mysql_connect(‘localhost‘,‘root‘,‘root‘,1); var_dump($db); $db2 = mysql_connect(‘localhost‘,‘root‘,‘root‘,1); var_dump($db2); sleep(10);
页面打印信息
resource(3) of type (mysql link) resource(5) of type (mysql link)
mysql连接数
mysql> show full processlist; +----+------+-----------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+-------+-----------------------+ | 32 | root | localhost:54408 | NULL | Query | 0 | NULL | show full processlist | | 45 | root | localhost:55320 | NULL | Sleep | 4 | | NULL | | 46 | root | localhost:55321 | NULL | Sleep | 4 | | NULL | +----+------+-----------------+------+---------+------+-------+-----------------------+
3、mysql_pconnect 持久连接
php代码
<?php $db = mysql_pconnect(‘localhost‘,‘root‘,‘root‘); var_dump($db); $db2 = mysql_pconnect(‘localhost‘,‘root‘,‘root‘); var_dump($db2);
页面打印信息
resource(3) of type (mysql link persistent) resource(4) of type (mysql link persistent)
mysql连接数
mysql> show full processlist; +----+------+-----------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+-------+-----------------------+ | 1 | root | localhost:55391 | NULL | Query | 0 | NULL | show full processlist | | 2 | root | localhost:55393 | NULL | Sleep | 5 | | NULL | +----+------+-----------------+------+---------+------+-------+-----------------------+
4、mysql_pconnect 加上第四个参数(new_link)即可 建立多个 持久连接
php代码
<?php $db = mysql_pconnect(‘localhost‘,‘root‘,‘root‘,1); var_dump($db); $db2 = mysql_pconnect(‘localhost‘,‘root‘,‘root‘,1); var_dump($db2);
页面打印信息
resource(3) of type (mysql link persistent) resource(4) of type (mysql link persistent)
mysql连接数
mysql> show full processlist; +----+------+-----------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+-------+-----------------------+ | 1 | root | localhost:55391 | NULL | Query | 0 | NULL | show full processlist | | 2 | root | localhost:55393 | NULL | Sleep | 316 | | NULL | | 3 | root | localhost:55418 | NULL | Sleep | 4 | | NULL | +----+------+-----------------+------+---------+------+-------+-----------------------+ 3 rows in set
5、总结:
函数 mysql_pconnetc 创建的 持久化连接 在php脚本结束后,mysql的连接并不会立即结束。什么时候结束,是由mysql自己管理。函数 mysql_colse 不能关闭 mysql_pconnetc 的连接。
函数 mysql_connetc 创建的 连接 在php脚本结束后,mysql的连接也会相应结束。
时间: 2024-10-13 00:47:50