Magento 服务器负载居高不下问题排除

文章目录

    上上周遇到的一个 Magento 服务器问题。

    问题现象

    服务器负载居高不下,MySQL 数据库 CPU 占用很高,从 show processlist 的结果看,看不出什么问题。但是通过统计执行的 SQL 总量看,每秒钟几千个 SQL 查询。而且偶尔有执行长时间的大型联表查询语句。而产品表有近两百万的数据量。

    经过一顿毫无头绪的排除。大概定位到是 magento 索引失效的问题,需要重建索引。

    但是,重建命令,看不到进度,第一次执行,不知道为什么失败了。于是尝试,逐个手动重建索引。

    重建索引前停掉 php fpm

    sudo lnmp php-fpm stop
    

    主要是为了防止请求进来,继续造成系统高负载。

    重建后再启动

    sudo lnmp php-fpm start
    

    执行命令

    以产品跟属性的索引为例:

    php bin/magento indexer:reindex catalog_product_attribute -vvv
    

    也可以一次性执行多个:

    $ php bin/magento indexer:reindex catalogrule_product catalogrule_rule catalog_category_product customer_grid design_config_grid inventory catalog_product_category cataloginventory_stock -vvv
    

    执行结果

    Design Config Grid index has been rebuilt successfully in 00:00:00
    Customer Grid index has been rebuilt successfully in 00:00:00
    Category Products index has been rebuilt successfully in 00:21:15
    Product Categories index has been rebuilt successfully in 00:00:00
    Catalog Rule Product index has been rebuilt successfully in 00:00:00
    Stock index has been rebuilt successfully in 00:01:59
    Inventory index has been rebuilt successfully in 00:00:00
    Catalog Product Rule index has been rebuilt successfully in 00:00:00
    Product Price index has been rebuilt successfully in 00:21:29
    Catalog Search index has been rebuilt successfully in 01:14:03
    

    那个 catalog_product_attribute Product EAV 执行时间最长,执行了三四天。所以最好在 screen 中执行。

    查看索引状态

    $ php bin/magento indexer:status
    +---------------------------+----------------------+--------+-----------+---------------------+---------------------+
    | ID                        | Title                | Status | Update On | Schedule Status     | Schedule Updated    |
    +---------------------------+----------------------+--------+-----------+---------------------+---------------------+
    | catalogrule_product       | Catalog Product Rule | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    | catalogrule_rule          | Catalog Rule Product | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    | catalogsearch_fulltext    | Catalog Search       | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 14:02:10 |
    | catalog_category_product  | Category Products    | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    | customer_grid             | Customer Grid        | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    | design_config_grid        | Design Config Grid   | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    | inventory                 | Inventory            | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    | catalog_product_category  | Product Categories   | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    | catalog_product_attribute | Product EAV          | Ready  | Schedule  | idle (0 in backlog) | 2026-06-22 10:09:25 |
    | catalog_product_price     | Product Price        | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 12:44:58 |
    | cataloginventory_stock    | Stock                | Ready  | Schedule  | idle (0 in backlog) | 2026-06-14 09:28:02 |
    +---------------------------+----------------------+--------+-----------+---------------------+---------------------+
    

    重建索引后,系统负载确实明显降了下来。

    排查的细节后续补充一下,今晚安装 RuoYi Vue 的环境,折腾了一晚上,明天要早起,先睡了。

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式