在单独的服务器上,PHP 和 MySQL 之间可以有多少个连接/秒?

发布于 2024-07-22 06:15:14 字数 922 浏览 7 评论 0原文

尝试将我的 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>

替代文本 http://img35.imageshack.us/img35/4580/picture2uyw .png

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-07-29 06:15:14

我认为限制是 65535。所以你必须同时有 65535 个连接才能达到该限制,因为常规 mysql 连接会自动关闭。

mysql_connect()

注意:脚本执行结束后,到服务器的链接将立即关闭,除非通过显式调用 mysql_close() 提前关闭。

但是如果您使用 持久 mysql 连接,那么你可能会遇到麻烦。

使用持久连接可能需要对 Apache 和 MySQL 配置进行一些调整,以确保不超过 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()

Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().

But if you're using a persistent mysql connection, then you can run into trouble.

Using persistent connections can require a bit of tuning of your Apache and MySQL configurations to ensure that you do not exceed the number of connections allowed by MySQL.

奢望 2024-07-29 06:15:14

每个 MySQL 连接实际上会使用几兆内存用于各种缓冲区,并且需要一段时间才能建立,这就是为什么 MySQL 默认限制为 100 个并发打开连接。 您可以提高该限制,但最好花时间通过各种方法尝试限制并发连接。

请注意不要将连接限制提高得太高,因为您可能会耗尽内存(我相信这会导致 mysql 崩溃),或者您可能会将重要的内容挤出内存。 例如,MySQL的性能高度依赖于操作系统自动将从磁盘读取的数据缓存在内存中; 如果您将连接限​​制设置得太高,您将与缓存争夺内存。

如果您不提高连接限制,那么您将在用完套接字/文件句柄/等之前很久就用完连接。 如果你确实增加了连接限制,那么在用完套接字/文件句柄/等之前,你就会用完 RAM。

关于限制并发连接:

  1. 使用连接池解决方案。 你是对的,PHP 中没有内置的,但是有很多独立的可供选择。 这节省了昂贵的连接设置/拆除时间。
  2. 仅在绝对需要时才打开数据库连接。 在我当前的项目中,我们在发出第一个查询时自动打开数据库连接,而不是之前; 我们还会在完成所有数据库工作之后、实际生成页面的 HTML 之前释放连接。 保持连接打开的时间越短,同时打开的连接就越少。
  3. 在 memcached 等轻量级解决方案中缓存您可以缓存的内容。 我当前的项目临时缓存向匿名用户显示的页面(因为每个匿名用户最终都会获得相同的 HTML - 为什么要在几毫秒后再次运行相同的数据库查询?),这意味着不需要数据库连接全部。 这对于匿名流量的爆发特别有用,例如头版挖掘。

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:

  1. Use a connection pooling solution. You're right, there isn't one built in to PHP, but there are plenty of standalone ones out there to choose from. This saves expensive connection setup/tear down time.
  2. Only open database connections when you absolutely need them. In my current project, we automatically open a database connection when the first query is issued, and not a moment before; we also release the connection after we've done all our database work, but before the page's HTML is actually generated. The shorter the period of time you hold connections open, the fewer connections will be open simultaneously.
  3. Cache what you can in a lighter-weight solution like memcached. My current project temporarily caches pages displayed to anonymous users (since every anonymous user gets the same HTML, in the end -- why bother running the same database queries all over again a few scant milliseconds later?), meaning no database connection is necessary at all. This is especially useful for bursts of anonymous traffic, like a front-page digg.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文