如何从 PHP 检查网络连接或数据库服务器的负载?

发布于 2024-10-08 16:05:45 字数 150 浏览 2 评论 0原文

我有两台服务器,X是网络服务器(PHP),Y是数据库服务器(MYSQL), 我如何监控 X 和 Y 之间的网络是否过载。 而且,有时Y的反应很慢!我如何检查Web服务器端(PHP端)的负载,以查看数据库服务器是否过载或这两个服务器之间的连接无法满足此负载?

感谢您的帮助

I have two servers, X is the web-server (PHP) and Y is the database server (MYSQL),
how can i monitor if there is overload on the network between X and Y.
also, sometimes the response from Y is very slow!!! how can i check the load from the web server side (PHP side) to see if the DB server is over-loaded or the connection between those two servers can't satisfy this load??

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

黯然#的苍凉 2024-10-15 16:05:46

最快的方法是通过SSH登录机器并使用CLI工具(例如htoptop)来检查当前负载,但是如果您有多个执行不同任务的服务器,那么安装某种监控来收集有关负载和当前负载的指标和 getter 信息是有意义的。使用方面,有很多复杂的工具,例如 Ganglia http://ganglia.sourceforge.net/ 、 Munin http://munin-monitoring.org/ 或者,如提到的 Nagios。

The fastest way would be, log on the machine via SSH and use CLI Tools like htop or top to check the current load, however if you have multiple Servers that performe different tasks it would make sense to install some kind of monitoring to collect metrics and getter Informations about Load & Usage, there a lot of sophisticated tools for that like Ganglia http://ganglia.sourceforge.net/ , Munin http://munin-monitoring.org/ or, as mentioned Nagios.

少女净妖师 2024-10-15 16:05:46

好吧,一种选择是在 MySQLi 中指定超时子句并使用 MySQLi ::real_connect...这样,如果它提前超时,您可以假设它已关闭...这是一个超时为 1 秒的示例:

$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 1);
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
    // Connection timed out (most likely), so assume load too high
}

另一种选择是使用类似 Nagios 来监视有问题的服务器。然后在尝试连接之前编写一些快速代码来检查 Nagios 的状态(就像读取日志文件一样简单)。

如果您真的很担心 /. 效果之类的事情,您可以保留纯 HTML 站点的副本,然后在站点速度足够慢时切换到它(编写一个 cron 作业检查服务器负载,如果负载太高则切换到服务器负载,如果负载下降足够则切换回来)。

但是,我要问,你为什么要这样做?为什么不让 PHP 优雅地失败呢?解决负载问题比编写所有这些代码(这会增加负载本身)来尝试检测它更好。修复缓慢且低效的部分,您将不必担心这一切......

Well, one option would be to specify a timeout clause in MySQLi and use MySQLi::real_connect... That way if it times out early, you can assume it's down... Here's an example with a timeout of 1 second:

$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 1);
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
    // Connection timed out (most likely), so assume load too high
}

Another option would be to use a tool like Nagios to monitor the server(s) in question. Then write a quick bit of code to check the status from Nagios before trying to connect (It's as simple as reading a log file).

If you're really that concerned about things like the /. effect, you could keep a copy of the site in pure HTML, and then switch to it if the site slows down enough (write a cron job to check the server load, and if it's too high switch to it, and if it falls enough switch back).

But, I have to ask, why do you want to do this? Why not just let PHP fail gracefully? It's better to fix the load issues than to write all this code (which is going to increase the load itself) to try to detect it. Fix the slow and inefficient parts and you won't have to worry about all this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文