一.打開主配置文件 添加以下參數
[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)
數據恢復成功