在實際的工作中,我們可能經(jīng)常會遇到數(shù)據(jù)庫宕機(jī),數(shù)據(jù)丟失的情況,下面,我將演示一個模擬環(huán)境
1.數(shù)據(jù)庫正常啟動插入數(shù)據(jù):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@client103 ~]# mysql -uroot -pkongzhong mysql> use test; mysql> insert into a select * from a; #注: 這里不演示建表,默認(rèn)大家都懂 # 下面進(jìn)行一次全備 [root@client103 ~]# innobackupex --user=root --password=kongzhong --defaults-file=/etc/my.cnf --port=3306 /tmp/backup/ >/tmp/backup/innoback.log 2>&1 # 再次插入數(shù)據(jù) mysql> insert into a select * from a; # 實行增量備份 [root@client103 ~]# innobackupex --user=root --password=kongzhong --defaults-file=/etc/my.cnf --port=3306 --incremental --incremental-basedir=/tmp/backup/2014-02-27_13-24-51/ /tmp/backup/ # 再次插入數(shù)據(jù) mysql> insert into a select * from a; # 此時大家記錄一下現(xiàn)在的總行數(shù)(一會還原需要核對數(shù)據(jù)的) # 此時數(shù)據(jù)庫宕機(jī) #模擬宕機(jī)操作為: # 1.將數(shù)據(jù)目錄的下的二進(jìn)制日志,復(fù)制到 /tmp/backup,一會需要重演日志(此步非常重要,復(fù)制操作默認(rèn)大家會了) # 2.刪除數(shù)據(jù)目錄下的所有文件,此時數(shù)據(jù)庫就崩潰了(如果關(guān)閉數(shù)據(jù)庫,是關(guān)不了,記得殺掉進(jìn)程,kill -9 ....) |
2.數(shù)據(jù)庫宕機(jī)后執(zhí)行恢復(fù)操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
在實際的工作中,我們可能經(jīng)常會遇到數(shù)據(jù)庫宕機(jī),數(shù)據(jù)丟失的情況,下面,我將演示一個模擬環(huán)境 1.數(shù)據(jù)庫正常啟動插入數(shù)據(jù): [root@client103 ~]# mysql -uroot -pkongzhong mysql> use test; mysql> insert into a select * from a; #注: 這里不演示建表,默認(rèn)大家都懂 # 下面進(jìn)行一次全備 [root@client103 ~]# innobackupex --user=root --password=kongzhong --defaults-file=/etc/my.cnf --port=3306 /tmp/backup/ >/tmp/backup/innoback.log 2>&1 # 再次插入數(shù)據(jù) mysql> insert into a select * from a; # 實行增量備份 [root@client103 ~]# innobackupex --user=root --password=kongzhong --defaults-file=/etc/my.cnf --port=3306 --incremental --incremental-basedir=/tmp/backup/2014-02-27_13-24-51/ /tmp/backup/ # 再次插入數(shù)據(jù) mysql> insert into a select * from a; # 此時大家記錄一下現(xiàn)在的總行數(shù)(一會還原需要核對數(shù)據(jù)的) # 此時數(shù)據(jù)庫宕機(jī) #模擬宕機(jī)操作為: # 1.將數(shù)據(jù)目錄的下的二進(jìn)制日志,復(fù)制到 /tmp/backup,一會需要重演日志(此步非常重要,復(fù)制操作默認(rèn)大家會了) # 2.刪除數(shù)據(jù)目錄下的所有文件,此時數(shù)據(jù)庫就崩潰了(如果關(guān)閉數(shù)據(jù)庫,是關(guān)不了,記得殺掉進(jìn)程,kill -9 ....) 2.數(shù)據(jù)庫宕機(jī)后執(zhí)行恢復(fù)操作 # 全備應(yīng)用日志 [root@client103 ~]# innobackupex --apply-log /tmp/backup/2014-02-27_13-24-51/ # 增備應(yīng)用日志 [root@client103 ~]# innobackupex --apply-log /tmp/backup/2014-02-27_13-24-51/ --incremental-dir=/tmp/backup/2014-02-27_13-32-44/ # 應(yīng)用完日志,利用全備恢復(fù)數(shù)據(jù) [root@client103 ~]# innobackupex --copy-back /tmp/backup/2014-02-27_13-24-51/ # 修改數(shù)據(jù)目錄權(quán)限為mysql:mysql [root@client103 ~]# chown mysq:mysql /var/lib/mysql -R # 啟動數(shù)據(jù)庫 [root@client103 ~]# /etc/init.d/mysql start # 登陸數(shù)據(jù)庫,查看數(shù)據(jù)行數(shù),發(fā)現(xiàn)數(shù)值與崩潰前是不一致的,所以需要利用日志恢復(fù) [root@client103 ~]# mysql -uroot -pkongzhong mysql> use test; mysql> select count (*) from a; # 查看最后一次增備完成時的日志文件名和pos號(備份時自動記錄的) [root@client103 ~]# cat /tmp/backup/2014-02-27_13-32-44/xtrabackup_binlog_info mysql-103-bin.000005 3694 # 所以,我們使用二進(jìn)制執(zhí)行恢復(fù)時,起始點為3694 # 利用日志恢復(fù) [root@client103 ~]# mysqlbinlog --start-pos=3694 /tmp/backup/mysql-103-bin.000005 |mysql -uroot -pkongzhong # 此時在登陸數(shù)據(jù)庫查看數(shù)據(jù)是否相符,請自行測試 mysql> use test; mysql> select count (*) from a; |
以上就是本文的全部內(nèi)容,希望大家可以喜歡。