简体   繁体   English

我应该如何处理mysql的“连接过多”问题?

[英]How should I handle a “too many connections” issue with mysql?

The web startup I'm working at gets a spike in number of concurrent web users from 5000 on a normal day to 10,000 on weekends. 我正在工作的网络创业公司的并发Web用户数量从正常的5000个增加到周末的10,000个。 This Saturday the traffic was so high that we started getting a "too many connections" error intermittently. 这个星期六的流量非常高,以至于我们开始间歇性地收到“连接过多”错误。 Our CTO fixed this by simply increasing the max_connections value on the tatabase servers. 我们的CTO通过简单地增加tatabase服务器上的max_connections值来解决此问题。 I want to know if using one persistent connection is a better solution here? 我想知道在这里使用一个持久连接是否是更好的解决方案?

ie instead of using: 即代替使用:

$db = new mysqli('db_server_ip', 'db_user', 'db_user_pass', 'db_name');

We use: 我们用:

$db = new mysqli('p:db_server_ip', 'db_user', 'db_user_pass', 'db_name');

We're already using multiple MySQL servers and as well as multiple web servers (Apache + mod_php). 我们已经在使用多个MySQL服务器和多个Web服务器(Apache + mod_php)。

You should share the database connection across multiple web requests. 您应该在多个Web请求之间共享数据库连接。 Every process that is running on the application server should get an own mysql connection, that is kept open as long as the process is running and reused for every web request that comes in. 在应用程序服务器上运行的每个进程都应该获得自己的mysql连接,只要该进程正在运行,并且该连接对于进入的每个Web请求都可以重用。

From the PHP Docs : PHP文档

Persistent connections are good if the overhead to create a link to your SQL server is high. 如果创建到SQL Server的链接的开销很大,则持久连接是好的。

And

Note, however, that this can have some drawbacks if you are using a database with connection limits that are exceeded by persistent child connections. 但是请注意,如果您使用的数据库具有持久子连接超出的连接限制,则这可能会有一些缺点。 If your database has a limit of 16 simultaneous connections, and in the course of a busy server session, 17 child threads attempt to connect, one will not be able to. 如果您的数据库限制了16个并发连接,并且在服务器会话繁忙的过程中,有17个子线程尝试连接,那么一个子线程将无法连接。

Persistent connections aren't the solution to your problem. 持久连接不是解决问题的方法。 Your problem is that your burst usage is beyond the limits set in your database configuration, and potentially your infrastructure. 您的问题是突发使用超出了数据库配置中设置的限制,甚至超出了基础架构中设置的限制。 What your CTO did, increasing the connection limit, is a good first step. 您的CTO所做的,增加连接限制,是一个很好的第一步。 Now you need to monitor the resource utilization on your database servers to make sure they can handle the increased load from additional connections. 现在,您需要监视数据库服务器上的资源利用率,以确保它们可以处理来自其他连接的增加的负载。 If they can, you're fine. 如果可以的话,那很好。 If you start seeing the database server running out of resources, you'll need to set up additional servers to handle the burst in traffic. 如果开始发现数据库服务器资源不足,则需要设置其他服务器来处理流量激增。

Too Many Connections 太多联系

Cause This is error is caused by 原因这是错误引起的

  • a lot of simultaneous connections, or 大量同时连接,或
  • by old connections not being released soon enough 由于旧的连接不够快而无法释放

You already did SHOW VARIABLES LIKE "max_connections"; 您已经执行了SHOW VARIABLES LIKE "max_connections"; and increased the value. 并增加了价值。

Permanent Connections 永久连接

If you use permanent or persistent database connections , you have to always take the MySQL directive wait_timeout into account. 如果使用永久或持久数据库连接 ,则必须始终考虑MySQL指令wait_timeout Closing won't work, but you could lower the timeout. 关闭不起作用,但是您可以降低超时时间。 So used resources will be faster available again. 因此,使用过的资源将再次更快地可用。 Utilize netstat to find out whats going on exactly as described here https://serverfault.com/questions/355750/mysql-lowering-wait-timeout-value-to-lower-number-of-open-connections . 利用netstat找出完全按照此处描述的方式进行的操作https://serverfault.com/questions/355750/mysql-lowering-wait-timeout-value-to-lower-number-of-open-connections

Do not forget to free your result sets to reduce wasting of db server resources. 不要忘记释放结果集以减少数据库服务器资源的浪费。

Be advised to use temporary, short lived connections instead of persistent connections. 建议使用临时的,短暂的连接,而不是持久连接。 Introducing persistence is pretty much against the whole web request-response flow, because it's stateless. 在整个Web请求-响应流中,引入持久性非常重要,因为它是无状态的。 You know: 1 pconnect request, causes an 8 hour persistant connection dangling around at the db server, waiting for the next request, which never comes. 您知道:1个pconnect请求,导致8小时持久性连接在db服务器上晃来晃去,等待下一个请求,但永远不会到来。 Multiply by number of users and look at your resources. 用用户数量乘以并查看您的资源。

Temporary connections 临时连接

If you use mysql_connect() - do not forget to mysql_close(). 如果使用mysql_connect()-不要忘记mysql_close()。 Set new_link set to false and pass the CLIENT_INTERACTIVE flag. new_link设置为false并传递CLIENT_INTERACTIVE标志。 You might adjusting interactive_timeout , which helps in stopping old connections blocking up the work. 您可能需要调整interactive_timeout ,这有助于阻止旧的连接阻塞工作。

If the problem persists, scale 如果问题仍然存在,请扩展

If the problem remains, then decide to scale. 如果问题仍然存在,则决定扩展规模。 Either by adding another DB server and putting a proxy in front, (MySQL works well with HAProxy) or by switching to an automatically scaling cloud-service. 通过添加另一台数据库服务器并在其前面放置一个代理(MySQL与HAProxy很好地配合使用),或切换到自动扩展的云服务。

I really doubt, that your stuff is correctly configured. 我真的怀疑您的资料配置正确。 How can this be a problem, when you are already running multiple MySQL servers, as well as multiple web servers? 当您已经在运行多个MySQL服务器以及多个Web服务器时,这怎么可能是一个问题? Please describe your load balancing setup. 请描述您的负载平衡设置。

Sounds like Apache 2.2 + mod_php + MySQL + unknown balancer, right? 听起来像Apache 2.2 + mod_php + MySQL +未知的平衡器,对吗?

Maybe try 也许尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM