MySQL general_log日志抓取业务SQL语句
一、基本介绍
业务生产环境中,我们经常会遇到一些比较棘手的异常问题,比如,DB频发crash却无法定位到原因;业务库QPS或IO吞吐非常高,但是业务量并未有增长等等情况,今天我将借助general_log日志抓取mysqldump的执行过程,来向大家介绍一下general_log功能的用法。
友情提示:开启general_log会记录MySQL数据库执行的所有SQL语句,会额外增加数据库的性能消耗。对于比较空闲的数据库影响不大,但针对比较繁忙的、IO吞吐量比较大的数据库来说,可能会感知比较明显。请根据业务容忍度,谨慎评估,开启时机和开启时长。
官网地址:https://dev.mysql.com/doc/refman/5.7/en/query-log.html
二、实验操作
2.1 配置general_log
#(1)开启general_log mysql> set global general_log=on; mysql> show variables like '%general%'; +------------------+----------------------------+ | Variable_name | Value | +------------------+----------------------------+ | general_log | ON | | general_log_file | /data/mysql/log/mysqld.log | +------------------+----------------------------+ 2 rows in set (0.01 sec) #(2)设置general_log存储格式为FILE mysql> set global log_output=file; Query OK, 0 rows affected (0.01 sec) mysql> show variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | +---------------+-------+ 1 row in set (0.01 sec)
注:general_log和slow_log一样,支持两种存储模式,TABLE和FILE,这里我为了方便vim编辑/过滤和查看,就使用了File模式。
2.2 抓取mysqldump在从库备份时的参数特性
官网介绍:https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_dump-slave
- 注1:根据mysqldump的特性,mysqldump在从库备份时,加入了--dump-slave的参数,这个参数会在备份过程中自动断开sql_thread,这个时候从库是没有新的数据写入的(主库过来的事务无法被写入)
- 注2:该参数用于在从服务器导出dmp文件的同时,记录主服务器的二进制文件及位置,便于执行时间点恢复操作。
#(1)执行备份操作 [root@blogs-v2 ~]# mysqldump -uroot -h192.168.0.24 -pUcloudcn --single-transaction --dump-slave=2 --databases starcto > starcto.sql #(2)查看备份期间general_log日志输出 [root@192-168-0-103 ~]# vim /data/mysql/log/mysqld.log 2022-08-08T11:38:42.753759+08:00 1363 Connect root@10.25.25.25 on using TCP/IP 2022-08-08T11:38:42.753922+08:00 1363 Query /*!40100 SET @@SQL_MODE='' */ 2022-08-08T11:38:42.754984+08:00 1363 Query SHOW VARIABLES LIKE 'gtid\_mode' 2022-08-08T11:38:42.756997+08:00 1363 Query SELECT @@GLOBAL.GTID_EXECUTED 2022-08-08T11:38:42.757109+08:00 1363 Query SHOW SLAVE STATUS 2022-08-08T11:38:42.757249+08:00 1363 Query UNLOCK TABLES 2022-08-08T11:38:42.757384+08:00 1363 Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('starcto'))) GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, INITIAL_SIZE ORDER BY LOGFILE_GROUP_NAME 2022-08-08T11:38:42.758173+08:00 1363 Query SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('starcto')) ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME 2022-08-08T11:38:42.758651+08:00 1363 Query SHOW VARIABLES LIKE 'ndbinfo\_version' 2022-08-08T11:38:42.760160+08:00 1363 Init DB starcto 2022-08-08T11:38:42.760252+08:00 1363 Query SHOW CREATE DATABASE IF NOT EXISTS `starcto` 2022-08-08T11:38:42.760345+08:00 1363 Query SAVEPOINT sp 2022-08-08T11:38:42.760429+08:00 1363 Query show tables 2022-08-08T11:38:42.760570+08:00 1363 Query show table status like 'account' 2022-08-08T11:38:42.760768+08:00 1363 Query SET SQL_QUOTE_SHOW_CREATE=1 2022-08-08T11:38:42.760863+08:00 1363 Query SET SESSION character_set_results = 'binary' 2022-08-08T11:38:42.760953+08:00 1363 Query show create table `account` 2022-08-08T11:38:42.761057+08:00 1363 Query SET SESSION character_set_results = 'utf8' 2022-08-08T11:38:42.761147+08:00 1363 Query show fields from `account` 2022-08-08T11:38:42.761373+08:00 1363 Query show fields from `account` 2022-08-08T11:38:42.761597+08:00 1363 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `account` 2022-08-08T11:38:42.761778+08:00 1363 Query SET SESSION character_set_results = 'binary' 2022-08-08T11:38:42.761865+08:00 1363 Query use `starcto` 2022-08-08T11:38:42.761951+08:00 1363 Query select @@collation_database 2022-08-08T11:38:42.762045+08:00 1363 Query SHOW TRIGGERS LIKE 'account' 2022-08-08T11:38:42.762278+08:00 1363 Query SET SESSION character_set_results = 'utf8' 2022-08-08T11:38:42.762363+08:00 1363 Query ROLLBACK TO SAVEPOINT sp 2022-08-08T11:38:42.762452+08:00 1363 Query RELEASE SAVEPOINT sp 2022-08-08T11:38:42.762535+08:00 1363 Query SHOW SLAVE STATUS 2022-08-08T11:38:42.762665+08:00 1363 Query START SLAVE 2022-08-08T11:38:42.763592+08:00 1363 Quit
作者:UStarGao
链接:https://www.starcto.com/mysql/308.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
UCloud云平台推荐
随便看看
- 2021-07-21MySQL主从同步异常之从库误写数据
- 2021-03-27MySQL GTID全局事物标识
- 2021-10-08Redis容器化部署教程
- 2021-09-07Linux远程拷贝数据-限速和断点续传
- 2022-01-15K8S Pod模板介绍