使用mysqldump備份時,如果存儲引擎為MyISAM,則只能實現溫備份,並需使用選項--lock-all-tables鎖定所有表。如果存儲引擎為InnoDB,則加上--single-transaction選項,可以實現熱備。
使用mysqldump進行邏輯備份,還存在如下問題:
SELECT * INTO OUTFILE '/path/to/somefile.txt' FROM tb_name [WHERE clause];
LOAD DATA INFILE '/path/to/somefile.txt' INTO TABLE tb_name;
mysql> SELECT * INTO OUTFILE '/tmp/tutors.txt' FROM tutors;
Query OK, 8 rows affected (0.31 sec)[root@localhost ~]# cat /tmp/tutors.txt
2 HuangYaoshi M 63
3 Miejueshitai F 72
4 OuYangfeng M 76
6 YuCanghai M 56
7 Jinlunfawang M 67
8 HuYidao M 42
9 NingZhongze F 49
14 HuFei M 31
注意:導出的僅僅是數據,表結構並不會被導出。
mysql> CREATE TABLE test_tb LIKE tutors;
Query OK, 0 rows affected (1.28 sec)
mysql> LOAD DATA INFILE '/tmp/tutors.txt' INTO TABLE test_tb;
Query OK, 8 rows affected (0.17 sec)
Records: 8 Deleted: 0 Skipped: 0 Warnings: 0
結合LVM快照功能對數據庫進行備份,能夠實現幾乎熱備(snapshot)的功能。
mysql> FLUSH TABLES WITH READ LOCK;
mysql> FLUSH LOGS;
$ mysql -uroot -p -e 'SHOW MASTER STATUS\G' > /path/to/master-`date +%F`.info
# lvcreate -L # -s -p r -n LV_NAME /path/to/source_lv
實例:
查看mysql數據目錄的vg信息:
[root@localhost ~]# df
文件系統 1K-塊 已用 可用 已用% 掛載點
/dev/mapper/VolGroup-lv_root
19134332 4151016 14011336 23% /
tmpfs 247204 0 247204 0% /dev/shm
/dev/sda1 495844 53354 416890 12% /boot
/dev/mapper/VolGroup-data
3096336 192984 2746068 7% /data[root@localhost ~]# vgdisplay VolGroup
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 24.50 GiB
PE Size 4.00 MiB
Total PE 6273
Alloc PE / Size 5762 / 22.51 GiB
Free PE / Size 511 / 2.00 GiB
VG UUID jbfFM4-hChV-QUzt-yPwV-d15b-tyGj-NeAPed
還有剩余PE,可以直接創建lv,如果沒有剩余PE,則需要先擴展VG。
創建快照卷:
[root@localhost ~]# lvcreate -L 1024M -s -p r -n data_snap /dev/mapper/VolGroup-data
Logical volume "data_snap" created
mysql> UNLOCK TABLES;
掛載快照卷:
[root@localhost ~]# mkdir /mnt/data_snap
[root@localhost ~]# mount /dev/mapper/VolGroup-data_snap /mnt/data_snap
mount: block device /dev/mapper/VolGroup-data_snap is write-protected, mounting read-only
[root@localhost ~]# cd /mnt/data_snap
[root@localhost data_snap]# ls
backup lost+found mysql