惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tor Project blog
B
Blog RSS Feed
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
GbyAI
GbyAI
N
Netflix TechBlog - Medium
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
W
WeLiveSecurity
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
Cisco Talos Blog
Cisco Talos Blog
Martin Fowler
Martin Fowler
A
About on SuperTechFans
S
Security @ Cisco Blogs
T
Tenable Blog
C
Check Point Blog
N
News and Events Feed by Topic
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Attack and Defense Labs
Attack and Defense Labs
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
P
Palo Alto Networks Blog
V
V2EX
博客园 - 聂微东
Project Zero
Project Zero
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
N
News | PayPal Newsroom
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
Application and Cybersecurity Blog
Application and Cybersecurity Blog
人人都是产品经理
人人都是产品经理
V2EX - 技术
V2EX - 技术
I
Intezer
L
LINUX DO - 最新话题

博客园 - 哈喽哈喽111111

RockyLinux SSH 跳板转发 3389(Windows 远程桌面)完整方案 yum方式安装redis7 nacos新加用户操作 网络运营商禁止端口信息 Linux开机启动rc.local不生效的一般解决方案 Nginx流量拷贝ngx_http_mirror_module模块使用方法详解 Adobe 修改 hosts 文件 axios 投毒与好莱坞式骗术 Linux系统在使用systemctl启动服务的失败,报错如下:Error No space left on device git submodule 的增、查、改、删 Rocky Linux 安装 Google Chrome 浏览器 “头号玩家”—— 美国技术霸权下的全球虚拟货币资产收割行动深层解析 你是第几级 AI 编程 MySQL中通过关联update将一张表的一个字段更新到另外一张表中 Linux 的 Port Knocking 端口碰撞(端口敲门) MySQL解除死锁 jar文件解压缩操作 设置Windows服务器远程桌面能使用多个桌面 20251024- 使用shell脚本分库定时备份MySQL数据 禁用sentinel 在 Linux 中安装和配置 NTP 服务器和 NTP 客户端 springboot配置文件关系及加载顺序 用自带的Nginx为gitlab做白名单 Rocky9和Ubuntu使用pip安装python的库mysqlclient失败解决方式 在Spring Boot Admin中根据Nacos的命名空间来区分和管理不同的环境
MySQL使用自带的logrotate配置日志轮转
哈喽哈喽111111 · 2026-06-16 · via 博客园 - 哈喽哈喽111111
# The log file name and location can be set in
# /etc/my.cnf by setting the "log-error" option
# in [mysqld]  section as follows:
#
# [mysqld]
# log-error=/var/log/mysqld.log
#
# For the mysqladmin commands below to work, root account
# password is required. Use mysql_config_editor(1) to store
# authentication credentials in the encrypted login path file
# ~/.mylogin.cnf
#
# Example usage:
#
#  mysql_config_editor set --login-path=client --user=root --host=localhost --password
#
# When these actions has been done, un-comment the following to
# enable rotation of mysqld's log error.
#

#/var/log/mysqld.log {
#        create 640 mysql mysql
#        notifempty
#        daily
#        rotate 5
#        missingok
#        compress
#    postrotate
#       # just if mysqld is really running
#       if test -x /usr/bin/mysqladmin && \
#          /usr/bin/mysqladmin ping &>/dev/null
#       then
#          /usr/bin/mysqladmin flush-logs
#       fi
#    endscript
#}


一、整段文件整体说明

这段是 MySQL 的 logrotate 日志切割配置模板注释,放在 /etc/logrotate.d/mysqld 文件里,作用:
MySQL 错误日志 /var/log/mysqld.log 会持续变大,logrotate 是 Linux 自带日志轮转工具,自动拆分、压缩、清理旧日志,避免磁盘占满。
所有带 # 开头的都是注释,默认不生效,需要手动修改开启。

1. 日志文件路径说明

# The log file name and location can be set in
# /etc/my.cnf by setting the "log-error" option
# in [mysqld]  section as follows:
#
# [mysqld]
# log-error=/var/log/mysqld.log

含义:
MySQL 错误日志存放路径,是在 MySQL 主配置文件 /etc/my.cnf 的 [mysqld] 段里通过 log-error 参数定义;示例配置日志存在 /var/log/mysqld.log。

    如果你的 my.cnf 里日志路径不是这个,后面 logrotate 配置里的路径也要对应修改。

2. mysqladmin 免密说明(关键)

# For the mysqladmin commands below to work, root account
# password is required. Use mysql_config_editor(1) to store
# authentication credentials in the encrypted login path file
# ~/.mylogin.cnf
#
# Example usage:
#
#  mysql_config_editor set --login-path=client --user=root --host=localhost --password

