6人参与 • 2026-03-19 • Mysql
基于 145 环境 mysql-0d130fde 实例验证
实例信息:
audit_log active audit audit_log.so gpl
| 参数 | 值 | 说明 |
|---|---|---|
| audit_log_policy | all | 当前审计级别(全量) |
| audit_log_format | json | 日志格式 |
| audit_log_file | /var/log/mysql/audit.log | 日志文件路径 |
| audit_log_rotate_on_size | 536870912 (512mb) | 日志轮转大小 |
| audit_log_rotations | 10 | 保留日志文件数量 |
| audit_log_strategy | asynchronous | 异步写入策略 |
| audit_log_buffer_size | 1048576 (1mb) | 缓冲区大小 |
| audit_log_max_sqltext | 2048 | sql最大记录长度 |
| audit_log_exclude_accounts | root@%,root@localhost,repl@%,@ | 排除审计账户 |
| audit_log_display_query_time | on | 显示查询耗时 |
percona audit plugin 支持 4 种审计策略(policy):
| policy | 等级 | 审计内容 |
|---|---|---|
| none | level 0 | 关闭审计 |
| logins | level 1 | 连接事件 + 默认查询记录 |
| queries | level 2 | 所有 sql 查询(不含连接) |
| all | level 3 | 连接 + 查询(完整审计) |
注意:percona 的 logins 级别仍会记录 query,这是与 mariadb audit plugin 的区别。
设置方式
set global audit_log_policy=none;
特点
验证结果
# 执行查询 select 1; # 审计日志:无新增记录
设置方式
set global audit_log_policy=logins;
记录内容
适用场景
审计日志示例
{"name":"connect","record":"464","timestamp":"2025-12-24t09:48:53z","user":"audit_test","host":"localhost"}
{"name":"query","record":"466","timestamp":"2025-12-24t09:48:53z","command_class":"select","sqltext":"select 1"}
{"name":"quit","record":"468","timestamp":"2025-12-24t09:48:53z","user":"audit_test"}
性能影响
设置方式
set global audit_log_policy=queries;
记录内容
适用场景
审计日志示例
{"name":"query","record":"471","timestamp":"2025-12-24t09:49:03z","command_class":"select","sqltext":"select 1"}
{"name":"query","record":"472","timestamp":"2025-12-24t09:49:03z","command_class":"delete","sqltext":"delete from test_audit.t where id=1"}
性能影响
设置方式
set global audit_log_policy=all;
记录内容
适用场景
审计日志示例
{"name":"connect","record":"474","timestamp":"2025-12-24t09:49:16z","user":"audit_test","host":"localhost"}
{"name":"query","record":"475","timestamp":"2025-12-24t09:49:16z","command_class":"select","sqltext":"select @@version_comment limit 1"}
{"name":"query","record":"476","timestamp":"2025-12-24t09:49:16z","command_class":"select","sqltext":"select * from test_audit.t"}
{"name":"quit","record":"477","timestamp":"2025-12-24t09:49:16z","user":"audit_test"}
性能影响
| 字段 | 说明 |
|---|---|
| name | 事件类型(connect/query/quit/table/audit) |
| timestamp | utc 时间戳 |
| connection_id | 连接 id |
| user | 执行用户 |
| host | 连接来源主机 |
| ip | 客户端 ip |
| db | 数据库名 |
| command_class | sql 类型(select/insert/update/delete/drop等) |
| sqltext | 完整 sql 文本 |
| status | 执行状态码(0=成功) |
| start_time | 查询开始时间(微秒) |
| end_time | 查询结束时间(微秒) |
当前环境已配置排除 root 用户:
-- 查看排除列表 show variables like 'audit_log_exclude_accounts'; -- 设置排除账户 set global audit_log_exclude_accounts='root@%,root@localhost,repl@%,@';
-- 只审计特定库 set global audit_log_include_databases='business_db,core_db';
-- 只审计 dml set global audit_log_include_commands='insert,update,delete';
-- 单文件最大 512mb set global audit_log_rotate_on_size=536870912; -- 保留 10 个历史文件 set global audit_log_rotations=10;
推荐配置:logins + 慢日志
set global audit_log_policy=logins; set global long_query_time=1;
优势
推荐配置:queries + 定期清理
set global audit_log_policy=queries;
配合措施
-- 长期开启 all,配合 audit_log_strategy=asynchronous set global audit_log_policy=all; set global audit_log_strategy=asynchronous;
风险
| 日志类型 | 安全合规 | 数据恢复 | 性能分析 | select记录 |
|---|---|---|---|---|
| 审计日志 | ✅ | ❌ | ❌ | 可选 |
| binlog | ❌ | ✅ | ❌ | ❌ |
| 慢日志 | ❌ | ❌ | ✅ | 仅慢sql |
a: percona audit plugin 的 logins 级别行为与 mariadb 不同,会同时记录连接和查询。如需只记录连接,需使用 audit_log_exclude_commands 过滤。
set global audit_log_policy=none;
审计插件会自动轮转,但建议配置监控:
# 检查审计日志大小 ls -lh /var/log/mysql/audit.log* # 设置定期清理任务 find /var/log/mysql/audit.log* -mtime +30 -delete
实测数据(参考):
[mysqld] # 加载审计插件(通常已预装) plugin-load=audit_log.so # 审计策略 audit_log_policy=logins audit_log_format=json audit_log_file=/var/log/mysql/audit.log # 轮转配置 audit_log_rotate_on_size=536870912 audit_log_rotations=9 # 排除管理账户(避免日志膨胀) audit_log_exclude_accounts=root@%,root@localhost,repl@%,@ # 性能优化 audit_log_strategy=asynchronous audit_log_buffer_size=1048576 # sql 记录配置 audit_log_max_sqltext=2048 audit_log_display_query_time=on
到此这篇关于mysql 审计级别配置实现步骤的文章就介绍到这了,更多相关mysql 审计级别配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论