mysql通過二進制日志恢復數據庫

一.打開主配置文件 添加以下參數

[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庫的起點和刪除表的起點?

分別是2057和2871

七.把二進制日志輸出為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'; ##查看二進制文件發生的事情


發現gdit的1到3是建庫和查數據的gdit值

六.把二進制日志信息導出為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)

數據恢復成功

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,415評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,647評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,130評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,366評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,887評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,737評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,939評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,174評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,608評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,914評論 2 372