含义:
后面日志切割完成后,需要执行 mysqladmin flush-logs 让 MySQL 重新生成新日志文件;这条命令需要 MySQL root 密码,直接写明文密码不安全。
官方推荐工具 mysql_config_editor 把账号密码加密存在 ~/.mylogin.cnf 文件,后续执行 mysql/mysqladmin 自动免密登录。

示例命令解释:

mysql_config_editor set --login-path=client --user=root --host=localhost --password

执行后会交互式输入 root 密码,加密保存,后续不用输密码。

3. 开启日志轮转提示

# When these actions has been done, un-comment the following to
# enable rotation of mysqld's log error.

含义:
完成上面免密配置后,把下面大括号那段配置前面的 # 注释删掉,日志轮转功能才会生效。

4. logrotate 配置块详解(被注释的核心配置)

#/var/log/mysqld.log {
#        create 640 mysql mysql
#        notifempty
#        daily
#        rotate 5
#        missingok
#        compress
#    postrotate
#       # just if mysqld is really running
#       if test -x /usr/bin/mysqladmin && \
#          /usr/bin/mysqladmin ping &>/dev/null
#       then
#          /usr/bin/mysqladmin flush-logs
#       fi
#    endscript
#}

逐行参数解释:

    /var/log/mysqld.log
    要切割的日志文件路径,和 my.cnf 中 log-error 保持一致。
    create 640 mysql mysql
    切割完成后新建空日志文件:权限 640,属主 mysql,属组 mysql。
    notifempty
    如果日志为空,不执行轮转,避免生成空日志备份。
    daily
    按天切割日志(可选 weekly/monthly)。
    rotate 5
    只保留最近 5 份历史日志,超过 5 个自动删除旧文件。
    missingok
    如果日志文件不存在,不报错、直接跳过。
    compress
    旧日志自动 gz 压缩,节省磁盘空间。
    postrotate ... endscript
    日志切割完成后执行的脚本:
        test -x /usr/bin/mysqladmin:判断 mysqladmin 工具是否存在可执行
        mysqladmin ping:测试 MySQL 服务是否正常运行
        mysqladmin flush-logs:刷新 MySQL 日志,关闭旧日志句柄,写入新日志文件
        如果不执行这条,MySQL 还会往已经被切割走的旧文件写日志,轮转失效。

二、完整操作步骤(一步一步照着执行)

步骤 1:确认 MySQL 错误日志路径
编辑 MySQL 配置

vim /etc/my.cnf

找到 [mysqld] 下的 log-error:

[mysqld]
log-error=/var/log/mysqld.log

记下路径,如果不是 /var/log/mysqld.log,后面要同步修改。


步骤 2:配置 MySQL root 免密登录(必须做)

执行加密凭证命令:

mysql_config_editor set --login-path=client --user=root --host=localhost --password

回车后输入你的 MySQL root 密码,密码会加密存到 root家目录/.mylogin.cnf,不会明文泄露。
验证免密是否生效:

mysqladmin ping

输出 mysqld is alive 代表免密成功。

步骤 3:开启 logrotate MySQL 轮转配置
打开日志轮转配置文件(一般是 /etc/logrotate.d/mysqld)

vim /etc/logrotate.d/mysqld

把最下方配置块前面所有 # 全部删除,修改后完整内容:

/var/log/mysqld.log {
        create 640 mysql mysql
        notifempty
        daily
        rotate 5
        missingok
        compress
    postrotate
       # just if mysqld is really running
       if test -x /usr/bin/mysqladmin && \
          /usr/bin/mysqladmin ping &>/dev/null
       then
          /usr/bin/mysqladmin flush-logs
       fi
    endscript
}

    注意:如果步骤 1 查到日志路径不是 /var/log/mysqld.log,第一行路径同步替换。

步骤 4:手动测试日志轮转是否正常(关键,避免定时报错)

logrotate -vf /etc/logrotate.d/mysqld

参数说明:

    -v 打印详细执行日志
    -f 强制立即执行切割
    执行无报错,且 /var/log/ 下生成 mysqld.log.1.gz 压缩文件,代表配置成功。

步骤 5:补充说明自动执行机制

logrotate 由系统定时任务自动调度:

    CentOS/RHEL:/etc/cron.daily/logrotate,每天凌晨自动执行

    无需额外启动服务,系统自带定时任务

三、常见问题补充

    执行 mysqladmin ping 提示拒绝访问
    代表免密配置失败,重新执行 mysql_config_editor 输入正确 root 密码;
    日志切割后 MySQL 不写入新日志
    多半是 postrotate 里 flush-logs 没执行成功,检查免密、mysql 服务状态;
    想修改保存日志份数 / 周期
        rotate 5:数字改成想要保留的份数
        daily 改成 weekly 每周切割 / monthly 每月切割;
    权限报错
    确认日志文件夹 /var/log/ 权限正常,mysql 用户有读写权限。