大网数据广告活动
服务器运维问答

Does Apache web service restart close existing MySQL connections?

· 52CE 我爱测

In the case of 'too many connections' error, if I do a service httpd restart/reload , would it free up the existing MySQL connections? Is it possible that when restarting Apache, Apache closes the DB Connections but does not info…

问题原文

原帖链接 · starchx · CC BY-SA 3.0

In the case of 'too many connections' error, if I do a service httpd restart/reload, would it free up the existing MySQL connections?

Is it possible that when restarting Apache, Apache closes the DB Connections but does not inform mysqld.

Note: mysqld has wait_timeout = 8 hours.

已采纳解答

原帖链接 · Craig Watson · CC BY-SA 3.0

The short answer is yes, but not directly. In any case, I would consider reducing your MySQL wait_timeout to something more realistic like 30 seconds.

Apache is a web-server, it accepts HTTP connection requests and serves content back to the client - it cannot natively connect to a MySQL database.

The most likely source of your connections is an application which generates responses to those requests, i.e. a PHP, Java, Python, etc - you've tagged your question with php so I'll assume PHP here.

Now, also assuming you use Apache's mod_php rather than PHP-FPM, then PHP processes are forked by Apache, and so when you restart Apache, these processes are killed and respawned. If you were running PHP-FPM, restarting Apache would not kill any PHP processes as FPM runs as its own daemon.

In the case of a PHP process being killed via mod_php, it does not call mysql_close() to close the connection directly, and so the connection will be kept open for wait_timeout seconds.

You can tell Apache to do a "graceful" restart and allow existing threads to exit before termination, using service httpd graceful or apachectl -k graceful.

However, if your PHP code is not closing the connection via mysql_close() then that will be the ultimate source of your problem and restarting Apache via any method will not solve the issue.

Also, an important distinction to make with restart vs reload - the former terminates processes, the latter simply reloads configuration. If you really want to terminate processes, use restart.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。