Mysql-日志

日志

errlog错误日志

作用

记录Mysql从启动以来,所有的状态,警告,错误。

DBA靠此定位数据库问题。

配置

默认是开启,

vim /etc/my.cnf

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

binlog二进制日志

作用

主要记录数据库变化的日志。

数据恢复,主从复制中应用

#查看再用的binlogfile
show master status;
#查看binlog文件中的事件
show binlog events in 'bin.0004'

#筛选日志
mysql -e "show binlog events in 'bin.0004'" |grep xx

配置

vim /etc/my.cnf
server_id=4                      #主机编号:5.7后要加此参数
log_bin=/data/mysql/mysql-bin    #mysql-bin为前缀例如:mysql-bin001
sync_binlog=1                    # binlog日志刷盘策略
binlog_format=row                #binlog记录格式为row模式

binlog只记录变更的SQL,不记录查询语句。

1.DDL 原封不动的记录SQL

2.DCL 原封不动的记录SQL

3.DML 只记录已提交的DML

DML三种记录方式

1.statement(5.6默认) SBR(statement based replication)原封不动的记录SQL

2.ROW(5.7默认值) RBR(row based replication),记录数据行变化,用户看不懂,需要工具分析。

3.mixed(混合) MBR(mixed based replication):以上2种混合

查询日志

读取日志到a.sql,这种能够看到DDL,DCL但是DML事务的是乱码 。

mysqlbinlog binlog.000004 > /root/a.sql

需要这种方式才能看到DML的具体代码

mysqlbinlog --base64-output=decode-row -vvv  binlog.000004 > /root/b.sql

一个事务,begin开始,commit结束,中间的语句是,insert into jinwei.t1 value(33)

### INSERT INTO `jinwei`.`t1`
### SET
###   @1=33 /* INT meta=0 nullable=1 is_null=0 */
# at 811
#210204 13:12:25 server id 1  end_log_pos 888 CRC32 0xf6dba010  Query   thread_id=13    exec_time=0     error_code=0
SET TIMESTAMP=1612415545/*!*/;
BEGIN
/*!*/;
# at 888
#210204 13:12:25 server id 1  end_log_pos 938 CRC32 0x2324d32c  Table_map: `jinwei`.`t1` mapped to number 96
# at 938
#210204 13:12:25 server id 1  end_log_pos 978 CRC32 0xf54e2e4b  Write_rows: table id 96 flags: STMT_END_F
### INSERT INTO `jinwei`.`t1`
### SET
###   @1=33 /* INT meta=0 nullable=1 is_null=0 */
# at 978
#210204 13:12:25 server id 1  end_log_pos 1009 CRC32 0x1ac1d8f2         Xid = 82
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;

日志截取

确定要截取的日志,开始到结束位置。

mysqlbinlog --start-position=233 --stop-position=702 /data/mysql/binlog.000005 > /root/e.sql

执行恢复

source /root/e.sql

日志删除

1.不能用rm

2.flush logs,服务重启也会新建一个新日志文件。

3.日志文件默认1G

4.自动清除

生产保留2个全备周期+1

select @@expire_log_days;

5.手工删除,

##删除1到5,保留6以后
PURGE BINARY LOGS TO 'binlog.00006'

##保留到这个点
PURGE BINARY LOGS BEFORE '202102-03 10:10:10'

##文件全部清空,让他从文件编号从1开始,危险,主从必宕
reset master;

binlogGTID模式管理

GTID(Global Transaction ID)

一个已提交事务的编号,并且是全局唯一 编号。

GTID=servier_uuid:transaction_id

xxxxx:30

开启GTID

my.cnf

gtid_mode=on
enforce_gtid_consistency=on

GTID截取日志

--include-gtids
--exclude-gtids
--skip-gtids
mysqlbinlog --include-gtids='xxx:1-6' --exclude-gtids='xx:4' --skip-gtids  /data/mysql/binlog.0006

截取恢复

mysqlbinlog -d tt1  --include-gtids='933ceeac-6519-11eb-80db-00155da88b26:10-17' --exclude-gtids='933ceeac-6519-11eb-80db-00155da88b26:14' --skip-gtids /data/mysql/binlog.000004 /data/mysql/binlog.000003 > /root/k.sql

//关闭此回话日志,不在记录恢复执行的操作,恢复完再打开。
set @@sql_log_bin=0;
source /root/k.sql;
set @@sql_log_bin=1;

Slowlog慢日志

记录Mysql运行过程中较慢的语句,文本文件记录。

配置

默认慢日志没有开启。

查询是否开启

select @@slow_query_log;

查询日志位置。

select @@slow_query_log_file;

查询慢的阈值。

select @@long_query_time;

不走索引的

select @@log_queries_not_using_indexes;

my.cnf

slow_query_log=1
slow_query_log_file=/data/mysql/slow.log
long_query_time=0.1
log_queries_not_using_indexes=1

读取最耗时的语句

mysqldumpslow -s c -t 10  slow.log
posted @ 2021-02-05 16:04  JinweiChang  阅读(118)  评论(0编辑  收藏  举报