mysql 打开binlog_Mysql开启binlog及查看

本文介绍了MySQL的Binlog操作,包括如何开启Binlog、检查其状态、查看Binlog文件内容等。此外,还详细讲解了使用mysqlbinlog工具提取和解析Binlog的方法。

mysql的binlog操作

开启binlog后,数据库的执行操作都会被记录到日志中,方便出差错时,及时回滚。

1、开启binlog

找到my.ini文件,在[mysqld]后面增加如下两行:

log-bin=mysql-bin

binlog-format=Row

重启mysql数据库

30297378cc5753825fcb1b04aa068da5.png

2、检查binlog开启状态:

mysql> show variables like 'log_%';

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

+----------------------------------------+---------------+

| Variable_name | Value |

+----------------------------------------+---------------+

| log_bin | OFF |

| log_bin_basename | |

| log_bin_index | |

| log_bin_trust_function_creators | OFF |

| log_bin_use_v1_row_events | OFF |

| log_builtin_as_identified_by_password | OFF |

| log_error | .\MONSTER.err |

| log_error_verbosity | 3 |

| log_output | FILE |

| log_queries_not_using_indexes | OFF |

| log_slave_updates | OFF |

| log_slow_admin_statements | OFF |

| log_slow_slave_statements | OFF |

| log_statements_unsafe_for_binlog | ON |

| log_syslog | ON |

| log_syslog_tag | |

| log_throttle_queries_not_using_indexes | 0 |

| log_timestamps | UTC |

| log_warnings | 2 |

+----------------------------------------+---------------+

View Code

如果log_bin显示为ON,则代表已开启。

3、查看当前正在写入的binlog文件

mysql>show master status;+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 | 1357 | | | |

+------------------+----------+--------------+------------------+-------------------+

4、查看当前所有的binlog文件

修改或删除一条表记录后,观察日志

运行以下语句可以查看当前记录的日志名称,及日志大小,你会发现每当有修改或删除操作时,日志大小都会增加。

mysql>show binary logs;+------------------+-----------+

| Log_name | File_size |

+------------------+-----------+

| mysql-bin.000001 | 1357 |

+------------------+-----------+

5、只查看第一个binlog文件的内容 和查看指定binlog文件的内容

mysql> show binlog events;

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------+

| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |

+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------+

| mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.28-log, Binlog ver: 4 |

| mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | |

| mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |

| mysql-bin.000001 | 219 | Query | 1 | 344 |use `test`; CREATE TABLE `test2` (

`1` int(1) NOT NULL DEFAULT '0')|

| mysql-bin.000001 | 344 | Anonymous_Gtid | 1 | 409 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |

| mysql-bin.000001 | 409 | Query | 1 | 481 | BEGIN |

| mysql-bin.000001 | 481 | Table_map | 1 | 529 | table_id: 109 (test.test2) |

| mysql-bin.000001 | 529 | Write_rows | 1 | 569 | table_id: 109 flags: STMT_END_F |

| mysql-bin.000001 | 569 | Xid | 1 | 600 | COMMIT /*xid=11*/ |

| mysql-bin.000001 | 600 | Anonymous_Gtid | 1 | 665 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |

| mysql-bin.000001 | 665 | Query | 1 | 825 |use `test`; CREATE TABLE `test3` (

`a`int(1) NOT NULL DEFAULT '0',

`b`int(1) NOT NULL DEFAULT '0')|

| mysql-bin.000001 | 825 | Anonymous_Gtid | 1 | 890 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |

| mysql-bin.000001 | 890 | Query | 1 | 962 | BEGIN |

| mysql-bin.000001 | 962 | Table_map | 1 | 1011 | table_id: 110 (test.test3) |

| mysql-bin.000001 | 1011 | Write_rows | 1 | 1055 | table_id: 110 flags: STMT_END_F |

| mysql-bin.000001 | 1055 | Xid | 1 | 1086 | COMMIT /*xid=14*/ |

| mysql-bin.000001 | 1086 | Anonymous_Gtid | 1 | 1151 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |

