MySQL 错误“连接太多"

本文介绍了MySQL 错误“连接太多"的处理方法,对大家解决问题具有一定的参考价值

问题描述

我正在将 MySQL 5.0 用于由 GoDaddy (linux) 托管的站点.

I am using MySQL 5.0 for a site that is hosted by GoDaddy (linux).

我正在对我的网络应用程序进行一些测试,突然我注意到页面刷新非常缓慢.最后,经过漫长的等待,我到了一个页面,上面写着MySQL 错误,连接太多......",它指向了我连接到数据库的 config.php 文件.

I was doing some testing on my web app, and suddenly I noticed that the pages were refreshing really slowly. Finally, after a long wait, I got to a page that said something along the lines of "MySQL Error, Too many connections...", and it pointed to my config.php file which connects to the database.

只是我连接到数据库,没有其他用户.在我的每个页面上,我在顶部包含 config.php 文件,并在页面末尾关闭 mysql 连接.中间可能有几个查询.我担心我没有足够关闭mysql连接(mysql_close()).

It has just been me connecting to the database, no other users. On each of my pages, I include the config.php file at the top, and close the mysql connection at the end of the page. There may be several queries in between. I fear that I am not closing mysql connections enough (mysql_close()).

但是,当我在运行查询后尝试关闭它们时,我在页面上收到连接错误.我的页面是 PHP 和 HTML.当我尝试关闭一个查询时,下一个似乎无法连接.我是否必须在关闭后再次包含 config.php 才能连接?

However, when I try to close them after running a query, I receive connection errors on the page. My pages are PHP and HTML. When I try to close a query, it seems that the next one won't connect. Would I have to include config.php again after the close in order to connect?

这个错误让我很害怕,因为在 2 周内,大约有 84 人开始使用这个网络应用程序.

This error scared me because in 2 weeks, about 84 people start using this web application.

谢谢.

这是我页面的一些伪代码:

Here is some pseudo-code of my page:

 require_once('../scripts/config.php');

 <?php
    mysql_query..

    if(this button is pressed){
       mysql_query...
    }
    if(this button is pressed){
       mysql_query...
    }
    if(this button is pressed){
       mysql_query...
    }
 ?>
 some html..
 ..
 ..
 ..
 ..
 <?php
   another mysql_query...
 ?>
 some more html..
 ..
 ..
 <?php mysql_close(); ?>

我想这样,每次页面打开时,连接都会打开,然后在页面加载完成时连接会关闭.然后,当有人单击页面上的按钮时,连接再次打开,依此类推...

I figured that this way, each time the page opens, the connection opens, and then the connection closes when the page is done loading. Then, the connection opens again when someone clicks a button on the page, and so on...

好的,我刚刚与 GoDaddy 通了电话.显然,使用我的经济套餐,我一次只能连接 50 个.虽然我今天的问题发生在只有我访问该站点的情况下,但他们说他们早些时候遇到了一些服务器问题.但是,考虑到我的网络应用程序将如何拥有 84 个用户,我可能应该升级到豪华",它一次允许 100 个连接.在某一天,一次可能有大约 30 个用户访问我的网站,所以我认为 100 个用户会更安全.你们同意吗?

Okay, so I just got off the phone with GoDaddy. Apparently, with my Economy Package, I'm limited to 50 connections at a time. While my issue today happened with only me accessing the site, they said that they were having some server problems earlier. However, seeing as how I am going to have 84 users for my web app, I should probably upgrade to "Deluxe", which allows for 100 connections at a time. On a given day, there may be around 30 users accessing my site at a time, so I think the 100 would be a safer bet. Do you guys agree?

推荐答案

共享主机提供商通常允许同一用户同时进行少量连接.

Shared-hosting providers generally allow a pretty small amount of simultaneous connections for the same user.

您的代码所做的是:

  • 打开与 MySQL 服务器的连接
  • 做它的东西(生成页面)
  • 在页面末尾关闭连接.

