實驗之MySQL日志備份與恢復


       日志是mysql數據庫的重要組成部分。日志文件中記錄着mysql數據庫運行期間發生的變化;也就是說用來記錄mysql數據庫的客戶端連接狀況、SQL語句的執行情況和錯誤信息等。當數據庫遭到意外的損壞時,可以通過日志查看文件出錯的原因,並且可以通過日志文件進行數據恢復。下面是對MYSQL日志和數據恢復的簡單介紹。

一、MySQL日志

主要包含:錯誤日志、查詢日志、慢查詢日志、事務日志、二進制日志

二 、MySQL備份工具

mysqldump:邏輯備份工具 ,適用於所有引擎,可用於溫備,能實現完全備份,部分備份

cp,tar等文件系統工具:物理備份工具,適用於所有存儲引擎,用於冷備,能實現完全備份、部分備份

lvm2的快照:幾乎熱備;借助文件系統工具實現物理備份

mysqlhotcopy:幾乎冷備;僅用於MyISAM存儲引擎

三、由於二進制日志格外重要,所以這里介紹基於二進制的mysql備份方法

方法1、mysqldump + binlog

完全備份,通過備份二進制日志實現增量備份

命令的語法格式

mysqldump [OPTIONS] database [tables]:備份單個庫,或庫指定的一個或多個表

mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2DB3...]:備份一個或多個庫

mysqldump [OPTIONS] --all-databases [OPTIONS]:備份所有庫

實驗步驟如下:

①准備備份目錄

[root@centos7 ~]# mkdir /mysqlback  創建備份目錄
[root@centos7 ~]# chown -R mysql.mysql /mysqlback/ 修改權限

②准備備份數據庫及表

[root@centos7 ~]# mysql -uroot -p   登錄數據庫
MariaDB [(none)]> create database magedu;   創建數據庫
MariaDB [(none)]>use magedu;
MariaDB [magedu]> create table m26(id int not null.name char (20));  創建數據表

③進行完整備份

[root@centos7 /mysqlback]# mysqldump --database magedu --flush-log > /mysqlback/mysql-all-back-`date +%F-%T`.sql  完整數據庫備份
[root@centos7 /mysqlback]# mysqldump --database magedu --flush-log > /mysqlback/mysql-magedu-back-`date +%F-%T`.sql  單個數據庫備份,備份magedu數據庫 
[root@centos7 /mysqlback]# ls
mysql-all-back-2017-11-16-14:47:03.sql  mysql-magedu-back-2017-11-16-14:48:44.sql

④向表中插入數據

MariaDB [(none)]> use magedu;
Database changed
MariaDB [magedu]> insert into m26 values(004,'xiaohong');
Query OK, 1 row affected (0.01 sec)
MariaDB [magedu]> insert into m26 values(005,'xiaolan'); 
Query OK, 1 row affected (0.00 sec)
MariaDB [magedu]> select * from m26;
+----+----------+
| id | name |
+----+----------+
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | xiaohong |
| 5 | xiaolan |
+----+----------+

⑤進行增量備份,備份二進制日志

[root@centos7 /var/lib/mysql]# mysqlbinlog bin-log.000007  查看position(BEGIN418 COMMIT644)
[root@centos7 /var/lib/mysql]# mysqlbinlog --start-position=418 --stop-position=644 /var/lib/mysql/bin-log.000007 > /mysqlback/bin-log-`date +%F_%T`.sql  根據上面查到的position位置進行增量備份

⑥繼續向數據庫中插入數據,沒備份直接刪除數據庫

MariaDB [(none)]> use magedu;
Database changed
MariaDB [magedu]> insert into m26 values(008,'liuyifei');
Query OK, 1 row affected (0.03 sec)
MariaDB [magedu]> drop database magedu;

⑦數據恢復,首先保護好最后的二進制日志,查看刪除數據庫之前的position值

BEGIN
/*!*/;
# at 714
#171116 15:07:56 server id 1 end_log_pos 817 Query thread_id=11 exec_time=0 error_code=0
SET TIMESTAMP=1510816076/*!*/;
insert into m26 values(008,'liuyifei')
/*!*/;
# at 817
#171116 15:07:56 server id 1 end_log_pos 844 Xid = 439
COMMIT/*!*/;
# at 844
#171116 15:08:16 server id 1 end_log_pos 929 Query thread_id=11 exec_time=0 error_code=0
SET TIMESTAMP=1510816096/*!*/;
drop database magedu