| mysql-bin.000001 | 1151 | Query | 1 | 1223 | BEGIN |

| mysql-bin.000001 | 1223 | Table_map | 1 | 1272 | table_id: 110 (test.test3) |

| mysql-bin.000001 | 1272 | Update_rows | 1 | 1326 | table_id: 110 flags: STMT_END_F |

| mysql-bin.000001 | 1326 | Xid | 1 | 1357 | COMMIT /*xid=20*/ |

+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------+

21 rows in set (0.00 sec)

View Code

mysql> show binlog events in 'mysql-bin.000001';

使用mysqlbinlog查看二进制日志

(一)mysqlbinlog工具介绍

binlog类型是二进制的,也就意味着我们没法直接打开看,MySQL提供了mysqlbinlog来查看二进制日志,该工具类似于Oracle的logminer。mysqlbinlog用法为

其中,常用的option选项如下:

option

作用

-d , --database=name

只列出指定数据库的操作

-o , --offset = n

忽略日志前n行

-r , --result-file=name

将输出的文本格式日志保存到文件

-v

-vv

-v  :从binlog中重建sql语句

-vv:显示的SQL语句增加了注释,可以理解为-v的增强

--start-datetime=datetime

--stop-datetime=datetime

指定日期间隔内的所有日志

--start-position=position

--stop-position=position

指定位置间隔内的所有日志

(二)mysqlbinlog使用例子

(1)提取指定的binlog日志

mysqlbinlog /home/mysql/data/ins3308/binlog.000001

(2)提取指定position位置的binlog日志

mysqlbinlog --start-position=120 --stop-position=332 binlog.000001

(3)提取指定position位置的binlog日志并输出到压缩文件

mysqlbinlog --start-position=120 --stop-position=332 binlog.000001 |gzip >extra_01.sql.gz

(4)提取指定position位置的binlog日志导入数据库

mysqlbinlog --start-position=120 --stop-position=332 binlog.000001| mysql -uroot -p

(5)提取指定开始时间的binlog并输出到日志文件

mysqlbinlog --start-datetime="2017-08-14 10:00:00" binlog.000001--result-file=extra02.sql

(6)提取指定位置的多个binlog日志文件

mysqlbinlog --start-position=120 --stop-position=332 binlog.000001 binlog.000002|more

(7)提取指定数据库binlog并转换字符集到UTF8

mysqlbinlog --database=test --set-charset=utf8 binlog.000001 binlog.000002 >test.sql

(8)远程提取日志,指定结束时间

mysqlbinlog -uroot -p -h192.168.10.02 -P3306 --stop-datetime="2017-08-14 10:00:00" --read-from-remote-server binlog.000001 |more

(9)远程提取使用row格式的binlog日志并输出到本地文件

mysqlbinlog -uroot -p -P3308 -h192.168.10.02 --read-from-remote-server -vv binlog.000001 >row.sql

案例:

mysqlbinlog --base64-output=decode-rows -vv .\mysql-bin.000001

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER/*!*/;

# at4#201224 13:55:03 server id 1 end_log_pos 123 CRC32 0x7277df22 Start: binlog v 4, server v 5.7.28-log created 201224 13:55:03at startup

# Warning: this binlog is eitherinuse or was not closed properly.

ROLLBACK/*!*/;

# at123#201224 13:55:03 server id 1 end_log_pos 154 CRC32 0xb076cda3 Previous-GTIDs

# [empty]

# at154#201224 14:07:05 server id 1 end_log_pos 219 CRC32 0x940c4fcf Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=no

SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;

# at219#201224 14:07:05 server id 1 end_log_pos 344 CRC32 0x52b87213 Query thread_id=3 exec_time=0 error_code=0use `test`/*!*/;

SET TIMESTAMP=1608790025/*!*/;

SET @@session.pseudo_thread_id=3/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

SET @@session.sql_mode=1344274432/*!*/;

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;/*!\C gbk*//*!*/;

SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=8/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

CREATE TABLE `test2` (

`1` int(1) NOT NULL DEFAULT '0')/*!*/;

