Web 和 DB 服务器之间的开放连接数量极大
我运行 2 个服务器,1 个网络(nginx/php),1 个数据库(mysql)。
Nginx 每秒大约有 1500 个活动进程,mysql status 平均显示大约 15 个当前选项连接。
今天我开始运行:netstat -npt | awk '{print $5}' | grep -v "ffff\|127\.0\.0\.1" | grep -v "ffff\|127\.0\.0\.1" | awk -F ':' '{print $1}' |排序 -n | uniq-c| sort -n
这表明从我的网络服务器到我的数据库服务器 IP 有超过 7000 个活动连接。这似乎有些极端。我不使用PHP中的持久连接来连接Mysql。
我也尝试使用 mysql_close() ,但这似乎没有什么区别。
在 Web 服务器上,netstat 显示超过 7000 个到数据库服务器的连接
在数据库服务器上,netstat 显示只有 300 个到 Web 服务器的连接
知道为什么有这么多打开的连接吗?
I run 2 servers, 1 web (nginx/php), 1 database (mysql).
Nginx has about 1500 active processes per second, and mysql status shows about 15 currently option connections on average.
Now today i started running: netstat -npt | awk '{print $5}' | grep -v "ffff\|127\.0\.0\.1" | awk -F ':' '{print $1}' | sort -n | uniq -c | sort -n
This showed that there were over 7000 active connections from my webserver to my database server IP. This seems kind of extreme. I do not use persistent connections in PHP to connect to Mysql.
I tried using mysql_close() also, but that seems to make no difference.
On the webserver netstat shows over 7000 connections to the database server
On the database server netstat shows just 300 connections to the web server
Any idea why there are so many open connections?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试仅检查已建立的连接:
Try checking for established connections only:
这些连接的状态如何?我认为这是 TIME_WAIT。当您的请求数为 1500 个/秒时,您将打开和关闭 1500 个 TCP 连接。关闭连接需要一些时间(协议中指定了一些超时,因此即使在本地主机上,关闭也不是即时的 - 此处有更多信息 此处)。
这没有什么问题 - 7000 个连接并不算多(但是接下来提到的持久连接会稍微快一些)。 Web 服务器和数据库服务器之间最多可以有 65536 个连接。另请注意,这并不是通常认为的打开连接的总限制。此限制不是针对您的 IP 地址,而是针对每个 IP 对(每对 src-ip 和 dst-ip 之间可以有 65536 个连接)。
当使用nginx时,我假设你在Fast-CGI模式下使用PHP。因此,对于这 1500 个请求/秒,您应该有一些 PHP 进程等待处理它们(我的一台服务器上也有大约 2000-5000 个请求/秒,并且有 50-250 个 PHP 进程)。对于如此高的连接数和相对较少的 PHP 进程数,最好使用持久连接。每个 PHP 进程都会与数据库建立一个永久连接。因此,您将拥有大约 100 个与 MySQL 的连接,但它们将保持打开状态,因此无需进行 3 路 TCP 握手和握手。关闭每个网络请求。
What is the status of those connections? I assume it is TIME_WAIT. When you have 1500 req/s, you are opening and closing 1500 TCP connections. And it takes some time to close the connection (there are some timeouts specified in the protocol, so even on localhost the closing is not instant - more info here here).
There is nothing wrong with that - 7000 connections is not much (however persistent connections as mentioned next will be slightly faster). You can have up to 65536 connections between your webserver and database server. Also please note that this isn't your total limit of opened connections as it is usually believed. This limit is not per your IP address, but per IP pair (each pair of src-ip and dst-ip can have 65536 connections among them).
When using nginx, I assume you use PHP in Fast-CGI mode. So for those 1500 req/s, you should have some PHP processes waiting to process them (I have also about 2000-5000 req/s on one of my servers and have 50-250 PHP processes). For such high number of connections and relatively low number of PHP processes, it is better to use persistent connections. Each PHP process will make one permanent connections to the database. So you will have about 100 connections to MySQL, but they will stay opened, so no need to make 3-way TCP handshake & close on each web request.