duanliao5995 2015-12-01 16:12
浏览 37
已采纳

PHP - 持久连接,连接太多

I have a website where I have around 700 - 1000 users online at all times. Lately I have experienced that my website is getting very slow. There seems to be some sort of bottleneck.

My current setup is I have an index.php page, which handles all my subpages. The design is like this:

include_once($settings['incpath'].'_ify_database.php');

    //Define the extra page levels. 
    $i = (isset($_GET['i']) ? inputFilter($_GET['i']) : 'h');
    $xp = (isset($_GET['p']) ? inputFilter($_GET['p']) : null);
    $xpp = (isset($_GET['n']) ? inputFilter($_GET['n']) : null);    

    switch($i){
        case 'h': // Home
            include($settings['themespath'].$config['active_theme'].'/indexheader.php');
            include($settings['pagepath'].'home.php');
            include($settings['themespath'].$config['active_theme'].'/indexfooter.php');
        break;
     }

This is how I connect to the database (_ify_database.php):

try {
    $dsn = "mysql:host=" . $database['host'] . ";dbname=" . $database['db'];
    $dbh = new PDO($dsn, $database['user'], $database['pass'], array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT));     
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); // <== add this line
    }
catch(PDOException $e){
    echo $e->getMessage();
}

As you can see, I've set the persistent connection to false:

PDO::ATTR_PERSISTENT => false

If I enable it to TRUE I see a good increase in the load time, but after some time I get Too Many Connections error.

What am I doing wrong? If there is 500 people on the website it's fast. If there is 900-1000 it double it's load time.

  • 写回答

1条回答 默认 最新

  • dongpeng8994 2015-12-01 16:18
    关注

    limit the max number of persistent connections from php, or increase the limit in mysql.. try

    ini_set('mysql.max_persistent','500');
    ini_set('mysqli.max_persistent','500');
    

    and found this in the mysql docs, seems related: https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)