当前位置:网站首页>如何配置Binlog
如何配置Binlog
2022-07-17 04:01:00 【Lord Chaser】
binlog日志有两个最重要的使用场景
1)MySQL主从复制:MySQL Replication在Master端开启binlog,Master把它的二进制日志传递给slaves来达到
master-slave数据一致的目的。
2)自然就是数据恢复了,通过使用mysqlbinlog工具来使恢复数据。
binlog日志包括两类文件
1)二进制日志索引文件(文件名后缀为.index)用于记录所有的二进制文件
2)二进制日志文件(文件名后缀为.00000*)记录数据库所有的DDL和DML(除了数据查询语句select)语句事件。
二、开启binlog日志功能
1)编辑打开mysql配置文件/etc/mys.cnf
[[email protected] ~]# vim /etc/my.cnf
在[mysqld] 区块添加
log-bin=mysql-bin 确认是打开状态(mysql-bin 是日志的基本名或前缀名)
**注意:**每次服务器(数据库)重启,服务器会调用flush logs;,新创建一个binlog日志!
2)重启mysqld服务使配置生效
[[email protected] ~]# /etc/init.d/mysqld stop
[[email protected] ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
3)查看binlog日志是否开启
mysql> show variables like ‘log_%’;
±--------------------------------±--------------------+
| Variable_name | Value |
±--------------------------------±--------------------+
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| log_bin_trust_routine_creators | OFF |
| log_error | /var/log/mysqld.log |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_slow_queries | OFF |
| log_warnings | 1 |
±--------------------------------±--------------------+
9 rows in set (0.00 sec)
三、常用的binlog日志操作命令
1)查看所有binlog日志列表
mysql> show master logs;
±-----------------±----------+
| Log_name | File_size |
±-----------------±----------+
| mysql-bin.000001 | 149 |
| mysql-bin.000002 | 4102 |
±-----------------±----------+
2 rows in set (0.00 sec)
2)查看master状态,即最后(最新)一个binlog日志的编号名称,及其最后一个操作事件pos结束点(Position)值
mysql> show master status;
±-----------------±---------±-------------±-----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±-----------------±---------±-------------±-----------------+
| mysql-bin.000002 | 4102 | | |
±-----------------±---------±-------------±-----------------+
1 row in set (0.00 sec)
3)flush刷新log日志,自此刻开始产生一个新编号的binlog日志文件
mysql> flush logs;
Query OK, 0 rows affected (0.13 sec)
mysql> show master logs;
±-----------------±----------+
| Log_name | File_size |
±-----------------±----------+
| mysql-bin.000001 | 149 |
| mysql-bin.000002 | 4145 |
| mysql-bin.000003 | 106 |
±-----------------±----------+
3 rows in set (0.00 sec)
注意:每当mysqld服务重启时,会自动执行此命令,刷新binlog日志;在mysqldump备份数据时加 -F 选项也会刷新binlog日志;
4)重置(清空)所有binlog日志
mysql> reset master;
Query OK, 0 rows affected (0.12 sec)
mysql> show master logs;
±-----------------±----------+
| Log_name | File_size |
±-----------------±----------+
| mysql-bin.000001 | 106 |
±-----------------±----------+
1 row in set (0.00 sec)
四、利用binlog日志恢复mysql数据
以下对ops库的member表进行操作
mysql> use ops;
mysql> CREATE TABLE IF NOT EXISTS member
(
-> id
int(10) unsigned NOT NULL AUTO_INCREMENT,
-> name
varchar(16) NOT NULL,
-> sex
enum(‘m’,‘w’) NOT NULL DEFAULT ‘m’,
-> age
tinyint(3) unsigned NOT NULL,
-> PRIMARY KEY (id
)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.10 sec)
mysql> show tables;
±--------------+
| Tables_in_ops |
±--------------+
| member |
±--------------+
1 row in set (0.00 sec)
mysql> desc member;
±--------±--------------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±--------±--------------------±-----±----±--------±---------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(16) | NO | | NULL | |
| sex | enum(‘m’,‘w’) | NO | | m | |
| age | tinyint(3) unsigned | NO | | NULL | |
| classid | char(6) | YES | | NULL | |
±--------±--------------------±-----±----±--------±---------------+
5 rows in set (0.00 sec)
事先插入两条数据
mysql> insert into member(name
,sex
,age
,classid
) values(‘wangshibo’,‘m’,27,‘cls1’),(‘guohuihui’,‘w’,27,‘cls2’);
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from member;
±—±----------±----±----±--------+
| id | name | sex | age | classid |
±—±----------±----±----±--------+
| 1 | wangshibo | m | 27 | cls1 |
| 2 | guohuihui | w | 27 | cls2 |
±—±----------±----±----±--------+
2 rows in set (0.00 sec)
边栏推荐
- Wechat e-book reading of small program graduation design (5) task book
- Leetcode7 DFS + dynamic programming + double pointer
- Set administrator permissions for idea and console
- Machine learning 09: unsupervised learning
- leetcode977. 有序数组的平方
- 若依框架包名修改器
- The adaptation of go language under windows10:vscode
- [ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)
- 【数据库】期末必知必会-----第七章 数据库完整性
- 小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(3)后台功能
猜你喜欢
Mqant in-depth analysis
Intel experts share: how to program efficiently on XPU architecture? Zhiqiang Research Institute
Heartless sword Chinese English bilingual poem 005 Lyric
模拟服务器进行请求
Set administrator permissions for idea and console
Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?
基于STM32的SG90舵机实验含代码(HAL库)
小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书
Machine learning 10: Integrated Learning
Some problems after xcode11 new project
随机推荐
Cabasicanimation pause \ enterprise action
Wechat online education video on demand learning applet graduation design (3) background function
leetcode977. 有序数组的平方
Simple explanation of C constructors
WPF cannot find resource file problem
leetcode7-dfs+动态规划+双指针
Wechat e-book reading of applet completion works (7) Interim inspection report
小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(1)开发概要
ospf综合实验
基于STM32的SG90舵机实验含代码(HAL库)
图形验证码验证
Chapter 3 performance platform godeye source code analysis - memory module
巧用企业网盘收取报告或总结
Simulation Implementation of library function
Wechat Online Education video on Demand Learning of applet Graduation Design (3) Background Function
Smart fan system based on STM32F103
【数据库】期末必知必会-----第一章 数据库概述
Intel experts share: how to program efficiently on XPU architecture? Zhiqiang Research Institute
Unity - how to modify a package or localize it
Academic sharing | design and development of multi staining pathological image information evaluation system based on openvino