当前位置:网站首页>如何配置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)
边栏推荐
- [super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
- wpf 找不到资源文件问题
- 【数据库】期末必知必会-----第二章 关系数据模型
- Wechat e-book reading applet graduation design of applet completion works (3) background function
- AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
- Wkwebview white screen
- 企业邮局如何设置SPF记录?
- 【数据库】期末必知必会-----第九章 数据库设计
- V4l2 learning materials collection
- ffmpeg中AVFrame\AVPacket与自己的数据交互
猜你喜欢

PAC Decade: witness HPC from CPU era to XPU Era

MAUI 框架入門學習05 MVVM數據模型理解

小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能

Skillfully use enterprise network disk to collect reports or summaries

机器学习09:无监督学习

ospf防环
![[database] knowledge and skills at the end of the term ----- Chapter 9 database design](/img/8d/90ccb1eac114706d27fccc1250b58c.png)
[database] knowledge and skills at the end of the term ----- Chapter 9 database design

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(3)后台功能

图形验证码验证

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(2)小程序功能
随机推荐
[database] must know and be able at the end of the term ----- Chapter VIII database security
Some problems after xcode11 new project
Wechat Online Education video on Demand Learning of applet Graduation Design (3) Background Function
小程序毕设作品之微信电子书阅读小程序毕业设计(7)中期检查报告
笔记本电脑插入耳机仍然外放(亲测有效)
Avplayer adds playback progress monitoring
【微信小程序】超易懂的条件渲染和列表渲染
Smart fan system based on STM32F103
Chapter 0 performance platform godeye source code analysis - Course Introduction
【数据库】期末必知必会-----第一章 数据库概述
Ftxui basic notes (botton button component Foundation)
Wechat e-book reading of small program graduation design (5) task book
小程序毕设作品之微信电子书阅读小程序毕业设计(4)开题报告
C# 构造函数(Constructors)简单讲解
Introduction to Maui framework 05 MVVM data model understanding
Machine learning 10: Integrated Learning
About the problem of database, the concept of uniqueness and non repetition
windows10:vscode下go语言的适配
小程序畢設作品之微信在線教育視頻點播學習小程序畢業設計(3)後臺功能
Wechat e-book reading applet graduation project (6) opening defense ppt