MySQL内存占用过高+php-cgi CPU过载致服务器崩溃求助
服务器崩溃排查与慢查询日志使用指导
当检测30个URL的HTTP状态时,服务器直接崩溃——仅前5-6个资源返回200,其余全部返回服务器错误。触发请求时通过top命令观察Apache,发现5-6个php-cgi进程占据CPU前列,每个进程CPU负载超5%。搭建的是WordPress站点,怀疑MySQL内存占用过高与php-cgi CPU过载共同导致服务器崩溃。
服务器配置
vCPU/s:1 vCPU RAM:2048.00 MB Storage: 64 GB NVMe
MySQL Tuner检测结果
perl mysqltuner.pl >> MySQLTuner 2.0.9 * Jean-Marie Renouard <jmrenouard@gmail.com> * Major Hayden <major@mhtx.net> >> Bug reports, feature requests, and downloads at http://mysqltuner.pl/ >> Run with '--help' for additional options and output filtering [--] Skipped version check for MySQLTuner script [OK] Currently running supported MySQL version 5.7.40-log [OK] Operating on 64-bit architecture -------- Log file Recommendations ------------------------------------------------------------------ [OK] Log file /var/log/mysqld.log exists [--] Log file: /var/log/mysqld.log (5M) [OK] Log file /var/log/mysqld.log is not empty [OK] Log file /var/log/mysqld.log is smaller than 32 Mb [OK] Log file /var/log/mysqld.log is readable. [!!] /var/log/mysqld.log contains 4595 warning(s). [!!] /var/log/mysqld.log contains 1470 error(s). [--] 126 start(s) detected in /var/log/mysqld.log [--] 1) 2022-11-26T16:51:46.008951Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 2) 2022-11-26T14:25:04.411192Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 3) 2022-11-26T14:00:44.505181Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 4) 2022-11-26T13:41:24.726384Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 5) 2022-11-26T13:30:30.052935Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 6) 2022-11-26T13:14:52.946860Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 7) 2022-11-26T13:10:52.597525Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 8) 2022-11-26T13:06:44.314567Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 9) 2022-11-26T12:59:06.893587Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 10) 2022-11-26T12:56:43.639611Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 47 shutdown(s) detected in /var/log/mysqld.log [--] 1) 2022-11-26T16:51:45.487321Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 2) 2022-11-26T14:25:03.877227Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 3) 2022-11-26T14:00:44.020059Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 4) 2022-11-26T14:00:41.779016Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 5) 2022-11-26T13:37:23.080832Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 6) 2022-11-26T13:37:22.580337Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 7) 2022-11-26T13:37:22.111923Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 8) 2022-11-26T13:37:21.666683Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 9) 2022-11-26T13:37:21.130343Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 10) 2022-11-26T13:30:29.577245Z 0 [Note] /usr/sbin/mysqld: Shutdown complete -------- Storage Engine Statistics ----------------------------------------------------------------- [--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA [--] Data in MyISAM tables: 200.8M (Tables: 137) [--] Data in InnoDB tables: 16.0K (Tables: 1) [OK] Total fragmented tables: 0 -------- Analysis Performance Metrics -------------------------------------------------------------- [--] innodb_stats_on_metadata: OFF [OK] No stat updates during querying INFORMATION_SCHEMA. -------- Views Metrics ----------------------------------------------------------------------------- -------- Triggers Metrics -------------------------------------------------------------------------- -------- Routines Metrics -------------------------------------------------------------------------- -------- Security Recommendations ------------------------------------------------------------------ [OK] No Role user detected [OK] There are no anonymous accounts for any database users [OK] All database users have passwords assigned [!!] There is no basic password file list! -------- CVE Security Recommendations -------------------------------------------------------------- [--] Skipped due to --cvefile option undefined -------- Performance Metrics ----------------------------------------------------------------------- [--] Up for: 14m 56s (5K q [6.276 qps], 182 conn, TX: 48M, RX: 731K) [--] Reads / Writes: 99% / 1% [--] Binary logging is disabled [--] Physical Memory : 1.8G [--] Max MySQL memory : 13.4G [--] Other process memory: 0B [--] Total buffers: 682.0M global + 260.6M per thread (50 max threads) [--] Performance_schema Max memory usage: 0B [--] Galera GCache Max memory usage: 0B [!!] Maximum reached memory usage: 1.7G (93.83% of installed RAM) [!!] Maximum possible memory usage: 13.4G (746.15% of installed RAM) [!!] Overall possible memory usage with other process exceeded memory [OK] Slow queries: 0% (0/5K) [OK] Highest usage of available connections: 8% (4/50) [OK] Aborted connections: 0.55% (1/182) [!!] CPanel and Flex system skip-name-resolve should be on [OK] Query cache is disabled by default due to mutex contention on multiprocessor machines. [OK] Sorts requiring temporary tables: 0% (0 temp sorts / 1K sorts) [OK] No joins without indexes [OK] Temporary tables created on disk: 13% (544 on disk / 4K total) [OK] Thread cache hit rate: 97% (4 created / 182 connections) [OK] Table cache hit rate: 61% (8K hits / 14K requests) [OK] table_definition_cache (912) is greater than number of tables (418) [OK] Open file limit used: 1% (780/40K) [OK] Table locks acquired immediately: 100% (5K immediate / 5K locks) -------- Performance schema ------------------------------------------------------------------------ [!!] Performance_schema should be activated. [--] Sys schema is installed. -------- ThreadPool Metrics ------------------------------------------------------------------------ [--] ThreadPool stat is disabled. -------- MyISAM Metrics ---------------------------------------------------------------------------- [!!] Key buffer used: 18.8% (24.1M used / 128.0M cache) [OK] Key buffer size / total MyISAM indexes: 128.0M/11.6M [OK] Read Key buffer hit rate: 99.1% (80K cached / 734 reads) [OK] Write Key buffer hit rate: 100.0% (166 cached / 166 writes) -------- InnoDB Metrics ---------------------------------------------------------------------------- [--] InnoDB is enabled. [--] InnoDB Thread Concurrency: 0 [OK] InnoDB File per table is activated [OK] InnoDB buffer pool / data size: 512.0M / 16.0K [!!] Ratio InnoDB log file size / InnoDB Buffer pool size (6.25%): 16.0M * 2 / 512.0M should be equal to 25% [OK] InnoDB buffer pool instances: 1 [--] Number of InnoDB Buffer Pool Chunk: 4 for 1 Buffer Pool Instance(s) [OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances [OK] InnoDB Read buffer efficiency: 97.50% (15884 hits / 16291 total) [!!] InnoDB Write Log efficiency: 0% (2 hits / 0 total) [OK] InnoDB log waits: 0.00% (0 waits / 2 writes) -------- Aria Metrics ------------------------------------------------------------------------------ [--] Aria Storage Engine not available. -------- TokuDB Metrics ---------------------------------------------------------------------------- [--] TokuDB is disabled. -------- XtraDB Metrics ---------------------------------------------------------------------------- [--] XtraDB is disabled. -------- Galera Metrics ---------------------------------------------------------------------------- [--] Galera is disabled. -------- Replication Metrics ----------------------------------------------------------------------- [--] Galera Synchronous replication: NO [--] No replication slave(s) for this server. [--] Binlog format: ROW [--] XA support enabled: ON [--] Semi synchronous replication Master: Not Activated [--] Semi synchronous replication Slave: Not Activated [--] This is a standalone server -------- Recommendations --------------------------------------------------------------------------- General recommendations: Check warning line(s) in /var/log/mysqld.log file Check error line(s) in /var/log/mysqld.log MySQL was started within the last 24 hours: recommendations may be inaccurate Reduce your overall MySQL memory footprint for system stability Dedicate this server to your database for highest performance. name resolution is enabled due to cPanel doesn't support this disabled. Performance schema should be activated for better diagnostics Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: Variables to adjust: *** MySQL's maximum memory usage is dangerously high *** *** Add RAM before increasing MySQL buffer variables *** skip-name-resolve=0 performance_schema=ON key_buffer_size (~ 25M) innodb_log_file_size should be (=64M) if possible, so InnoDB total log files size equals 25% of buffer pool size.
MySQL CNF配置
[mysqld] performance-schema=0 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid innodb_buffer_pool_size = 500M innodb_flush_log_at_trx_commit = 1 innodb_log_file_size = 16M # innodb_additional_mem_pool_size = 32M innodb_buffer_pool_instances = 1 innodb_log_buffer_size = 10M max_connections = 50 wait_timeout = 3600 interactive_timeout = 3600 join_buffer_size = 2M sort_buffer_size = 2M read_rnd_buffer_size = 2M key_buffer_size = 128M max_allowed_packet=268435456 read_rnd_buffer_size = 256K # thread_stac = 256K thread_cache_size = 8 open_files_limit = 3000 table_open_cache = 1024 default-storage-engine = MyISAM innodb_file_per_table = 1 query_cache_type = 0 query_cache_limit = 0 query_cache_size = 0 max_heap_table_size = 32M tmp_table_size = 32M general_log_file = /var/log/mysql/mysql.log general_log = 0 slow-query-log = 1 slow-query-log-file = /var/log/mysql/mysql-slow.log long_query_time = 1 log-queries-not-using-indexes = 1
慢查询日志使用指导
1. 确认日志状态
你的配置已开启慢查询日志,先检查日志文件是否存在并生成内容:
ls -lh /var/log/mysql/mysql-slow.log tail -20 /var/log/mysql/mysql-slow.log
2. 解读日志关键信息
每条慢查询记录重点关注:
Query_time:查询执行时长,数值越大优先级越高Lock_time:表锁等待时长,过长说明存在锁竞争Rows_sent与Rows_examined:若扫描行数远大于返回行数,说明查询效率极低(如全表扫描)- 实际SQL语句:定位到WordPress对应的主题/插件功能
3. 针对性优化
结合1核2G的服务器配置,优先处理:
- MySQL内存过载:
- 将
key_buffer_size从128M降至25M(符合MySQLTuner建议) - 将
innodb_buffer_pool_size从500M降至256M(你的InnoDB数据仅16K,无需过大缓冲池) - 将
max_connections降至20(1核服务器无法支撑50个并发连接)
- 将
- php-cgi CPU过载:
- 禁用未使用的插件,尤其是频繁查询数据库的统计、SEO类插件
- 安装缓存插件减少动态页面的数据库查询次数
- 给WordPress核心表(如wp_posts、wp_postmeta)添加缺失索引
4. 验证优化效果
调整配置后重启服务:
systemctl restart mysqld systemctl restart httpd
重复URL检测,观察服务器负载与请求返回状态。
内容的提问来源于stack exchange,提问作者RocketBrian
相关产品推荐
相关产品推荐

