mysql备份与恢复之真实环境使用冷备 一 真实环境使用冷备 在上一篇文章(mysql备份与恢复之冷备)中,我们提到了冷备。但是有个问题,我们存储的数据文件是保存在当前本地磁盘的,如果这个磁盘挂掉,那我们存储的数据不就丢失了,这样备份数据不就功亏一篑,
mysql备份与恢复之真实环境使用冷备
一 真实环境使用冷备
在上一篇文章(mysql备份与恢复之冷备)中,我们提到了冷备。但是有个问题,我们存储的数据文件是保存在当前本地磁盘的,如果这个磁盘挂掉,那我们存储的数据不就丢失了,这样备份数据不就功亏一篑,劳而无功。所以真实环境中我们多准备几块磁盘,然后再在这些磁盘上搭建lvm,把mysql的数据目录挂载到lvm上,这样数据就不是存储在当前磁盘上,就可以保证数据的安全性。
二 示意图
三 真实环境使用冷备模拟
第一步,需要提前规划好磁盘,这里做模拟,添加两磁盘
第二步,对磁盘进行分区
[root@serv01 ~]# fdisk /dev/sdb[root@serv01 ~]# fdisk /dev/sdc[root@serv01 ~]# ll /dev/sd[bc]1brw-rw----. 1 root disk 8, 17 sep 10 18:06 /dev/sdb1brw-rw----. 1 root disk 8, 33 sep 10 18:09 /dev/sdc1
第三步,yum安装lvm2
[root@serv01 ~]# yum install lvm2 -y
第四步,创建物理卷
[root@serv01 ~]# pvcreate /dev/sdb1 /dev/sdc1 physical volume /dev/sdb1 successfully created physical volume /dev/sdc1 successfully created
第五步,创建卷组
[root@serv01 ~]# vgcreate data /dev/sdb1 /dev/sdc1 volume group data successfully created
第六步,创建逻辑卷
[root@serv01 ~]# lvcreate -l 2g -n mydata data logical volume mydata created
第七步,格式化磁盘
[root@serv01 ~]# mkfs.ext4 /dev/data/mydata mke2fs 1.41.12 (17-may-2010)filesystem label=os type: linuxblock size=4096 (log=2)fragment size=4096 (log=2)stride=0 blocks, stripe width=0 blocks131072 inodes, 524288 blocks26214 blocks (5.00%) reserved for the super userfirst data block=0maximum filesystem blocks=53687091216 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupsuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912writing inode tables: done creating journal (16384 blocks): donewriting superblocks and filesystem accounting information: donethis filesystem will be automatically checked every 28 mounts or180 days, whichever comes first. use tune2fs -c or -i to override.
第八步,冷备
[root@serv01 ~]# ls /usr/local/mysql/data/crm ib_logfile0 mysql-bin.000001 mysql-bin.000005 mysql-bin.000009 mysql-bin.000013 mysql-bin.index testgame ib_logfile1 mysql-bin.000002 mysql-bin.000006 mysql-bin.000010 mysql-bin.000014 performance_schemahello larrydb mysql-bin.000003 mysql-bin.000007 mysql-bin.000011 mysql-bin.000015 serv01.host.com.erribdata1 mysql mysql-bin.000004 mysql-bin.000008 mysql-bin.000012 mysql-bin.000016 serv01.host.com.pid[root@serv01 opt]# tar -cvpzf mysql01.tar.gz /usr/local/mysql/data/
第九步,删除数据库文件
[root@serv01 ~]# rm -rf /usr/local/mysql/data/*
第十步,挂载
[root@serv01 ~]# mount /dev/data/mydata /usr/local/mysql/data/[root@serv01 ~]# df -hfilesystem size used avail use% mounted on/dev/sda2 9.7g 2.4g 6.8g 27% /tmpfs 188m 0 188m 0% /dev/shm/dev/sda1 194m 25m 160m 14% /boot/dev/sda5 4.0g 160m 3.7g 5% /opt/dev/sr0 3.4g 3.4g 0 100% /iso/dev/mapper/data-mydata 2.0g 67m 1.9g 4% /usr/local/mysql/data
第十一步,将挂载信息写入配置文件
[root@serv01 opt]# echo /dev/mapper/data-mydata /usr/local/mysql/data ext4 defaults 1 2 >> /etc/fstab [root@serv01 opt]# tail -n1 /etc/fstab /dev/mapper/data-mydata /usr/local/mysql/data ext4 defaults 1 2
第十二步,停掉数据库
[root@serv01 ~]# /etc/init.d/mysqld stop error! mysql server pid file could not be found![root@serv01 ~]# ps -ef | grep mysqldroot 1055 1 0 18:05 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/serv01.host.com.pidmysql 1332 1055 0 18:05 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/serv01.host.com.err --pid-file=/usr/local/mysql/data/serv01.host.com.pid --socket=/tmp/mysql.sock --port=3306root 1885 1490 0 18:18 pts/0 00:00:00 grep mysqld[root@serv01 ~]# pkill -9 mysql[root@serv01 ~]# ps -ef | grep mysqldroot 1888 1490 0 18:18 pts/0 00:00:00 grep mysqld[root@serv01 ~]# chown mysql.mysql /usr/local/mysql/data/ -r[root@serv01 opt]# ll /usr/local/mysql/data/total 0[root@serv01 opt]# ll /usr/local/mysql/data/ -ddrwxr-xr-x. 2 mysql mysql 4096 sep 10 18:17 /usr/local/mysql/data/
第十三步,恢复数据
[root@serv01 opt]# tar -xpvf mysql01.tar.gz
第十四步,启动数据库,登录mysql,然后查看数据是否丢失
[root@serv01 opt]# /etc/init.d/mysqld startstarting mysql success! [root@serv01 ~]# mysqlwelcome to the mysql monitor. commands end with ; or \g.your mysql connection id is 1server version: 5.5.29-log source distributioncopyright (c) 2000, 2012, oracle and/or its affiliates. all rights reserved.oracle is a registered trademark of oracle corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners.type 'help;' or '\h' for help. type '\c' to clear the current input statement.mysql> use larrydb;database changedmysql> show tables;+-------------------+| tables_in_larrydb |+-------------------+| class || stu |+-------------------+2 rows in set (0.00 sec)mysql> select * from class;+------+--------+| cid | cname |+------+--------+| 1 | linux || 2 | oracle |+------+--------+2 rows in set (0.01 sec)mysql> select * from stu;+------+---------+------+| sid | sname | cid |+------+---------+------+| 1 | larry01 | 1 || 2 | larry02 | 2 |+------+---------+------+2 rows in set (0.00 sec)
第十五步,使用lvs的快照功能创建快照,快照不需要格式化。
[root@serv01 opt]# lvcreate -l 100m -s -n smydata /dev/data/mydata logical volume smydata created
第十六步,挂载
[root@serv01 opt]# mount /dev/data/smydata /mnt[root@serv01 opt]# df -hfilesystem size used avail use% mounted on/dev/sda2 9.7g 2.4g 6.8g 27% /tmpfs 188m 0 188m 0% /dev/shm/dev/sda1 194m 25m 160m 14% /boot/dev/sda5 4.0g 161m 3.7g 5% /opt/dev/sr0 3.4g 3.4g 0 100% /iso/dev/mapper/data-mydata 2.0g 98m 1.8g 6% /usr/local/mysql/data/dev/mapper/data-smydata 2.0g 98m 1.8g 6% /mnt
第十七步,模拟数据丢失和验证快照的数据不会受本身数据的影响
[root@serv01 opt]# cd /mnt[root@serv01 mnt]# lscrm ib_logfile1 mysql-bin.000003 mysql-bin.000008 mysql-bin.000013 mysql-bin.indexgame larrydb mysql-bin.000004 mysql-bin.000009 mysql-bin.000014 performance_schemahello mysql mysql-bin.000005 mysql-bin.000010 mysql-bin.000015 serv01.host.com.erribdata1 mysql-bin.000001 mysql-bin.000006 mysql-bin.000011 mysql-bin.000016 serv01.host.com.pidib_logfile0 mysql-bin.000002 mysql-bin.000007 mysql-bin.000012 mysql-bin.000017 test#进入数据目录,创建一个文件[root@serv01 ~]# cd /usr/local/mysql/data/[root@serv01 data]# touch aa01.txt#进入快照挂载目录,发现没有这个文件[root@serv01 mnt]# ls aa01.txtls: cannot access aa01.txt: no such file or directory
第十八步,备份数据
[root@serv01 mnt]# cd /databackup/[root@serv01 databackup]# lltotal 976-rw-r--r--. 1 root root 995761 sep 10 17:47 mysql01.tar.gz[root@serv01 databackup]# /etc/init.d/mysqld status success! mysql running (2198)[root@serv01 databackup]# tar -cvzf mysql02.tar.gz /mnt[root@serv01 mnt]# rm -rf /usr/local/mysql/data/*[root@serv01 mnt]# /etc/init.d/mysqld stop error! mysql server pid file could not be found![root@serv01 mnt]# pkill -9 mysql[root@serv01 mnt]# ps -ef | grep mysqld | grep grep -v[root@serv01 mnt]# cd /usr/local/mysql/data/[root@serv01 data]# lltotal 0
第十九步,恢复数据,启动数据库,登录mysql,然后查看数据是否丢失
[root@serv01 data]# tar -xvf /databackup/mysql02.tar.gz[root@serv01 data]# lsmnt[root@serv01 data]# cd mnt/[root@serv01 mnt]# mv ./* ../[root@serv01 mnt]# cd ..[root@serv01 data]# lscrm ib_logfile0 mysql mysql-bin.000004 mysql-bin.000008 mysql-bin.000012 mysql-bin.000016 serv01.host.com.errgame ib_logfile1 mysql-bin.000001 mysql-bin.000005 mysql-bin.000009 mysql-bin.000013 mysql-bin.000017 serv01.host.com.pidhello larrydb mysql-bin.000002 mysql-bin.000006 mysql-bin.000010 mysql-bin.000014 mysql-bin.index testibdata1 mnt mysql-bin.000003 mysql-bin.000007 mysql-bin.000011 mysql-bin.000015 performance_schema[root@serv01 data]# /etc/init.d/mysqld startstarting mysql success! [root@serv01 data]# mysqlwelcome to the mysql monitor. commands end with ; or \g.your mysql connection id is 1server version: 5.5.29-log source distributioncopyright (c) 2000, 2012, oracle and/or its affiliates. all rights reserved.oracle is a registered trademark of oracle corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners.type 'help;' or '\h' for help. type '\c' to clear the current input statement.mysql> use larrydb;database changedmysql> select * from class;+------+--------+| cid | cname |+------+--------+| 1 | linux || 2 | oracle |+------+--------+2 rows in set (0.00 sec)mysql> select * from stu;+------+---------+------+| sid | sname | cid |+------+---------+------+| 1 | larry01 | 1 || 2 | larry02 | 2 |+------+---------+------+2 rows in set (0.00 sec)
四 相关阅读
mysql备份与恢复之冷备磁盘管理——lvm
我的邮箱:wgbno27@163.com 新浪微博:@jutdb 微信公众平台:justoracle(微信号:justoracle) 数据库技术交流群:336882565(加群时验证 from csdn xxx) all is well 2013年11月3日 by larry wen
@wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [csdn博客]