最后一步,在页面末尾完成时不是强制 : (引用 mysql_close 的手册):

The last step, when done at the end of the page is not mandatory : (quoting mysql_close's manual) :

通常不使用 mysql_close()必要的,因为非持久打开链接会自动关闭脚本执行结束.

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.

但请注意,您可能无论如何都不应该使用持久连接...

But note you probably shouldn't use persistent connections anyway...

两个提示:

  • mysql_pconnect 中使用 mysql_connect (你已经可以了)
  • 将mysql_connect的第四个参数设置为false(已经可以了,因为它是默认值):(引用手册):
  • use mysql_connect insead of mysql_pconnect (already OK for you)
  • Set the fourth parameter of mysql_connect to false (already OK for you, as it's the default value) : (quoting the manual) :

如果第二次调用mysql_connect() 同论点,不会有新的链接建立,但相反,链接已打开链接的标识符将被退回.

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.

新链接参数修改此行为并使 mysql_connect() 总是打开一个新链接,即使 mysql_connect() 是之前用同样的方法调用过参数.

The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.


那么可能导致问题的原因是什么?

What could cause the problem, then ?

也许您正在尝试并行访问多个页面(例如,使用浏览器中的多个选项卡),这将模拟多个用户同时使用该网站?

Maybe you are trying to access several pages in parallel (using multiple tabs in your browser, for instance), which will simulate several users using the website at the same time ?

如果您有许多用户同时使用该站点,并且 mysql_connect 之间的代码和关闭连接需要很长时间,这意味着同时打开了许多连接... 你会达到极限:-(

If you have many users using the site at the same time and the code between mysql_connect and the closing of the connection takes lots of time, it will mean many connections being opened at the same time... And you'll reach the limit :-(

不过,由于您是该应用程序的唯一用户,考虑到您最多允许同时连接 200 个连接,因此有些奇怪的事情正在发生...

Still, as you are the only user of the application, considering you have up to 200 simultaneous connections allowed, there is something odd going on...


好吧,想想连接太多"和max_connections"...

Well, thinking about "too many connections" and "max_connections"...

如果我没记错的话,max_connections不限制可以打开到MySQL服务器的连接数,而是连接总数 可以打开到该服务器,任何连接到它的人.

If I remember correctly, max_connections does not limit the number of connections you can open to the MySQL Server, but the total number of connections that can bo opened to that server, by anyone connecting to it.

Too many connections :

如果您的连接过多当您尝试连接到mysqld服务器,这意味着所有可用的连接正在由其他客户.

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.

允许的连接数是由 max_connections 控制系统变量.它的默认值为100.如果需要支持更多的连接,应该设置更大的此变量的值.

The number of connections allowed is controlled by the max_connections system variable. Its default value is 100. If you need to support more connections, you should set a larger value for this variable.

所以,实际上,问题可能不是来自您或您的代码(实际上看起来不错):它可能只是"是您不是唯一一个试图连接到该代码的人MySQL 服务器(记住,共享主机"),而且有太多人同时使用它...

So, actually, the problem might not come from you nor your code (which looks fine, actually) : it might "just" be that you are not the only one trying to connect to that MySQL server (remember, "shared hosting"), and that there are too many people using it at the same time...

...而且 如果我是对的,那就是这样,您无法解决问题:只要该服务器上的数据库/用户太多并且 max_connection设置为200,你会继续受苦...

... And if I'm right and it's that, there's nothing you can do to solve the problem : as long as there are too many databases / users on that server and that max_connection is set to 200, you will continue suffering...


作为旁注:在回到 GoDaddy 询问他们之前,如果有人可以验证我刚才所说的话,那就太好了^^


As a sidenote : before going back to GoDaddy asking them about that, it would be nice if someone could validate what I just said ^^

这篇关于MySQL 错误“连接太多"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1173

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1071

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:809

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:901

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:932

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:880

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:865

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:834

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1738

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1309