新建一個(gè)lvm磁盤,這里我建的lv為mydatalv,掛載到了/data下
[root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mydatalv mydata -wi-ao---- 1.00g [root@localhost ~]# df -h 文件系統(tǒng) 容量 已用 可用 已用% 掛載點(diǎn) /dev/mapper/mydata-mydatalv 976M 2.6M 907M 1% /data
將原數(shù)據(jù)庫文件復(fù)制到/data目錄下
[root@localhost ~]# cp -a /var/lib/mysql /data/
修改配置文件,將mysql數(shù)據(jù)庫文件放在lvm盤中,二進(jìn)制文件放在非lvm盤的/var/lib/mysql/目錄下
[root@ns1 ~]# vim /etc/my.cnf [mysqld] log_bin=/var/lib/mysql/mysql-bin datadir=/data/mysql [root@localhost ~]# service mariadb restart [root@localhost ~]# ls /data/mysql aria_log.00000001 ibdata1 ib_logfile1 mysql-bin.000001 mysql-bin.000003 performance_schema aria_log_control ib_logfile0 mysql mysql-bin.000002 mysql-bin.index test
可以看到重啟后數(shù)據(jù)庫文件已存放在了/data/mysql目錄中了
對mysql進(jìn)行鎖表備份
[root@localhost ~]# mysql -e 'flush tables with read lock;' 鎖表 [root@localhost ~]# mysql -e 'flush logs;' 對日志進(jìn)行滾動(dòng), [root@localhost ~]# mysql -e 'show master status;' > /root/back.$(date +%F+%T) [root@localhost ~]# ls back.2016-07-13+10:14:29
對lv創(chuàng)建快照
[root@localhost ~]# lvcreate -L 1G -n mysqlback -p r -s /dev/mydata/mydatalv
釋放鎖
[root@localhost ~]# mysql -e 'unlock tables;'
在別的磁盤上創(chuàng)建備份目錄,只讀掛載快照后備份至備份目錄
[root@localhost ~]# mkdir /myback [root@localhost ~]# mount -r /dev/mydata/mysqlback /mnt [root@localhost ~]# cp -a /mnt/mysql /myback
修改表內(nèi)容,然后刪除掉數(shù)據(jù)庫文件內(nèi)容即/data/mysql中的內(nèi)容
[root@localhost ~]# mysql MariaDB [hellodb]> use hellodb; MariaDB [hellodb]> insert into classes (class,numofstu) values ('xxoo',39); [root@localhost ~]# rm -rf /data/*
修改配置文件中二進(jìn)制日志和數(shù)據(jù)庫文件的位置
[root@localhost ~]# vim /etc/my.cnf [mysqld] log_bin=/data/mysql/mysql-bin datadir=/data/mysql
利用/myback/中的內(nèi)容還原
[root@localhost ~]# cp -a /myback/* /data/ [root@localhost ~]# service mariadb restart
利用二進(jìn)制日志還原快照后的操作,由下面這個(gè)文件來查看快照執(zhí)行時(shí)二進(jìn)制日志的位置
[root@localhost ~]# cat back.2016-07-13+10\:14\:29 File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000014 245
將000014中245之后的操作做成sql文件,進(jìn)行還原
[root@localhost ~]# mysqlbinlog --start-position=245 /var/lib/mysql/mysql-bin.000014 > binlog.sql [root@localhost ~]# mysql < /root/binlog.sql
查看恢復(fù)情況
[root@localhost ~]# mysql MariaDB [(none)]> use hellodb; MariaDB [hellodb]> select * from classes; +---------+----------------+----------+ | ClassID | Class | NumOfStu | +---------+----------------+----------+ | 1 | Shaolin Pai | 10 | | 2 | Emei Pai | 7 | | 3 | QingCheng Pai | 11 | | 4 | Wudang Pai | 12 | | 5 | Riyue Shenjiao | 31 | | 6 | Lianshan Pai | 27 | | 7 | Ming Jiao | 27 | | 8 | Xiaoyao Pai | 15 | | 9 | xxoo | 39 | +---------+----------------+----------+ 9 rows in set (0.00 sec)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com