开发者社区> 问答> 正文

php mysql_connect默认使用了连接池来管理连接吗??报错

在本地测试Mysql 引擎Innodb与MyISAM的性能差别, 通过Shell脚本来进行并发更新操作,如下所示:


for ((i=0; i<$concurrent; i++))
do
    mysql -uroot -p123456 -Dtest -e "update ${tablename} set count=count+1;" &
done

但设置并发为200时报错: Too many connections.


看文档得知报此错误正常.


If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients. The number of connections permitted is controlled by the max_connections system variable. The default value is 151 to improve performance when MySQL is used with the Apache Web server. (Previously, the default was 100.) If you need to support more connections, you should set a larger value for this variable.


但使用ab测试(访问php页面)来进行同样并发量的测试时,却没有报错。

php脚本:


<?php
$conn=mysql_connect('127.0.0.1', 'root', '123456') or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$sql="update count_t set count=count+1";
mysql_query($sql) or die(mysql_error());
?>

ab测试命令:

$ ab -n200 -c200 http://localhost/test_update_count_t.php



是因为mysql_connect自动使用了连接池来管理连接了吗? 怎么看连接池的配置选项呢? 如连接池大小等。




展开
收起
爱吃鱼的程序员 2020-06-14 14:55:04 703 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    mysql管理者自己的连接池,mysql链接的过期时间默认是8个小时,所以持续不断的请求,mysql还是会把旧的链接分配给你;

    mysql的链接是按需创建的,没有固定的链接;

    可以通过 max_connections=num设置最大连接数

    2020-06-14 14:55:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载

相关镜像