标签:使用 路径 cut 存储 过期 pos 更新 root start
二进制日志主要记录mysql数据库的变化,二进制日志包含所有更新了数据或者潜在更新了数据(如没有匹配到任何行的delete语句),语句以时间的形式保存,描述了数据的更改。二进制日志还包含执行每个更新数据库语句的时间信息,使用二进制日志的主要目的是最大可能的恢复数据库。因为二进制日志包含备份后进行的所有更新,不记录没有修改任何数据的语句。
1、默认情况下二进制日志是关闭的,通过配置文件来启动和设置二进制日志。修改my.cng,插入如下内容,然后重启mysqld服务。
server-id = 1 # mysql5.7必须加,否则mysql服务启动报错 log-bin = /data/3306/tmp/binlog/mysql-bin #路径及命名,默认在data下 expire_logs_days = 10 # 过期时间,二进制文件自动删除的天数,0代表不删除 max_binlog_size = 100M # 单个日志文件大小
2、通过show variables like ‘log_bin%‘查看二进制日志设置
mysql> show variables like ‘log_bin%‘; +---------------------------------+---------------------------------------+ | Variable_name | Value | +---------------------------------+---------------------------------------+ | log_bin | ON | | log_bin_basename | /data/3306/tmp/binlog/mysql-bin | | log_bin_index | /data/3306/tmp/binlog/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | +---------------------------------+---------------------------------------+
3、查看当前服务器所有的二进制日志文件 show binary logs / show master logs
mysql> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 732 | +------------------+-----------+
4、查看当前二进制日志状态 show master status
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 732 | | | | +------------------+----------+--------------+------------------+-------------------+
每次重启MySQL服务也会生成一个新的二进制日志文件,相当于二进制日志切换。切换二进制日志时,你会看到这些number会不断递增。另外,除了这些二进制日志文件外,你会看到还生成了一个DB-Server-bin.index的文件,这个文件中存储所有二进制日志文件的清单又称为二进制文件的索引。
执行 flush logs 可以刷新切换二进制文件
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 732 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> flush logs; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
1、使用show binlog events 可以获取当前及指定日志 show binlog events ‘mysql-bin.000002‘ from 639(只产看639)
mysql> show binlog events; +------------------+-----+----------------+-----------+-------------+---------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+----------------+-----------+-------------+---------------------------------------+ | mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.24-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 | 291 | BEGIN | | mysql-bin.000001 | 291 | Table_map | 1 | 350 | table_id: 108 (test.t_count) | | mysql-bin.000001 | 350 | Write_rows | 1 | 412 | table_id: 108 flags: STMT_END_F | | mysql-bin.000001 | 412 | Xid | 1 | 443 | COMMIT /* xid=5 */ | | mysql-bin.000001 | 443 | Anonymous_Gtid | 1 | 508 | SET @@SESSION.GTID_NEXT= ‘ANONYMOUS‘ | | mysql-bin.000001 | 508 | Query | 1 | 580 | BEGIN | | mysql-bin.000001 | 580 | Table_map | 1 | 639 | table_id: 108 (test.t_count) | | mysql-bin.000001 | 639 | Write_rows | 1 | 701 | table_id: 108 flags: STMT_END_F | | mysql-bin.000001 | 701 | Xid | 1 | 732 | COMMIT /* xid=7 */ | | mysql-bin.000001 | 732 | Rotate | 1 | 779 | mysql-bin.000002;pos=4 | +------------------+-----+----------------+-----------+-------------+---------------------------------------+
2、打印二进制日志到一个明文文件,该文件记录的更新了数据的sql,但是在5.7以上已经被加密
mysqlbinlog /data/3306/tmp/binlog/mysql-bin.000001 > mysql-bin.log # 打印日志文件 mysqlbinlog --base64-output=decode-rows -v /data/3306/tmp/binlog/mysql-bin.000001 > binlog.sql # 解密文件
# 截取的部分日志内容,可以看到insert语句
BEGIN /*!*/; # at 580 #181215 11:12:03 server id 1 end_log_pos 639 CRC32 0xc66c75c3 Table_map: `test`.`t_count` mapped to number 108 # at 639 #181215 11:12:03 server id 1 end_log_pos 701 CRC32 0xf8a50341 Write_rows: table id 108 flags: STMT_END_F ### INSERT INTO `test`.`t_count` ### SET ### @1=533500356 ### @2=‘W3V‘ ### @3=‘fg8rVMuT0‘ ### @4=‘2018:08:28‘ ### @5=‘2016-08-12 15:36:09‘ # at 701 #181215 11:12:03 server id 1 end_log_pos 732 CRC32 0xc42a0c0d Xid = 7 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= ‘AUTOMATIC‘ /* added by mysqlbinlog */ /*!*/; DELIMITER ;
1、reset mstaer 可以删除所有日志文件 (不存在主从复制关系),执行完该语句,所有二进制日志被删除。mysql重新创建二进制日志,编号从000001开始
2、purge master logs语句删除指定日志文件
purge binary logs to ‘DB-Server-bin.000002‘; # 删除该日志文件之前的所有日志文件
purge binary logs before ‘2017-03-10 10:10:00‘; # 清除该时间点以前的所有日志文件
purge master logs before date_sub( now( ), interval 7 day); # 清除7天前的日志文件
如果开启了二进制日志,出现了数据丢失,可以通过二进制日志恢复数据库,语法如下
mysqlbinlog [option] filename | mysql -u user -p passwd
option的参数主要有两个 --start-datetime --stop-datetime 和 start-position --stop-position ,前者指定恢复的时间点,后者指定恢复的位置(位置指的是二进制文件中 # at 580 580就是位置)
mysqlbinlog --start-position="291" --stop-position="439" /data/3306/tmp/binlog/mysql-bin.000001 | mysql -uroot -p111111
标签:使用 路径 cut 存储 过期 pos 更新 root start
原文地址:https://www.cnblogs.com/jkin/p/10124182.html