您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)MySQL中怎么實(shí)現(xiàn)實(shí)時(shí)在線備份恢復(fù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
1.主庫(kù)準(zhǔn)備測(cè)試數(shù)據(jù)
mysql> create database cnfol; Query OK, 1 row affected (0.00 sec) mysql> create table cnfol.t (id int primary key); Query OK, 0 rows affected (0.02 sec) mysql> insert into cnfol.t select 1; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into cnfol.t select 2; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0
到備庫(kù)確認(rèn):
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | cnfol | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> select * from cnfol.t; +----+ | id | +----+ | 1 | | 2 | +----+ 2 rows in set (0.00 sec)
2.加個(gè)全局讀鎖 在備庫(kù)
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec)
3.為備庫(kù)所在分區(qū)創(chuàng)建快照
[root@localhost ~]# lvcreate --size 1G --snapshot --name backup_mysql /dev/vg/mysql Logical volume "backup_mysql" created [root@localhost ~]# lvs LV VG Attr LSize Origin snap% Move Log Copy% Convert backup_mysql vg swi-a- 1.00G mysql 0.00 mysql vg owi-ao 2.00G
4.獲取二進(jìn)制日志坐標(biāo)
在備庫(kù):
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 727 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
解鎖 在備庫(kù):
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec)
5.掛載快照
[root@localhost ~]# mount /dev/vg/backup_mysql /mnt/backup [root@localhost ~]# cd /mnt/backup/mysql/data/cnfol/ && ls -alh 總計(jì) 32K drwx------ 2 mysql dba 4.0K 10-14 09:57 . drwx------ 5 mysql dba 4.0K 10-14 09:57 .. -rw-rw---- 1 mysql dba 61 10-14 09:57 db.opt -rw-rw---- 1 mysql dba 8.4K 10-14 09:57 t.frm -rw-rw---- 1 mysql dba 14 10-14 09:57 t.MYD -rw-rw---- 1 mysql dba 2.0K 10-14 10:06 t.MYI
6.主庫(kù)某無(wú)經(jīng)驗(yàn)DBA誤操作
mysql> drop database cnfol; Query OK, 1 row affected (0.05 sec)
記錄下此時(shí)時(shí)間:2013-10-14 10:17:10
備庫(kù)確認(rèn)是否存在庫(kù)cnfol:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.01 sec)
7.備份快照
[root@localhost backup]# pwd /mnt/backup [root@localhost backup]# tar -jcv -f /mnt/snapshot/mysql.tar.bz2 *
這里做備份的原因有2點(diǎn) * 其一,昂貴的IO,因?yàn)榇蓬^要在快照區(qū)和系統(tǒng)區(qū)來(lái)回跑 * 其二,快照區(qū)空間不足,因?yàn)槭荂OW原理
8.刪除快照
[root@localhost ~]# umount /mnt/backup [root@localhost ~]# lvremove --force /dev/vg/backup_mysql Logical volume "backup_mysql" successfully removed
9.格式化備庫(kù)所在分區(qū)
[mysql@localhost ~]$ mysqladmin -uroot -poracle shutdown 131014 10:32:40 mysqld_safe mysqld from pid file /mnt/lvm/mysql/data/localhost.localdomain.pid ended [1]+ Done mysqld_safe [root@localhost ~]# umount /mnt/lvm [root@localhost ~]# mkfs -t ext3 /dev/vg/mysql [root@localhost ~]# mount /dev/vg/mysql /mnt/lvm [root@localhost ~]# lvs LV VG Attr LSize Origin snap% Move Log Copy% Convert mysql vg -wi-ao 2.00G [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg 4 1 0 wz--n- 3.81G 1.81G
10.解壓縮快照到備庫(kù)所在分區(qū)
# tar -jxv -f /mnt/snapshot/mysql.tar.bz2 -C /mnt/lvm/ [root@localhost lvm]# pwd /mnt/lvm [root@localhost lvm]# ls lost+found mysql
11.啟動(dòng)MySQL
12.利用binlog執(zhí)行point-in-time恢復(fù)
[mysql@localhost ~]$ mysqlbinlog --stop-datetime="2013-10-14 10:17:10" /mnt/lvm/mysql/data/mysql-bin.000003 | mysql -uroot -poracle
13.確認(rèn)數(shù)據(jù)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | cnfol | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> select * from cnfol.t; +----+ | id | +----+ | 1 | | 2 | +----+ 2 rows in set (0.00 sec)
看完上述內(nèi)容,你們對(duì)MySQL中怎么實(shí)現(xiàn)實(shí)時(shí)在線備份恢復(fù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。