mysqldump完全備份 和 二進位制即時點還原資料庫
一 實驗目標
1 掌握mysqldump用法
2 掌握 mysqldump 完全備份,增量備份
3 掌握 mysqldump 恢復資料庫,二進位制日誌即時點還原
二 實驗環境
作業系統:CentOS release 6.8 (Final)
應用軟體:percona-server-5.6.29-76.2
三 備份策略
周完全+每日增量
完全備份: 使用mysqldump備份所有庫
增量備份: 備份二進位制日誌檔案
四 模擬過程
4.1 實驗前準備
匯入jiaowu.sql 資料
[[email protected] ~]# mysql < /root/jiaowu.sql
[[email protected]
mysql> show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.10 sec)
mysql> use jiaowu;
mysql> show tables;
+------------------+
| Tables_in_jiaowu |
+------------------+
| courses |
| scores |
| students |
| tutors |
+------------------+
4.2 用mysqldump 做完全備份
[[email protected] ~]# mysqldump -uroot -p --master-data=2 --lock-all-tables --flush-logs --all-databases > /root/alldatabases.sql
連線mysql
[[email protected] ~]# mysql
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 10389 |
| mysql-bin.000002 | 120 |
+------------------+-----------+
刪掉舊二進位制日誌
mysql> PURGE BINARY LOGS TO 'mysql-bin.000002';
Query OK, 0 rows affected (0.01 sec)
4.3 第一天增量備份
模擬資料庫修改,刪掉部分資料
[[email protected] ~]# mysql
mysql> use jiaowu;
mysql> SELECT * FROM tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
mysql> DELETE FROM tutors WHERE Age>90;
Query OK, 1 row affected (0.00 sec)
mysql> FLUSH LOGS;
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000002 | 390 |
| mysql-bin.000003 | 120 |
+------------------+-----------+
mysql> quit;
[[email protected] ~]# ls -l /data/mysql
[[email protected] ~]# cp -a /data/mysql/mysql-bin.000002 /root/
將二進位制日誌檔案轉換為sql指令碼
[[email protected] ~]# mysqlbinlog /root/mysql-bin.000002 > /root/mon-incremental.sql
4.4 第二天資料庫增量備份
[[email protected] ~]# mysql
mysql> use jiaowu;
mysql> INSERT INTO tutors (Tname) VALUES ('stu123');
mysql> FLUSH LOGS;
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000002 | 390 |
| mysql-bin.000003 | 435 |
| mysql-bin.000004 | 120 |
+------------------+-----------+
mysql> QUIT;
備份第二天二進位制日誌檔案
[[email protected] ~]# cp -a /data/mysql/mysql-bin.000003 /root/
4.5 刪掉資料庫,模擬資料損壞(已提前備份好資料和二進位制日誌檔案)
[[email protected] ~]# rm -rf /data/mysql/*
[[email protected] ~]# service mysqld stop
MySQL (Percona Server) PID file could not be found! [FAILED]
[[email protected] ~]# killall mysqld
初始化mysql
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db
--user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
[[email protected] ~]# service mysqld start
4.6 還原完全備份
[[email protected] ~]# mysql -uroot -p < /root/alldatabases.sql
4.7 恢復第一天增量備份
[[email protected] ~]# mysqlbinlog /root/mysql-bin.000002 | mysql -uroot –p
4.8 恢復第二天增量備份
[[email protected] ~]# mysqlbinlog /root/mysql-bin.000003 | mysql -uroot -p