自学内容网 自学内容网

Mysql数据库SQL语句执行日志

Mysql数据库SQL语句执行日志

MySQL 日志类型

日志类型功能用途相关变量(Variable_name
错误日志记录服务器启动、关闭及运行时的错误。排查服务异常问题log_error
通用查询日志记录所有客户端与服务器的交互 SQL 语句。调试 SQL 查询general_log
general_log_file
慢查询日志记录执行时间超过指定阈值的查询操作。性能优化slow_query_log
slow_query_log_file
long_query_time
二进制日志记录所有数据更改操作(INSERTUPDATE 等)。数据恢复、主从复制log_bin
binlog_format
expire_logs_days
中继日志存储从服务器接收的二进制日志副本。主从复制从服务器relay_log
relay_log_info_file
事务日志记录事务的修改操作,用于崩溃恢复。保证事务持久性innodb_log_file_size
innodb_log_files_in_group
innodb_flush_log_at_trx_commit
审计日志记录用户活动(需插件支持)。安全审计与合规需安装 audit_log 插件
DDL 日志记录表结构修改操作(CREATEALTER 等)。审计与监控表结构变更依赖审计插件或日志解析

动态查看、开启和关闭日志的命令

1. 错误日志

  • 查看错误日志路径:
    SHOW VARIABLES LIKE 'log_error';
    
  • 错误日志无法通过动态命令开启/关闭,需要通过配置文件设置。

2. 通用查询日志

  • 查看状态和路径:
    SHOW VARIABLES LIKE 'general_log%';
    
  • 动态开启:
    SET GLOBAL general_log = 'ON';
    
  • 动态关闭:
    SET GLOBAL general_log = 'OFF';
    

3. 慢查询日志

  • 查看状态、路径和慢查询阈值:
    SHOW VARIABLES LIKE 'slow_query_log%';
    SHOW VARIABLES LIKE 'long_query_time';
    
  • 动态开启:
    SET GLOBAL slow_query_log = 'ON';
    SET GLOBAL long_query_time = 2;  -- 设置慢查询阈值(单位:秒)
    
  • 动态关闭:
    SET GLOBAL slow_query_log = 'OFF';
    

4. 二进制日志

  • 查看状态和配置:
    SHOW VARIABLES LIKE 'log_bin%';
    SHOW VARIABLES LIKE 'binlog_format';
    
  • 二进制日志无法通过动态命令开启/关闭,需要通过配置文件设置。

查询SQL执行日志

第一步 查看日志开关情况

mysql> show variables like "general_log%";
+------------------+---------------------------------+
| Variable_name    | Value                           |
+------------------+---------------------------------+
| general_log      | OFF                             |
| general_log_file | /var/lib/mysql/7cee0e439963.log |
+------------------+---------------------------------+
2 rows in set (0.00 sec)

第二步 开启通用日志

mysql> SET GLOBAL general_log = 'ON';
Query OK, 0 rows affected (0.02 sec)

mysql> show variables like "general_log%";
+------------------+---------------------------------+
| Variable_name    | Value                           |
+------------------+---------------------------------+
| general_log      | ON                              |
| general_log_file | /var/lib/mysql/7cee0e439963.log |
+------------------+---------------------------------+
2 rows in set (0.00 sec)

第三步 查看日志

$ docker exec -it vg-mysql bash
root@7cee0e439963:/# tail -f /var/lib/mysql/7cee0e439963.log
2024-11-20T12:52:14.331131Z        11 Query     UPDATE `t_config_dict` SET `default`='100' WHERE `domain` = 'license' AND `key` = 'max_channels'
2024-11-20T12:52:14.331460Z        18 Query     UPDATE `t_config_dict` SET `default`='60' WHERE `domain` = 'license' AND `key` = 'max_users'
2024-11-20T12:52:14.331671Z        25 Query     UPDATE `t_config_dict` SET `default`='1729070899' WHERE `domain` = 'license' AND `key` = 'auth_ts'
2024-11-20T12:52:14.331912Z        23 Query     UPDATE `t_config_dict` SET `default`='1754990899' WHERE `domain` = 'license' AND `key` = 'expire_ts'
2024-11-20T12:52:24.341919Z        15 Query     UPDATE `t_config_dict` SET `default`='100' WHERE `domain` = 'license' AND `key` = 'max_channels'
2024-11-20T12:52:24.342863Z        21 Query     UPDATE `t_config_dict` SET `default`='60' WHERE `domain` = 'license' AND `key` = 'max_users'
2024-11-20T12:52:24.343620Z        19 Query     UPDATE `t_config_dict` SET `default`='1729070899' WHERE `domain` = 'license' AND `key` = 'auth_ts'
2024-11-20T12:52:24.344267Z        17 Query     UPDATE `t_config_dict` SET `default`='1754990899' WHERE `domain` = 'license' AND `key` = 'expire_ts'
2024-11-20T12:52:34.354779Z        10 Query     UPDATE `t_config_dict` SET `default`='100' WHERE `domain` = 'license' AND `key` = 'max_channels'
2024-11-20T12:52:34.355687Z        20 Query     UPDATE `t_config_dict` SET `default`='60' WHERE `domain` = 'license' AND `key` = 'max_users'
2024-11-20T12:52:34.356523Z        13 Query     UPDATE `t_config_dict` SET `default`='1729070899' WHERE `domain` = 'license' AND `key` = 'auth_ts'
2024-11-20T12:52:34.357383Z        27 Query     UPDATE `t_config_dict` SET `default`='1754990899' WHERE `domain` = 'license' AND `key` = 'expire_ts'
2024-11-20T12:52:44.366886Z        14 Query     UPDATE `t_config_dict` SET `default`='100' WHERE `domain` = 'license' AND `key` = 'max_channels'
2024-11-20T12:52:44.367307Z        22 Query     UPDATE `t_config_dict` SET `default`='60' WHERE `domain` = 'license' AND `key` = 'max_users'
2024-11-20T12:52:44.367476Z        11 Query     UPDATE `t_config_dict` SET `default`='1729070899' WHERE `domain` = 'license' AND `key` = 'auth_ts'
2024-11-20T12:52:44.367629Z        18 Query     UPDATE `t_config_dict` SET `default`='1754990899' WHERE `domain` = 'license' AND `key` = 'expire_ts'

查看mysql正在执行的sql语句

watch -n 0.5 "mysql -h127.0.0.1 -P33306 -uroot -pmysql -e 'SHOW FULL PROCESSLIST;'"

注:需要替换mysql登录信息

效果如下:
在这里插入图片描述


原文地址:https://blog.csdn.net/yjkhtddx/article/details/143924551

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!