当前位置:网站首页>深入解析 MySQL binlog
深入解析 MySQL binlog
2022-06-26 12:32:00 【@Autowire】
1.概述
binlog是Mysql sever层维护的一种二进制日志,与innodb引擎中的redo/undo log是完全不同的日志;其主要是用来记录所有数据库表结构变更、以及数据修改的二进制文件,不会记录SELECT SHOW等操作,Binlog以"事务"的形式保存在磁盘中,还包含语句执行的消耗时间;主要应用于两种场景:
主从复制
数据恢复
Binlog的文件名默认为“主机名_binlog-序列号”格式,也可以在配置文件中指定名称。文件记录模式一种有三种:
ROW:日志中会记录每一行数据被修改的情况,然后再slave端对相同的数据进行修改。完全实现数据同步、数据恢复。但是会产生大量的日志(可靠但是消耗资源)。如:大批量的数据更新、尤其是alter table 会让日志暴涨。
STATMENT:每一条被修改数据的sql都会被记录到master的binlog中,slave在复制的时候sql进程会解析成和原来master端执行过的相同sql再次执行。简称sql复制。日志量少、减少了IO、提升了存储和恢复速度,在某些情况下,会导致主从数据的不一致。如:now() last_insert_id()。
MIXED:以上两种模式的混用,一般使用STATMENT模式保存binlog,对于STATMENT无法复制的操作使用ROW模式保存binlog,MYSQL会根据执行的sql语句写入模式。
2.Binlog文件结构
Mysql的binlog文件中记录的是对数据库的各种修改操作,用来表示修改操作的数据结构是Log event。不同的修改操作对应不同的log event。比较常用的log event有:Query event 、Row event、 Xid event等。 binlog文件的内容就是各种log event的集合。
3.binlog的写入机制
根据记录模式和操作触发event事件生成log event (事件触发执行机制),将事务执行过程中产生log event 写入缓冲区,每个事务线程都有一个缓冲区。log event 保存在一个bin_cache_mngr的数据结构中,在该结构中有两个缓冲区,一个是stmt_cache, 用于存放不支持事务的信息;另一个是trx_cache,用于存放支持事务的信息。事务提交阶段将会产生的log event 写入到外部binlog文件中,不同事务以串行的方式将log event 写入到binlog文件中,所以一个事务包含的log event 信息保存在binlog文件中是连续的,中间不会插入其他事务的log event。
4 命令操作-恢复数据
在/etc 目录下新建my.cnf 并添加如下内容:
* /Users/zhaoshuai11 cd /etc
* /etc cat my.cnf
[mysqld]
# log_bin
log-bin = mysql-bin #开启binlog
binlog-format = ROW #选择row模式
server_id = 1 #配置mysql replication需要定义,不能和canal的slaveId重复
show variables like '%log_bin%';
获取binlog文件列表:
show binary logs;
生成新的日志文件的条件:每当我们停止或重启服务器时,服务器会把日志文件记入下一个日志文件,MySQL会在重启时生成一个新的日志文件,文件序号递增。如果日志文件超过max_binlog_size(默认值1G)系统变量配置的上限时,也会生成新的日志文件(在这里需要注意的是,如果你正使用大的事务,二进制日志还会超过max_binlog_size,不会生成新的日志文件,事务全写入一个二进制日志中,这种情况主要是为了保证事务的完整性)日志被刷新时,新生成一个日志文件
flush logs;
执行下列的语句:
create database if not exists test_binlog;
use test_binlog;
DROP TABLE IF EXISTS `Websites`;
CREATE TABLE `Websites` (
`id` int(11) NOT NULL,
`name` char(20) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `access_log`;
CREATE TABLE `access_log` (
`aid` int(11) NOT NULL,
`site_id` int(11) NOT NULL,
`count` int(11) NOT NULL
);
INSERT INTO `Websites` VALUES ('1', 'Google');
INSERT INTO `Websites` VALUES('2', 'TaoBao');
INSERT INTO `Websites` VALUES('3', 'CaiNiao');
INSERT INTO `access_log` VALUES ('1', '1', '45');
INSERT INTO `access_log` VALUES('2', '3', '100');
INSERT INTO `access_log` VALUES('3', '1', '230');
INSERT INTO `access_log` VALUES('4', '2', '10');
show binary logs
可以看到输出如下,说明我们刚才执行的操作已经写入了mysql-bin.000007中
我们可以使用如下的命令来查看当前正在写入的binlog文件:
show master status\G
使用mysqlbinlog进行查看还是以mysql-bin.000007日志为例,基本使用用法如下:
mysqlbinlog "/usr/local/mysql/data/mysql-bin.000007"
mysqlbinlog mysql-bin.000007
mysqlbinlog mysql-bin.000007 > 'test.sql'
使用mysqlbinlog恢复数据:
mysqlbinlog --start-datetime="2022-06-22 11:38:59" --stop-datetime="2022-06-22 11:40:01" 'mysql-bin.000007' | mysql -uroot -proot
边栏推荐
- Introduction to the four major FPGA manufacturers abroad
- "Pinduoduo and short video speed version", how can I roast!
- fastjson的JSONArray和JSONObject[通俗易懂]
- Question B of 2016 Sichuan Ti Cup Electronic Design Competition
- 大智慧哪个开户更安全,更好点
- Seven major trends deeply affecting the U.S. consumer goods industry in 2022
- Nodejs framework express and KOA
- Oracle lock table query and unlocking method
- 栈,后入先出
- Function collapse and expansion shortcut keys in vscode (latest and correct)
猜你喜欢
[graduation season · advanced technology Er] I remember the year after graduation
JMeter response time and TPS listener tutorial
Examples of how laravel uses with preload (eager to load) and nested query
Xiaolong 888 was released, Xiaomi 11 was launched, and 14 manufacturers carried it in the first batch!
The laravel dingo API returns a custom error message
Spark-day02-core programming-rdd
Spark-day01- get started quickly
Ad - update the modified PCB package to the current PCB
Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source
Vscode solves the problem of Chinese garbled code
随机推荐
Thinkphp5 query report: sqlstate[hy093]: invalid parameter number
Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
Implementing mixins scheme in applet
The most complete kubernetes core command of the latest version so far
China's smart toy market outlook and investment strategy consulting forecast report from 2022 to 2027
【毕业季·进击的技术er】忆毕业一年有感
Why is password salt called "salt"? [Close] - why is a password salt called a "salt"? [closed]
[solved] data duplication or data loss after laravel paginate() paging
Laravel uses find_ IN_ The set() native MySQL statement accurately queries whether a special string exists in the specified string to solve the problem that like cannot be accurately matched. (resolve
CG bone animation
7-2 大盗阿福
2022 edition of Beijing 5g industry investment planning and development prospect forecast analysis report
Scala-day01- companion objects and HelloWorld
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
Demand scale forecast and investment competitiveness analysis report of China's new material market 2022-2028
Five strategies and suggestions of member marketing in consumer goods industry
菜鸟实战UML——活动图
I'd like to know what preferential activities are available for stock account opening? Is it safe to open an account online?
2022 China smart bathroom cabinet Market Research and investment Competitiveness Analysis Report
Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files