⑧將最后操作的二進制日志備份

[root@centos7 /var/lib/mysql]# mysqlbinlog --stop-position=817 /var/lib/mysql/bin-log.000007 > /mysqlback/bin-log-`date +%F-%T`.sql

⑨導入之前的所有備份

[root@centos7 /var/lib/mysql]# cd /mysqlback/
[root@centos7 /mysqlback]# ls
bin-log-2017-11-16_15:02:49.sql mysql-all-back-2017-11-16-14:47:03.sql
bin-log-2017-11-16-15:16:41.sql mysql-magedu-back-2017-11-16-14:48:44.sql
bin-log-.sql
[root@centos7 /mysqlback]# mysql -uroot -p < mysql-all-back-2017-11-16-14:47:03.sql
[root@centos7 /mysqlback]# mysql -uroot -p < mysql-magedu-back-2017-11-16-14:48:44.sql
[root@centos7 /mysqlback]# mysql -uroot -p < bin-log-2017-11-16_15\:02\:49.sql 
[root@centos7 /mysqlback]# mysql -uroot -p < bin-log-2017-11-16-15:16:41.sql

⑩查看數據庫及數據(恢復成功)

[root@centos7 /mysqlback]# mysql -uroot -p
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| magedu |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use magedu;
Database changed
MariaDB [magedu]> select * from m26;
+----+----------+
| id | name |
+----+----------+
| 1 | one |
| 2 | two |
| 3 | three |
| 5 | xiaolan |
| 4 | xiaohong |
| 6 | liuyifei |
+----+----------+
6 rows in set (0.00 sec)

方法2、lbm2快照+binlog

LVM快照簡單來說就是將所快照源分區一個時間點所有文件的元數據(記錄數據屬性的數據)進行保存,如果源文件沒有改變,那么訪問快照卷的相應文件則直接指向源分區的源文件,如果源文件發生改變,則快照卷中與之對應的文件不會發生改變。快照卷主要用於輔助備份文件。

實驗步驟如下:

①添加一塊硬盤

②並划分磁盤類型為lvm類型

[root@centos7 ~]# echo '- - -' >/sys/class/scsi_host/host0/s  使主機識別硬盤
[root@centos7 ~]# fdisk -l  查看是否添加成功
Disk /dev/sde: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
分區
[root@centos7 ~]# fdisk /dev/sde  對磁盤進行分區
Welcome to fdisk (util-linux 2.23.2).

③對此磁盤的操作如下

