duanniu3385 2011-04-24 13:52
浏览 83
已采纳

PHP Mysql配置 - 连接太多

Good day!

I have a website deployed in a free web-hosting server. Recently, I cannot access my SQL and receive this warning: too many connections.

MY CODES:

In order to access mysql, i created a config.php file.

 <?php
        // save this as config.php
        define('DB_HOST', 'localhost');
        define('DB_USER', 'username');
        define('DB_PASSWORD', 'password');
        define('DB_DATABASE', 'pascual4_eventpost');
        $conn=mysql_pconnect(DB_HOST, DB_USER, DB_PASSWORD)
          or die ("Error connecting to database");
        mysql_select_db(DB_DATABASE)
          or die ("Error: Cannot access database");
    ?>

So If ever I need to access my database. I just call require("config.php");

I've read somewhere before that mysql connections automatically closes when the script is done. That's why I don't put any mysql close connection code on my PHP pages. Or do I still need to close it manually and input mysql_close($conn); on every page?

So I restarted my PC and flush my dns. Luckily, now I can access my mysql database but it is very slow. Maybe because I am using a free-web hosting? But, on my part, is there a way where I can speed-up my mysql connection and queries. What Validations can I include on the above code so that I could improve the performance of my mysql connection? Thank you.

  • 写回答

3条回答 默认 最新

  • dqpciw9742 2011-04-24 13:57
    关注

    As you state, PHP will close any open connections when your script terminates, so unless you're tying up connections by executing long-running scripts/queries, etc. then this is most likely a symptom of a very low max_connections mysqld setting, which isn't a huge surprise bearing in mind that you're using a free hosting package.

    I'm not sure how many connections you're using per "page", but:

    1. In my experience using mysql_pconnect generally makes the situation worse, rather than better. (Just use mysql_connect.)

    2. If you're using multiple connections on the same page, it would be an idea to re-use the same database connection if at all possible.

    Failing that, it might be time to look for a more appropriate hosting package.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog