在单独的服务器上,PHP 和 MySQL 之间可以有多少个连接/秒?
尝试将我的 LAMP 应用程序分成两台服务器,一台用于 php,一台用于 mysql。 到目前为止,应用程序通过文件套接字在本地连接并且工作正常。
我担心如果通过网络可以建立多少连接。 我一直在 unix 上测试 tcp 连接以进行基准测试,并且我知道每秒的连接数不能超过一定数量,否则它会由于缺乏资源(无论是套接字、文件句柄还是任何)。 我还了解到 php 没有实现连接池,因此对于每个页面加载都必须通过网络建立一个新连接。 我还研究了 php 的 pconnect,它似乎带来了更多问题。
我知道这是一个非常非常常见的设置(php+mysql),任何人都可以提供他们从服务器获得的一些典型用法和统计数据吗? 谢谢!
该问题与 MySQL 允许的连接耗尽无关。 主要问题是unix无法非常快速地创建和拆除tcp 连接。 套接字最终处于 TIME_WAIT 状态,您必须等待一段时间才能释放更多套接字以再次连接。 这两个屏幕截图清楚地显示了这种模式。 MySQL 确实工作到某个点,然后因为 Web 服务器耗尽套接字而暂停。 经过一定时间后,网络服务器能够建立新的连接。
替代文本 http://img35.imageshack.us/img35/3809/picture4k.png< /a>
Trying to separate out my LAMP application into two servers, one for php and one for mysql. So far the application connects locally through a file socket and works fine.
I'm worried about the number connections I can establish if it is over the network. I have been testing tcp connections on unix for benchmark purposes and I know that you cannot exceed a certain amount of connections per second otherwise it halts due to the lack of resources (be it sockets, or file handles or whatever). I also understand that php does not implement connection pooling so for each page load a new connection over the network must be made. I also looked into pconnect for php and it seems to bring more problems.
I know this is a very very common setup (php+mysql), can anyone provide some typical usage and statistics they get out of their servers? Thanks!
The problem is not related to running out of connections allowed my MySQL. The main problem is that unix cannot very quickly create and tear down tcp connections. Sockets end up in TIME_WAIT and you have to wait for a period before you free up more sockets to connect again. These two screenshots clearly shows this pattern. MySQL does work up to a certain point and then pauses because the web server ran out of sockets. After certain amount of time passed, the web server was able to make new connections.
alt text http://img35.imageshack.us/img35/3809/picture4k.png
alt text http://img35.imageshack.us/img35/4580/picture2uyw.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为限制是 65535。所以你必须同时有 65535 个连接才能达到该限制,因为常规 mysql 连接会自动关闭。
mysql_connect()
但是如果您使用 持久 mysql 连接,那么你可能会遇到麻烦。
I think the limit is at 65535. So you'd have to have 65535 connections at the same time to hit that limit since a regular mysql connection closes automatically.
mysql_connect()
But if you're using a persistent mysql connection, then you can run into trouble.
每个 MySQL 连接实际上会使用几兆内存用于各种缓冲区,并且需要一段时间才能建立,这就是为什么 MySQL 默认限制为 100 个并发打开连接。 您可以提高该限制,但最好花时间通过各种方法尝试限制并发连接。
请注意不要将连接限制提高得太高,因为您可能会耗尽内存(我相信这会导致 mysql 崩溃),或者您可能会将重要的内容挤出内存。 例如,MySQL的性能高度依赖于操作系统自动将从磁盘读取的数据缓存在内存中; 如果您将连接限制设置得太高,您将与缓存争夺内存。
如果您不提高连接限制,那么您将在用完套接字/文件句柄/等之前很久就用完连接。 如果你确实增加了连接限制,那么在用完套接字/文件句柄/等之前,你就会用完 RAM。
关于限制并发连接:
Each MySQL connection actually uses several meg of ram for various buffers, and takes a while to set up, which is why MySQL is limited to 100 concurrent open connections by default. You can up that limit, but it's better to spend your time trying to limit concurrent connections, via various methods.
Beware of raising the connection limit too high, as you can run out of memory (which, I believe, crashes mysql), or you may push important things out of memory. e.g. MySQL's performance is highly dependent on the OS automatically caching the data it reads from disk in memory; if you set your connection limit too high, you'll be contending for memory with the cache.
If you don't up your connection limit, you'll run out of connections long before your run out of sockets/file handles/etc. If you do increase your connection limit, you'll run out of RAM long before you run out of sockets/file handles/etc.
Regarding limiting concurrent connections: