01 mysql备份与恢复
1、mysqldump备份数据库(使用场合:1G以下的数据库)
1)单个数据库
[[email protected] ~]# mysqldump -uroot --databases hellodb > /root/helldb01.sql
2)多个数据库
[[email protected] ~]# mysqldump -uroot --databases hellodb mydb > /root/hellodb_mydb.sql
3)备份全部数据库
[[email protected] ~]# mysqldump -uroot --all-databases --lock-all-tables --master-data=2 > /root/all.sql
2、灾难恢复事例:(本示例有误,需要整理)
原服务器:192.168.1.73
恢复服务器:192.168.1.72
原服务器:执行插入、删除操作;
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> desc students;
#插入一条记录
MariaDB [hellodb]> insert into students (Name,Age,Gender,ClassID,TeacherID) VALUES (‘Chao Cai‘,47,‘M‘,3,7);
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 26 | Chao Cai | 47 | M | 3 | 7 |
+-------+---------------+-----+--------+---------+-----------+
26 rows in set (0.03 sec)
#删除某一条记录
MariaDB [hellodb]> DELETE FROM students WHERE StuID=3;
还原服务器:先使用mysqldump备份文件还原,再使用二进制日志文件还原
[[email protected] ~]# scp all.sql 192.168.1.72:/root
[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# mysql
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> \q
[[email protected] ~]# mysql < all.sql
[[email protected] ~]# mysql
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
查看备份文件,查到MASTER_LOG_POS的值,并记下
[[email protected] ~]# less all.sql
-- MySQL dump 10.14 Distrib 5.5.50-MariaDB, for Linux (x86_64)
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 5.5.50-MariaDB
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @[email protected]@TIME_ZONE */;
/*!40103 SET TIME_ZONE=‘+00:00‘ */;
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;
--
-- Position to start replication or point-in-time recovery from
--
-- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000001‘, MASTER_LOG_POS=245;
--
-- Current Database: `hellodb`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `hellodb` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `hellodb`;
--
这里的值为:MASTER_LOG_POS=245
[[email protected] ~]# mysqlbinlog --start-position=245 /var/lib/mysql/mysql-bin.000001 > incre.sql
[[email protected] ~]# scp incre.sql 192.168.1.72:/root
[[email protected] ~]# mysql < incre.sql
[[email protected] ~]# mysql
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 26 | Chao Cai | 47 | M | 3 | 7 |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
数据恢复完成
3、 基于lvm2实现热备(本示例待验证)
配置mariadb存储的lvm2分区
[[email protected] ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (51374080-251658239, default 51374080):
Using default value 51374080
Last sector, +sectors or +size{K,M,G} (51374080-251658239, default 251658239): +10G
Partition 3 of type Linux and of size 10 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[[email protected] ~]# partx -a /dev/sda
[[email protected] ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
[[email protected] ~]# vgcreate myvg /dev/sda3
Volume group "myvg" successfully created
[[email protected] ~]# lvcreate -L +5G -n mydata myvg
Logical volume "mydata" created.
[[email protected] ~]# mkfs.xfs /dev/myvg/mydata
[[email protected] ~]# systemctl stop mariadb.service
[[email protected] ~]# mount /dev/myvg/mydata /data/
[[email protected] ~]# mkdir -p /data/mysql
[[email protected] ~]# chown -R mysql.mysql /data/mysql/
[[email protected] ~]# vim /etc/my.cnf
修改
datadir=/var/lib/mysql
为
datadir=/data/mysql
[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# mysql < all.sql
[[email protected] ~]# mysql -e ‘SHOW MASTER STATUS‘ > /root/pos.`date +%F`
[[email protected] ~]# cat pos.2016-11-02
File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000002 245
#对数据库作lvm2备份
[[email protected] ~]# lvcreate -L 500M -s -n mydata-snap -p r /dev/myvg/mydata
Logical volume "mydata-snap" created.
#对数据库作修改操作
MariaDB [(none)]> UNLOCK TABLES;
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> DELETE FROM students WHERE StuID=5;
MariaDB [hellodb]> DELETE FROM students WHERE StuID=11;
MariaDB [hellodb]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
23 rows in set (0.00 sec)
[[email protected] ~]# systemctl stop mariadb.service
[[email protected] ~]# lvremove /dev/myvg/mydata-snap
[[email protected] ~]# umount /dev/myvg/mydata
[[email protected] ~]# mke2fs -t ext4 /dev/myvg/mydata
[[email protected] ~]# mount /dev/myvg/mydata /data/
[[email protected] ~]# mkdir /data/{mysql,binlogs}
[[email protected] ~]# chown -R mysql.mysql /data/*
[[email protected] ~]# vim /etc/my.cnf.d/server.cnf
在[server]行(9行左右)添加
log_bin=/data/binlogs/mysql-bin
[[email protected] ~]# systemctl start mariadb.service
MariaDB [(none)]> SET sql_log_bin=0;
MariaDB [(none)]> source /root/all.sql
MariaDB [test]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [test]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> SET sql_log_bin=1;
MariaDB [test]> FLUSH TABLES WITH READ LOCK;
[[email protected] ~]# mysql -e ‘flush logs;‘
[[email protected] ~]# mysql -e "SHOW MASTER STATUS;"
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 245 | | |
+------------------+----------+--------------+------------------+
[[email protected] ~]# mysql -e "SHOW MASTER STATUS;" > /root/pos-`date +%F`
[[email protected] ~]# cat pos-2016-11-03
File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000004 245
[[email protected] ~]# lvcreate -L 2G -n mydata-snap -s -p r /dev/myvg/mydata Logical volume "mydata-snap" created.
MariaDB [test]> UNLOCK TABLES;
MariaDB [hellodb]> DELETE FROM students WHERE StuID=18;
Query OK, 1 row affected (0.01 sec)
MariaDB [hellodb]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
23 rows in set (0.01 sec)
[[email protected] ~]# mount -r /dev/myvg/mydata-snap /mnt
[[email protected] ~]# ls /mnt/
binlogs lost+found mysql
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# ls mysql/
aria_log.00000001 hellodb ib_logfile0 mydb performance_schema
aria_log_control ibdata1 ib_logfile1 mysql test
[[email protected] mnt]# ls binlogs/
mysql-bin.000001 mysql-bin.000003 mysql-bin.index
mysql-bin.000002 mysql-bin.000004
[[email protected] mnt]# cp -a mysql/ /tmp/
[[email protected] mnt]# cp -a /data/binlogs/ /tmp/
[[email protected] mnt]# systemctl stop mariadb.service
#模拟误删除数据库中数据
[[email protected] mnt]# rm -rf /data/mysql/*
[[email protected] mnt]# rm -rf /data/binlogs/*
#恢复
[[email protected] mnt]# cp -a /tmp/mysql/* /data/mysql/
[[email protected] mnt]# cp -a /tmp/binlogs/* /data/binlogs/
[[email protected] mnt]# systemctl start mariadb.service
[[email protected] mnt]# mysql
MariaDB [(none)]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 30334 |
| mysql-bin.000002 | 1038814 |
| mysql-bin.000003 | 288 |
| mysql-bin.000004 | 245 |
| mysql-bin.000005 | 245 |
+------------------+-----------+
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
#数据恢复到备份时的数据;
MariaDB [hellodb]> \q
Bye
[[email protected] ~]# cat pos-2016-11-03
File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000004 245
[[email protected] ~]# cd /tmp/
[[email protected] ~]# cd /tmp/binlogs/
[[email protected] binlogs]# ls
mysql-bin.000001 mysql-bin.000003 mysql-bin.index
mysql-bin.000002 mysql-bin.000004
[[email protected] binlogs]# mysqlbinlog --start-position=245 mysql-bin.000004 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#161103 13:08:44 server id 1 end_log_pos 245 Start: binlog v 4, server v 5.5.50-MariaDB created 161103 13:08:44
# Warning: this binlog is either in use or was not closed properly.
BINLOG ‘
XMYaWA8BAAAA8QAAAPUAAAABAAQANS41LjUwLU1hcmlhREIAbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAA9VR4dg==
‘/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET [email protected]_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;