# at344#201224 14:07:05 server id 1 end_log_pos 409 CRC32 0x6b4a8cc7 Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=yes/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;

SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;

# at409#201224 14:07:05 server id 1 end_log_pos 481 CRC32 0x222ab41e Query thread_id=3 exec_time=0 error_code=0SET TIMESTAMP=1608790025/*!*/;

BEGIN/*!*/;

# at481#201224 14:07:05 server id 1 end_log_pos 529 CRC32 0xec90b509 Table_map: `test`.`test2` mapped to number 109# at529#201224 14:07:05 server id 1 end_log_pos 569 CRC32 0xf08256ae Write_rows: table id 109flags: STMT_END_F

### INSERT INTO `test`.`test2`

### SET

### @1=1 /*INT meta=0 nullable=0 is_null=0*/# at569#201224 14:07:05 server id 1 end_log_pos 600 CRC32 0x932732d7 Xid = 11COMMIT/*!*/;

# at600#201224 14:11:42 server id 1 end_log_pos 665 CRC32 0x454a006d Anonymous_GTID last_committed=2 sequence_number=3 rbr_only=no

SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;

# at665#201224 14:11:42 server id 1 end_log_pos 825 CRC32 0xc75a0c24 Query thread_id=3 exec_time=0 error_code=0SET TIMESTAMP=1608790302/*!*/;

CREATE TABLE `test3` (

`a`int(1) NOT NULL DEFAULT '0',

`b`int(1) NOT NULL DEFAULT '0')/*!*/;

# at825#201224 14:11:42 server id 1 end_log_pos 890 CRC32 0x010aa555 Anonymous_GTID last_committed=3 sequence_number=4 rbr_only=yes/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;

SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;

# at890#201224 14:11:42 server id 1 end_log_pos 962 CRC32 0x54d9f256 Query thread_id=3 exec_time=0 error_code=0SET TIMESTAMP=1608790302/*!*/;

BEGIN/*!*/;

# at962#201224 14:11:42 server id 1 end_log_pos 1011 CRC32 0x735e8b7c Table_map: `test`.`test3` mapped to number 110# at1011#201224 14:11:42 server id 1 end_log_pos 1055 CRC32 0x60ddce64 Write_rows: table id 110flags: STMT_END_F

### INSERT INTO `test`.`test3`

### SET

### @1=1 /*INT meta=0 nullable=0 is_null=0*/### @2=1 /*INT meta=0 nullable=0 is_null=0*/# at1055#201224 14:11:42 server id 1 end_log_pos 1086 CRC32 0x64816cb5 Xid = 14COMMIT/*!*/;

# at1086#201224 14:13:33 server id 1 end_log_pos 1151 CRC32 0x81a76dba Anonymous_GTID last_committed=4 sequence_number=5 rbr_only=yes/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;

SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;

# at1151#201224 14:13:33 server id 1 end_log_pos 1223 CRC32 0xf626162b Query thread_id=3 exec_time=0 error_code=0SET TIMESTAMP=1608790413/*!*/;

BEGIN/*!*/;

# at1223#201224 14:13:33 server id 1 end_log_pos 1272 CRC32 0x62f1ec4a Table_map: `test`.`test3` mapped to number 110# at1272#201224 14:13:33 server id 1 end_log_pos 1326 CRC32 0xd84d3f41 Update_rows: table id 110flags: STMT_END_F

### UPDATE `test`.`test3`

### WHERE

### @1=1 /*INT meta=0 nullable=0 is_null=0*/### @2=1 /*INT meta=0 nullable=0 is_null=0*/### SET

### @1=1 /*INT meta=0 nullable=0 is_null=0*/### @2=2 /*INT meta=0 nullable=0 is_null=0*/# at1326#201224 14:13:33 server id 1 end_log_pos 1357 CRC32 0xb1b4d8b0 Xid = 20COMMIT/*!*/;

SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /*added by mysqlbinlog*/ /*!*/;

DELIMITER ;

# End of logfile

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值