MySQL使用LVM快照實(shí)現(xiàn)備份
新建一個(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ù)庫(kù)文件復(fù)制到/data目錄下
[root@localhost ~]# cp -a /var/lib/mysql /data/
修改配置文件,將mysql數(shù)據(jù)庫(kù)文件放在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ù)庫(kù)文件已存放在了/data/mysql目錄中了
對(duì)mysql進(jìn)行鎖表備份
[root@localhost ~]# mysql -e 'flush tables with read lock;' 鎖表 [root@localhost ~]# mysql -e 'flush logs;' 對(duì)日志進(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
對(duì)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ù)庫(kù)文件內(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ù)庫(kù)文件的位置
[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è)文件來(lái)查看快照?qǐng)?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)
相關(guān)文章
Linux下安裝mysql的方式(yum和源碼編譯兩種方式)
這里介紹Linux下兩種安裝mysql的方式:yum安裝和源碼編譯安裝。需要的朋友可以參考下2018-02-02MySQL服務(wù)器的啟動(dòng)和關(guān)閉
作為MySQL管理員,一個(gè)普通的目標(biāo)就是確保服務(wù)器盡可能地處于運(yùn)行狀態(tài),使得客戶機(jī)能夠隨時(shí)訪問(wèn)它。但是,有時(shí)最好關(guān)閉服務(wù)器(例如,如果正在進(jìn)行數(shù)據(jù)庫(kù)的重定位,不希望服務(wù)器在該數(shù)據(jù)庫(kù)中更新表)。保持服務(wù)器運(yùn)行和偶爾關(guān)閉它的需求關(guān)系不是本書所解 決的。但是我們至少可以討論如何使服務(wù)器啟動(dòng)和停止,以便您具備進(jìn)行這兩個(gè)操作的能力。2008-04-04教您修復(fù)mysql數(shù)據(jù)庫(kù)的方法
你可能在使用MySQL過(guò)程中,各種意外導(dǎo)致數(shù)據(jù)庫(kù)表的損壞,而且這些數(shù)據(jù)往往是最新的數(shù)據(jù),通常不可能在備份數(shù)據(jù)中找到。本章將繼上篇文章中檢查出表的問(wèn)題后,告訴你如何修復(fù)表2014-05-05如何解決mysql表輸入中文出現(xiàn)問(wèn)號(hào)的問(wèn)題
這篇文章主要介紹了如何解決mysql表輸入中文出現(xiàn)問(wèn)號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01