共計(jì) 6936 個(gè)字符,預(yù)計(jì)需要花費(fèi) 18 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 mysql 中 innobackupex 備份和 binlog 日志完全恢復(fù)的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
前言:
MySQL 的完全恢復(fù),我們可以借助于完整的 備份 +binlog 來(lái)將數(shù)據(jù)庫(kù)恢復(fù)到故障點(diǎn)。
備份可以是熱備與邏輯備份(mysqldump),只要備份與 binlog 是完整的,都可以實(shí)現(xiàn)完全恢復(fù)。
1. 準(zhǔn)備實(shí)驗(yàn)環(huán)境
mysql select version();
+————+
| version() |
+————+
| 5.6.25-log |
+————+
1 row in set (0.00 sec)
mysql create database com_rec;
Query OK, 1 row affected (0.00 sec)
mysql use inc_rec;
Database changed
mysql create table andy (id int);
Query OK, 0 rows affected (0.08 sec)
mysql insert into andy values(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
2. 全備
[root@mysql02 full]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=oracle –port=3606 /xtrabackup/full/
xtrabackup: Transaction log of lsn (1662519) to (1662519) was copied.
170609 17:34:34 completed OK!
3. 查看全備生成文件
[root@mysql02 full]# ll /xtrabackup/full/
total 4
drwxr-x—. 6 root root 4096 Jun 9 17:34 2017-06-09_17-34-30
4. 模擬業(yè)務(wù)新數(shù)據(jù)
mysql insert into andy values(3),(4);
Query OK, 2 rows affected (0.14 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql commit;
Query OK, 0 rows affected (0.00 sec)
5. 增量備份
— 創(chuàng)建存放增量備份目錄并賦權(quán)
[root@mysql02 full]# mkdir -p /xtrabackup/incr/
[root@mysql02 full]# chown -R mysql:mysql /xtrabackup/incr/
[root@mysql02 full]# ll /xtrabackup/
total 8
drwxr-xr-x. 3 mysql mysql 4096 Jun 9 03:53 full
drwxr-xr-x. 2 mysql mysql 4096 Jun 9 04:00 incre
— 正式開(kāi)始增量備份
[root@mysql02 full]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=oracle –incremental \
–incremental-basedir=/xtrabackup/full/2017-06-09_17-34-30/ /xtrabackup/incr/
######################################## 下面是增量備份輸出
。。。省略
xtrabackup: Transaction log of lsn (1665808) to (1665808) was copied.
170609 17:36:46 completed OK!
6. 再模擬新業(yè)務(wù),該記錄在保存在 binlog,而不會(huì)存在于任何備份,這條記錄用于驗(yàn)證完全恢復(fù)
mysql insert into andy values(5);
Query OK, 1 row affected (0.00 sec)
7. 記下操作后的 position 點(diǎn)
mysql show master status;
+—————+———-+————–+——————+——————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+—————+———-+————–+——————+——————-+
| binlog.000005 | 1104 | | | |
+—————+———-+————–+——————+——————-+
1 row in set (0.00 sec)
8. 切換 binlog 日志
mysql flush logs;
Query OK, 0 rows affected (0.00 sec)
9. 使用 binlog events 命令來(lái)查看我們最后 insert 的一條記錄
mysql show binlog events in binlog.000005
+—————+——+————-+———–+————-+———————————————–+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+—————+——+————-+———–+————-+———————————————–+
| binlog.000005 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.25-log, Binlog ver: 4 |
| binlog.000005 | 120 | Query | 1 | 221 | drop database inc_rec |
| binlog.000005 | 221 | Query | 1 | 324 | create database com_rec |
| binlog.000005 | 324 | Query | 1 | 430 | use `com_rec`; create table andy (id int) |
| binlog.000005 | 430 | Query | 1 | 515 | BEGIN |
| binlog.000005 | 515 | Query | 1 | 625 | use `com_rec`; insert into andy values(1),(2) |
| binlog.000005 | 625 | Xid | 1 | 656 | COMMIT /* xid=67 */ |
| binlog.000005 | 656 | Query | 1 | 741 | BEGIN |
| binlog.000005 | 741 | Query | 1 | 851 | use `com_rec`; insert into andy values(3),(4) |
| binlog.000005 | 851 | Xid | 1 | 882 | COMMIT /* xid=81 */ |
| binlog.000005 | 882 | Query | 1 | 967 | BEGIN |
| binlog.000005 | 967 | Query | 1 | 1073 | use `com_rec`; insert into andy values(5) |
| binlog.000005 | 1073 | Xid | 1 | 1104 | COMMIT /* xid=96 */ |
| binlog.000005 | 1104 | Rotate | 1 | 1148 | binlog.000006;pos=4 |
+—————+——+————-+———–+————-+———————————————–+
14 rows in set (0.00 sec)
10. 查看 binlog 的位置 與 datadir 的位置,防止 mv datadir 時(shí)誤操作 binlog,影響恢復(fù)(binlog 與 datadir 一定要分開(kāi)放置)
mysql show variables like %log_bin%
+———————————+————————————+
| Variable_name | Value |
+———————————+————————————+
| log_bin | ON |
| log_bin_basename | /data/mysql/binarylog/binlog |
| sql_log_bin | ON |
+———————————+————————————+
6 rows in set (0.00 sec)
mysql show variables like %datadir%
+—————+————–+
| Variable_name | Value |
+—————+————–+
| datadir | /data/mysql/ |
+—————+————–+
1 row in set (0.00 sec)
11. 恢復(fù)
11.1 先做基于全備的 apply,注意,此時(shí)使用了 –redo-only
[root@mysql02 full]# innobackupex –defaults-file=/etc/my.cnf –user=root –apply-log –redo-only /xtrabackup/full/2017-06-09_17-34-30/
170609 04:19:30 completed OK!
11.2 在恢復(fù)增量備份集:– 此時(shí)沒(méi)有 –redo-only,如果有多個(gè)增備,僅僅最后一個(gè)增備無(wú)需指定 –redo-only
[root@mysql02 full]# innobackupex –defaults-file=/etc/my.cnf –user=root –apply-log /xtrabackup/full/2017-06-09_17-34-30/ –incremental-dir=/xtrabackup/incr/2017-06-09_17-36-39/
170609 04:24:45 completed OK! #結(jié)果出現(xiàn) completed OK 表示完全成功
說(shuō)明:/xtrabackup/full/2017-06-09_17-34-30/ 為全備基目錄,incremental-dir 為增量備份目錄
12. 關(guān)閉要恢復(fù)的實(shí)例
[root@mysql02 data]# /etc/init.d/mysql stop -p3306
netstat -nltp|grep mysql|grep 3606
13. 將原有文件夾重命名到新位置,并創(chuàng)建原文件夾
[root@mysql02 full]# mv /data/mysql /data/mysqlbak
[root@mysql02 full]# mkdir -p /data/mysql
14. 執(zhí)行拷貝恢復(fù)的文件到原來(lái)的數(shù)據(jù)位置
[root@mysql02 full]# innobackupex –defaults-file=/etc/my.cnf –user=root –copy-back /xtrabackup/full/2017-06-09_17-34-30/
170609 04:33:06 completed OK! #結(jié)果出現(xiàn) completed OK 表示完全成功
說(shuō)明:/xtrabackup/full/2017-06-09_17-34-30/ 為全備基目錄
15. 權(quán)限修改
[root@mysql02 ~]# mkdir -p /data/mysql/binarylog(說(shuō)明:這里我 binlog 在 datadir 在路徑下,所以要單獨(dú)為 binlog 創(chuàng)建目錄)
— 將 binlog 日志 mv 回原位置(如果 binlog 不在 datadir 下,就不用操作)
[root@mysql02 xtrabackup]# ll /data/mysqlbak/binarylog/
total 28
-rw-rw—-. 1 mysql mysql 164 Jun 9 06:12 binlog.000001
-rw-rw—-. 1 mysql mysql 386 Jun 9 06:14 binlog.000002
-rw-rw—-. 1 mysql mysql 143 Jun 9 06:53 binlog.000003
-rw-rw—-. 1 mysql mysql 143 Jun 9 07:35 binlog.000004
-rw-rw—-. 1 mysql mysql 1148 Jun 9 17:45 binlog.000005
-rw-rw—-. 1 mysql mysql 143 Jun 9 17:48 binlog.000006
-rw-rw—-. 1 mysql mysql 216 Jun 9 17:45 binlog.index
[root@mysql02 data]# mv /data/mysqlbak/binarylog/* /data/mysql/binarylog/
[root@mysql02 xtrabackup]# ll /data/mysql/binarylog/
total 28
-rw-rw—-. 1 mysql mysql 164 Jun 9 06:12 binlog.000001
-rw-rw—-. 1 mysql mysql 386 Jun 9 06:14 binlog.000002
-rw-rw—-. 1 mysql mysql 143 Jun 9 06:53 binlog.000003
-rw-rw—-. 1 mysql mysql 143 Jun 9 07:35 binlog.000004
-rw-rw—-. 1 mysql mysql 1148 Jun 9 17:45 binlog.000005
-rw-rw—-. 1 mysql mysql 143 Jun 9 17:48 binlog.000006
-rw-rw—-. 1 mysql mysql 216 Jun 9 17:45 binlog.index
[root@mysql02 data]# chown -R mysql:mysql /data/mysql
16. 啟動(dòng)恢復(fù)后的實(shí)例
mysqld_safe –defaults-file=/etc/my.cnf
17. 登錄檢查
[root@mysql02 ~]# mysql -uroot -poracle
mysql use com_rec
mysql select * from andy;
+——+
| id |
+——+
| 1 |
| 2 |
| 3 |
| 4 | 恢復(fù)成功,但是沒(méi)有恢復(fù)到最新,缺少 id=5 , Inbinlog 記錄并沒(méi)有被恢復(fù)。
+——+
18. 使用 binlog 做完全恢復(fù)
[root@mysql02 ~]# cd /xtrabackup/incr/2017-06-09_17-36-39/
– 從 innobackupex 獲得 binlog 的位置
[root@mysql02 2017-06-09_17-36-39]# more xtrabackup_binlog_info
binlog.000005 882
– 使用 mysqlbinlog 追加的最新
[root@mysql02 2017-06-09_17-36-39]# mysqlbinlog –start-position=882 –stop-position=1104 /data/mysql/binarylog/binlog.000005 | mysql -uroot -poracle
補(bǔ)充:
這樣做確實(shí)可以,而且 row 模式的 binlog,也可以通過(guò)這種方式來(lái)執(zhí)行。但是這樣做有幾個(gè)缺點(diǎn)
a. 如果解析出來(lái)的 binlog 在執(zhí)行的過(guò)程中報(bào)錯(cuò),如何處理?直接加 -f 強(qiáng)制執(zhí)行嗎?
b. 執(zhí)行中途如何停下來(lái),下次接著跑?比如我想調(diào)整一下 MySQL 的參數(shù)(需要重啟)后繼續(xù)跑?
c. 只能單線程執(zhí)行。而且 mysqlbinlog 解析再通過(guò)管道執(zhí)行,有比較高的性能開(kāi)銷(xiāo)。
19. 驗(yàn)證,可以看到最后一條記錄以及被恢復(fù)
mysql select * from andy;
+——+
| id |
+——+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+——+
5 rows in set (0.00 sec)
以上是“mysql 中 innobackupex 備份和 binlog 日志完全恢復(fù)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道!