[root@centos7 ~]# partprobe  重讀分區表
[root@centos7 ~]# fdisk -l   查看
Device Boot Start End Blocks Id System
/dev/sde1 2048 209715199 104856576 8e Linux LVM 
[root@centos7 ~]# pvcreate /dev/sde1  初始化物理卷
Physical volume "/dev/sde1" successfully created.
[root@centos7 ~]# vgcreate myvg /dev/sde1
Volume group "myvg" successfully created
[root@centos7 ~]# lvcreate -n mydata -L 50G myvg  創建LVM卷組
Logical volume "mydata" created.
[root@centos7 ~]# mkfs.ext4 /dev/mapper/myvg-mydata 格式化分區
mke2fs 1.42.9 (28-Dec-2013
Allocating group tables: done 
Writing inode tables: done 
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 
[root@centos7 ~]# mkdir /bak   創建一個掛載目錄
[root@centos7 ~]# mount /dev/mapper/myvg-mydata /bak   掛載
[root@centos7 ~]# cd /bak
[root@centos7 /bak]# ls
lost+found

④對數據庫的操作如下

[root@centos7 /bak]# systemctl stop mariadb
[root@centos7 /bak]# cd /var/lo
local/ lock/ log/ 
[root@centos7 /bak]# cd /var/lib/mysql/
[root@centos7 /var/lib/mysql]# mv * /bak
[root@centos7 /var/lib/mysql]# cd /bak
[root@centos7 /bak]# ls
aria_log.00000001 bin-log.000004 ibdata1 magedu.sql mysql-bin.000004
aria_log_control bin-log.000005 ib_logfile0 mysql mysql-bin.000005
bin-log.000001 bin-log.000006 ib_logfile1 mysql-bin.000001 mysql-bin.index
bin-log.000002 bin-log.000007 lost+found mysql-bin.000002 performance_schema
bin-log.000003 bin-log.index magedu mysql-bin.000003 test
[root@centos7 /bak]# chown mysql.mysql . -R

⑤整理

[root@centos7 /bak]# vim /etc/my.cnf
datadir=/bak  使得數據文件在邏輯卷上
[root@centos7 /bak]# systemctl start mariadb
[root@centos7 /bak]# mysql -uroot -p
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;  鎖定表
Query OK, 0 rows affected (0.00 sec)
[root@centos7 /bak]# lvcreate -L 1G -n mydata-snap -p r -s /dev/mapper/myvg-mydata  創建快照卷
Using default stripesize 64.00 KiB.
Logical volume "mydata-snap" created.
[root@centos7 /bak]# mysql -uroot -p
MariaDB [(none)]> UNLOCK TABLES;  解鎖所有表
Query OK, 0 rows affected (0.00 sec)
[root@centos7 /bak]# mkdir /snap
[root@centos7 /bak]# mount /dev/mapper/myvg-mydata /snap  掛載快照卷
[root@centos7 /bak]# cd /snap/
[root@centos7 /snap]# ls (這里是存儲的元數據,真實的數據存儲在/bak下)
aria_log.00000001 bin-log.000004 bin-log.index magedu mysql-bin.000003 test
aria_log_control bin-log.000005 ibdata1 magedu.sql mysql-bin.000004
bin-log.000001 bin-log.000006 ib_logfile0 mysql mysql-bin.000005
bin-log.000002 bin-log.000007 ib_logfile1 mysql-bin.000001 mysql-bin.index
bin-log.000003 bin-log.000008 lost+found mysql-bin.000002 performance_schema

⑥打包備份並刪庫

[root@centos7 /snap]# tar -cjvf mysql-backup.tar.gz ./*  打包物理備份
[root@centos7 /snap]# cd /bak
[root@centos7 /bak]# mv mysql-backup.tar.gz /root
[root@centos7 /bak]# rm -rf * (刪除bak下的數據,snap里也沒有了)
[root@centos7 /bak]# ls
[root@centos7 /bak]# cd /snap/
[root@centos7 /snap]# ls
[root@centos7 /snap]# cd /bak
[root@centos7 /bak]# mv /root/mysql-backup.tar.gz ./
[root@centos7 /bak]# ls
mysql-backup.tar.gz
[root@centos7 /bak]# tar -xvf mysql-backup.tar.gz
[root@centos7 /bak]# ls (bak和snap里的數據都恢復了)
aria_log.00000001 bin-log.000005 ib_logfile0 mysql-backup.tar.gz mysql-bin.index
aria_log_control bin-log.000006 ib_logfile1 mysql-bin.000001 performance_schema
bin-log.000001 bin-log.000007 lost+found mysql-bin.000002 test
bin-log.000002 bin-log.000008 magedu mysql-bin.000003
bin-log.000003 bin-log.index magedu.sql mysql-bin.000004
bin-log.000004 ibdata1 mysql mysql-bin.000005
[root@centos7 /bak]# cd /snap/
[root@centos7 /snap]# ls
aria_log.00000001 bin-log.000005 ib_logfile0 mysql-backup.tar.gz mysql-bin.index
aria_log_control bin-log.000006 ib_logfile1 mysql-bin.000001 performance_schema
bin-log.000001 bin-log.000007 lost+found mysql-bin.000002 test
bin-log.000002 bin-log.000008 magedu mysql-bin.000003
bin-log.000003 bin-log.index magedu.sql mysql-bin.000004
bin-log.000004 ibdata1 mysql mysql-bin.000005

⑦測試

[root@centos7 /snap]# systemctl start mariadb
[root@centos7 /snap]# mysql -uroot -p (數據庫都回來了)
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use magedu;
Database changed
MariaDB [magedu]> select * from m26;
+----+----------+
| id | name |
+----+----------+
| 1 | one |
| 2 | two |
| 3 | three |
| 5 | xiaolan |
| 4 | xiaohong |
| 5 | xiaolan |
| 9 | aodaili |
+----+----------+
7 rows in set (0.07 sec)

 總結:數據無價,學好備份與恢復很重要!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM