一.打开主配置文件 添加以下参数
[root@db1 ~]# vim /etc/my.cnf
server_id=6 # 服务id
log_bin=/data/binlog/mysql-bin #二进制日志存放路径和二进制日志的前缀 /data/binlog目录要提前创建好,并更改所属者和组为mysql mysql-bin会自动生成
binlog_format=row #设置日志文件记录DML语句的格式
二.重启mysql
[root@db1 ~]# systemctl restart mysql
三.登录mysql
[root@db1 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
四.创建一个库并插入些内容
mysql> create database test charset utf8mb4;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table t1(id int,name varchar(11));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 values(1,'小明');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values(2,'小陈');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+--------+
| id | name |
+------+--------+
| 1 | 小明 |
| 2 | 小陈 |
+------+--------+
2 rows in set (0.00 sec)
五.删除数据库
mysql> drop database test;
Query OK, 1 row affected (0.01 sec)
六.查看二进制日志
mysql> show binary logs; #查看所有已存在的二进制文件
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 201 |
| mysql-bin.000002 | 201 |
| mysql-bin.000003 | 201 |
| mysql-bin.000004 | 1992 |
+------------------+-----------+
4 rows in set (0.00 sec)
mysql> show master status; #查看正在使用的二进制文件
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 1992 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show binlog events in 'mysql-bin.000004'; #查看二进制文件记录的事件
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------+
| mysql-bin.000004 | 4 | Format_desc | 6 | 123 | Server ver: 5.7.29-log, Binlog ver: 4 |
| mysql-bin.000004 | 123 | Previous_gtids | 6 | 154 | |
| mysql-bin.000004 | 154 | Anonymous_Gtid | 6 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 219 | Query | 6 | 336 | create database binlog charset=utf8mb4 |
| mysql-bin.000004 | 336 | Anonymous_Gtid | 6 | 401 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 401 | Query | 6 | 519 | use `binlog`; create table t1 (id int,name varchar(4)) |
| mysql-bin.000004 | 519 | Anonymous_Gtid | 6 | 584 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 584 | Query | 6 | 658 | BEGIN |
| mysql-bin.000004 | 658 | Table_map | 6 | 708 | table_id: 108 (binlog.t1) |
| mysql-bin.000004 | 708 | Write_rows | 6 | 751 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000004 | 751 | Table_map | 6 | 801 | table_id: 108 (binlog.t1) |
| mysql-bin.000004 | 801 | Write_rows | 6 | 844 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000004 | 844 | Xid | 6 | 875 | COMMIT /* xid=26 */ |
| mysql-bin.000004 | 875 | Anonymous_Gtid | 6 | 940 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 940 | Query | 6 | 1059 | create database qinagge charset utf8mb4 |
| mysql-bin.000004 | 1059 | Anonymous_Gtid | 6 | 1124 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1124 | Query | 6 | 1215 | drop database qinagge |
| mysql-bin.000004 | 1215 | Anonymous_Gtid | 6 | 1280 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1280 | Query | 6 | 1399 | create database qiangge charset=utf8mb4 |
| mysql-bin.000004 | 1399 | Anonymous_Gtid | 6 | 1464 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1464 | Query | 6 | 1567 | use `qiangge`; create table t1(id int) |
| mysql-bin.000004 | 1567 | Anonymous_Gtid | 6 | 1632 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1632 | Query | 6 | 1707 | BEGIN |
| mysql-bin.000004 | 1707 | Table_map | 6 | 1755 | table_id: 110 (qiangge.t1) |
| mysql-bin.000004 | 1755 | Write_rows | 6 | 1795 | table_id: 110 flags: STMT_END_F |
| mysql-bin.000004 | 1795 | Xid | 6 | 1826 | COMMIT /* xid=69 */ |
| mysql-bin.000004 | 1826 | Anonymous_Gtid | 6 | 1891 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1891 | Query | 6 | 1992 | drop database qiangge |
| mysql-bin.000004 | 1992 | Anonymous_Gtid | 6 | 2057 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 2057 | Query | 6 | 2167 | create database test charset utf8mb4 |
| mysql-bin.000004 | 2167 | Anonymous_Gtid | 6 | 2232 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 2232 | Query | 6 | 2346 | use `test`; create table t1(id int,name varchar(11)) |
| mysql-bin.000004 | 2346 | Anonymous_Gtid | 6 | 2411 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 2411 | Query | 6 | 2483 | BEGIN |
| mysql-bin.000004 | 2483 | Table_map | 6 | 2531 | table_id: 112 (test.t1) |
| mysql-bin.000004 | 2531 | Write_rows | 6 | 2578 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000004 | 2578 | Table_map | 6 | 2626 | table_id: 112 (test.t1) |
| mysql-bin.000004 | 2626 | Write_rows | 6 | 2670 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000004 | 2670 | Table_map | 6 | 2718 | table_id: 112 (test.t1) |
| mysql-bin.000004 | 2718 | Update_rows | 6 | 2775 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000004 | 2775 | Xid | 6 | 2806 | COMMIT /* xid=143 */ |
| mysql-bin.000004 | 2806 | Anonymous_Gtid | 6 | 2871 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 2871 | Query | 6 | 2963 | drop database test |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------+
43 rows in set (0.00 sec)
找到创建 test库的起点和删除表的起点
七.把二进制日志输出为sql文件
# start-position输入出入起点 stop-postion输入删除表的起点 /data/binlog/就是一开始设置的存放二进制文件的目录 再跟上当前使用的二进制文件mysql-bin.000004
[root@db1 ~]# mysqlbinlog --start-position=2057 --stop-position=2871 /data/binlog/mysql-bin.000004 > /tmp/test.sql
八.进入数据库开始恢复数据
[root@db1 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set sql_log_bin=0; # 临时关闭恢复时产生的新日志
mysql> source /tmp/test.sql
mysql> set sql_log_bin=1; 改回来
九.查看数据库和数据
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
10 rows in set (0.01 sec)
mysql> use test;
Database changed
mysql> select * from t1;
+------+--------+
| id | name |
+------+--------+
| 1 | 小明 |
| 2 | 小陈 |
+------+--------+
2 rows in set (0.00 sec)
表和数据还在 恢复成功!!!
使用gdit的记录模式来恢复数据库
一.打开配置文件添加以下两个参数
[root@db1 ~]# vim /etc/my.cnf
gtid-mode=on ##开启gtid模式
enforce-gtid-consistency=true ##强制gtid一致性
[root@db1 ~]# systemctl restart mysql--- ###重启服务
二.登录数据库
[root@db1 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
三.创建一个数据库并插入一些数据
mysql> create database test2 charset utf8mb4; ###创建一个数据库
Query OK, 1 row affected (0.01 sec)
mysql> use test2;
Database changed
mysql> create table t1(id int, name varchar(11));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 values(1,'小明');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values(2,'小陈');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+--------+
| id | name |
+------+--------+
| 1 | 小明 |
| 2 | 小陈 |
+------+--------+
2 rows in set (0.00 sec)
四.删除数据库
mysql> drop database test2;Query OK, 1 row affected (0.02 sec)
五.查看二进制日志记录
mysql> show master status; ##查看当前使用的是哪个二进制日志文件
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000005 | 1035 | | | eee7e0b6-5466-11ea-9f97-005056bf8ab0:1-4 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql> show binlog events in 'mysql-bin.000005'; ##查看二进制文件发生的事情
六.把二进制日志信息导出为sql文件
[root@db1 ~]# mysqlbinlog --skip-gtids --include-gtids='eee7e0b6-5466-11ea-9f97-005056bf8ab0:1-3' /data/binlog/mysql-bin.000005 >/tmp/gtid.sql
######--skip-gtids 作用:在导出时,忽略原有的gtid信息,恢复时生成最新的gtid信息######
七.登录数据库 开始恢复数据
[root@db1 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set sql_log_bin=0; 临时关闭恢复时产生的新日志
mysql> source /tmp/gtid.sql;
mysql> set sql_log_bin=1 再开启
mysql> show databases; ### 查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test2 |
+--------------------+
11 rows in set (0.00 sec)
mysql> select * from t1;
+------+--------+
| id | name |
+------+--------+
| 1 | 小明 |
| 2 | 小陈 |
+------+--------+
2 rows in set (0.00 sec)
数据恢复成功