MySQL二进制日志(binlog)
Posted 数据库BOSS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL二进制日志(binlog)相关的知识,希望对你有一定的参考价值。
1.二进制日志定义
2.作用
3.参数
4.开启日志
5.阅读二进制文件
1.二进制日志定义
定义
:记录数据库发生更改的所有操作,以二进制形式保存在磁盘中。
2.作用
-
恢复(recovery):数据的恢复需要用到二进制日志 -
复制(replication):通过复制和执行二进制日志使得一台远程的 mysql 数据库 与一台MySQL数据库进行实时同步。 -
审计(audit) :用户可以通过二进制日志中的信息来进行审计,判断是否有对数据库进行注入攻击或者操作
3.参数
mysql> show variables like '%log_bin%';
+---------------------------------+-------+
| 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 |
| sql_log_bin | ON |
+---------------------------------+-------+
show variables like '%sql_log%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | ON |
| sql_log_off | OFF |
+---------------+-------+
mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
-
log_bin:是否启用二进制日志 -
log_bin_basename:二进制日志的文件的基名 -
log_bin_index :二进制日志文件的索引文件 -
log_bin_trust_function_creators:是否允许创建可能导致不安全的函数 -
log_bin_use_v1_row_events:该参数代表binlog的版本信息,从5.6默认使用version2,所以该参数默认OFF -
sql_log_bin:是否启用二进制日志(会话级别) -
sql_log_off:是否将一般的查询操作记录到二进制日志中 -
binlog_format:基于那种方式进行记录row|mixex|statement
4.开启日志
--建目录
mkdir -p /jssdb/mysql/log/3306/binlog
--赋予权限
chown -R mysql:mysql /jssdb/mysql/log/3306/binlog
chmod -R 755 /jssdb/mysql/log/3306/binlog
--修改my.cnf
log_bin =/jssdb/mysql/log/3306/binlog/binlog
log_bin_index =/jssdb/mysql/log/3306/binlog/binlog.index
--重启MySQL,查看
mysql> show variables like '%log_bin%';
+---------------------------------+-------------------------------------------+
| Variable_name | Value |
+---------------------------------+-------------------------------------------+
| log_bin | ON |
| log_bin_basename | /jssdb/mysql/log/3306/binlog/binlog |
| log_bin_index | /jssdb/mysql/log/3306/binlog/binlog.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------------------------------------------+
[root@jssdb01 binlog]# pwd
/jssdb/mysql/log/3306/binlog
[root@jssdb01 binlog]# ls
binlog.000001 binlog.index
[root@jssdb01 binlog]# cat binlog.index
/jssdb/mysql/log/3306/binlog/binlog.000001
5.阅读二进制文件
--刚启动数据库的时候二进制内容
[root@jssdb01 binlog]# mysqlbinlog -vv binlog.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210128 12:48:48 server id 3306 end_log_pos 123 CRC32 0xe2aeedb0 Start: binlog v 4, server v 5.7.32-log created 210128 12:48:48 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
MEISYA/qDAAAdwAAAHsAAAABAAQANS43LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAwQhJgEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AbDtruI=
'/*!*/;
# at 123
#210128 12:48:48 server id 3306 end_log_pos 154 CRC32 0xe90759bb Previous-GTIDs
# [empty]
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
进行表的增删改查操作
mysql> create database jss_sip character set utf8mb4 collate utf8mb4_general_ci;
mysql> use jss_sip;
mysql> create table jss_t1(id int(10) not null,name varchar(20));
mysql> insert into jss_t1 values(1,'tom');
mysql> commit;
mysql> select * from jss_t1;
+----+------+
| id | name |
+----+------+
| 1 | tom |
+----+------+
1 row in set (0.00 sec)
再查看二进制日志:
[root@jssdb01 binlog]# mysqlbinlog -vv binlog.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210128 12:48:48 server id 3306 end_log_pos 123 CRC32 0xe2aeedb0 Start: binlog v 4, server v 5.7.32-log created 210128 12:48:48 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
MEISYA/qDAAAdwAAAHsAAAABAAQANS43LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAwQhJgEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AbDtruI=
'/*!*/;
# at 123
#210128 12:48:48 server id 3306 end_log_pos 154 CRC32 0xe90759bb Previous-GTIDs
# [empty]
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@jssdb01 binlog]# mysqlbinlog -vv binlog.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210128 12:48:48 server id 3306 end_log_pos 123 CRC32 0xe2aeedb0 Start: binlog v 4, server v 5.7.32-log created 210128 12:48:48 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
MEISYA/qDAAAdwAAAHsAAAABAAQANS43LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAwQhJgEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AbDtruI=
'/*!*/;
# at 123
#210128 12:48:48 server id 3306 end_log_pos 154 CRC32 0xe90759bb Previous-GTIDs
# [empty]
# at 154
#210128 13:00:05 server id 3306 end_log_pos 219 CRC32 0x3e1ebee5 Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#210128 13:00:05 server id 3306 end_log_pos 371 CRC32 0x4d6fa2cb Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1611810005/*!*/;
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=1436549120/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database jss_sip character set utf8mb4 collate utf8mb4_general_ci
/*!*/;
# at 371
#210128 13:01:12 server id 3306 end_log_pos 436 CRC32 0xa4c3b589 Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 436
#210128 13:01:12 server id 3306 end_log_pos 573 CRC32 0xe96bcf57 Query thread_id=3 exec_time=0 error_code=0
use `jss_sip`/*!*/;
SET TIMESTAMP=1611810072/*!*/;
create table jss_t1(id int(10) not null,name varchar(20))
/*!*/;
# at 573
#210128 13:01:30 server id 3306 end_log_pos 638 CRC32 0x50d2e745 Anonymous_GTID last_committed=2 sequence_number=3 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 638
#210128 13:01:24 server id 3306 end_log_pos 713 CRC32 0x8cd3831e Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1611810084/*!*/;
BEGIN
/*!*/;
# at 713
#210128 13:01:24 server id 3306 end_log_pos 768 CRC32 0x0d2d92da Table_map: `jss_sip`.`jss_t1` mapped to number 108
# at 768
#210128 13:01:24 server id 3306 end_log_pos 812 CRC32 0xe42c5cae Write_rows: table id 108 flags: STMT_END_F
BINLOG '
JEUSYBPqDAAANwAAAAADAAAAAGwAAAAAAAEAB2pzc19zaXAABmpzc190MQACAw8CUAAC2pItDQ==
JEUSYB7qDAAALAAAACwDAAAAAGwAAAAAAAEAAgAC//wBAAAAA3Rvba5cLOQ=
'/*!*/;
### INSERT INTO `jss_sip`.`jss_t1`
### SET
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2='tom' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
# at 812
#210128 13:01:30 server id 3306 end_log_pos 843 CRC32 0x597e4cb9 Xid = 18
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@jssdb01 binlog]#
对数据库做修改的操作都记录在了日志当中。
以上是关于MySQL二进制日志(binlog)的主要内容,如果未能解决你的问题,请参考以下文章