简体   繁体   English

MySQL连接限制和PHP MySQL连接

[英]MySQL connection limit and PHP MySQL connections

I am having a frustrating issue. 我遇到一个令人沮丧的问题。 My web service has an email to php module. 我的网络服务有一封电子邮件发送到php模块。 Briefly, once my user sends an email to his dedicated email address, my mail server captures the email, pipes it to a PHP script running on my server. 简而言之,一旦用户将电子邮件发送到其专用电子邮件地址,我的邮件服务器就会捕获该电子邮件,并将其通过管道传输到在我的服务器上运行的PHP脚本。 This PHP script opens a MySQL connection and saves the email content to the database. 该PHP脚本打开一个MySQL连接,并将电子邮件内容保存到数据库中。

So far so good. 到现在为止还挺好。 The problem starts to occur once my MySQL connection limit set in /etc/my.cnf is exceeded. 一旦超过了在/etc/my.cnf中设置的MySQL连接限制,该问题就会开始发生。 It's set to 500. If 500 or more users sends emails at the same time, my MySQL gets unable to handle new connections. 设置为500。如果同时有500个或更多用户发送电子邮件,我的MySQL将无法处理新连接。

What do you suggest? 你有什么建议? Should I use a persistent MySQL connection or any other method like writing the received email content to a txt file and then saving them into the db with a cron job? 我是否应该使用持久的MySQL连接或任何其他方法,例如将接收到的电子邮件内容写入txt文件,然后使用cron作业将其保存到db中?

Thanks for your suggestions! 感谢您的建议!

另一个建议是像守护进程那样运行PHP脚本,该脚本仅连接到DB一次,并等待输入以无穷循环处理。

I have a similar feature on a site I run. 我在运行的网站上有类似的功能。 Instead of piping the email to a script, where you have no control over how many emails will be delivered at a given time, set up aa cron. 设置电子邮件,而不是将电子邮件发送到脚本,在脚本中您无法控制在给定时间要发送多少电子邮件。 This cron can run as often as you like. 该cron可以根据需要运行多次。 It can connect to the mail server, read as much mail as it chooses at once, and maintain a single connection to MySQL. 它可以连接到邮件服务器,一次读取任意数量的邮件,并保持与MySQL的单一连接。

This will also greatly help in the event you get mail-bombed. 如果您遭到邮件炸弹袭击,这也将极大地帮助您。

If 500 or more users sends emails at the same time 如果500个或更多用户同时发送电子邮件

hmmm. 嗯。 It's very unusual for two events to occur "at the same time" on a computer system - but frequent for processes to overlap. 在计算机系统上“同时”发生两个事件是非常不寻常的-但过程经常重叠。 I expect you will get a lot of mileage out of optimizing the email handler. 我希望您将从优化电子邮件处理程序中受益匪浅。 But you've provided very little details of how that works. 但是您只提供了很少的细节。 Not do you say what OS this is running on. 您不说这是在什么操作系统上运行。

I'd recommend you check that there aren't signs of overuse on the CPU or memory (NB you want around 80% of your memory free). 我建议您检查一下CPU或内存上是否没有过度使用的迹象(请注意,大约80%的可用内存可用)。 If the handler starts a PHP instance for each message then that's a major overhead - but this does not contribute to the time that the mysql connection is open. 如果处理程序为每个消息启动一个PHP实例,那么这是一个主要的开销-但这不会增加mysql连接打开的时间。

If you can avoid it, don't use Blobs/Clobs - dump the data to diskfile and reference it from the database. 如果可以避免,请不要使用Blob / Clobs-将数据转储到diskfile并从数据库中引用它。

Apply all the usual performance tuning steps (optimizing physical storage layer - eg RAID, database tuning). 应用所有常规的性能调整步骤(优化物理存储层-例如RAID,数据库调整)。

Collate all the DB interactions run by the script and run them in one block, bracketed with your db connect and close statements. 整理由脚本运行的所有数据库交互,并在一个块中运行它们,并与您的数据库connect和close语句一起放在括号中。

If you can't handle the volume synchronously, then... Rather than reinventing the wheel, DO NOT implement your own queueing system for processing of messages - route the messages directly to a maildir or mailbox and run a daemon to poll the contents and process it. 如果无法同步处理该卷,则...不要重新发明轮子,不要实现自己的排队系统来处理消息-将消息直接路由到maildir或邮箱,并运行守护程序来轮询内容并处理它。 The off-the shelf MDAs will be much more efficient than using PHP. 现成的MDA将比使用PHP更有效。

C. C。

Why not increase the connection limit to the number of concurrent users you are expecting? 为什么不将连接限制增加到期望的并发用户数?

OR 要么

Have a slight delay or retry period where it attempts to connect again if it fails. 稍有延迟或重试期,如果失败,它将尝试再次连接。

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

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