日志是mysql数据库的重要组成部分。日志文件中记录着mysql数据库运行期间发生的变化;也就是说用来记录mysql数据库的客户端连接状况、SQL语句的执行情况和错误信息等。当数据库遭到意外的损坏时,可以通过日志查看文件出错的原因,并且可以通过日志文件进行数据恢复。下面是对MYSQL日志和数据恢复的简单介绍。
一、MySQL日志
主要包含:错误日志、查询日志、慢查询日志、事务日志、二进制日志
二 、MySQL备份工具
mysqldump:逻辑备份工具 ,适用于所有引擎,可用于温备,能实现完全备份,部分备份
cp,tar等文件系统工具:物理备份工具,适用于所有存储引擎,用于冷备,能实现完全备份、部分备份
lvm2的快照:几乎热备;借助文件系统工具实现物理备份
mysqlhotcopy:几乎冷备;仅用于MyISAM存储引擎
三、由于二进制日志格外重要,所以这里介绍基于二进制的mysql备份方法
方法1、mysqldump + binlog
完全备份,通过备份二进制日志实现增量备份
命令的语法格式
mysqldump [OPTIONS] database [tables]:备份单个库,或库指定的一个或多个表
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2DB3...]:备份一个或多个库
mysqldump [OPTIONS] --all-databases [OPTIONS]:备份所有库
实验步骤如下:
①准备备份目录
[root@centos7 ~]# mkdir /mysqlback 创建备份目录 [root@centos7 ~]# chown -R mysql.mysql /mysqlback/ 修改权限
②准备备份数据库及表
[root@centos7 ~]# mysql -uroot -p 登录数据库 MariaDB [(none)]> create database magedu; 创建数据库 MariaDB [(none)]>use magedu; MariaDB [magedu]> create table m26(id int not null.name char (20)); 创建数据表
③进行完整备份
[root@centos7 /mysqlback]# mysqldump --database magedu --flush-log > /mysqlback/mysql-all-back-`date +%F-%T`.sql 完整数据库备份 [root@centos7 /mysqlback]# mysqldump --database magedu --flush-log > /mysqlback/mysql-magedu-back-`date +%F-%T`.sql 单个数据库备份,备份magedu数据库 [root@centos7 /mysqlback]# ls mysql-all-back-2017-11-16-14:47:03.sql mysql-magedu-back-2017-11-16-14:48:44.sql
④向表中插入数据
MariaDB [(none)]> use magedu; Database changed MariaDB [magedu]> insert into m26 values(004,'xiaohong'); Query OK, 1 row affected (0.01 sec) MariaDB [magedu]> insert into m26 values(005,'xiaolan'); Query OK, 1 row affected (0.00 sec) MariaDB [magedu]> select * from m26; +----+----------+ | id | name | +----+----------+ | 1 | one | | 2 | two | | 3 | three | | 4 | xiaohong | | 5 | xiaolan | +----+----------+
⑤进行增量备份,备份二进制日志
[root@centos7 /var/lib/mysql]# mysqlbinlog bin-log.000007 查看position(BEGIN418 COMMIT644) [root@centos7 /var/lib/mysql]# mysqlbinlog --start-position=418 --stop-position=644 /var/lib/mysql/bin-log.000007 > /mysqlback/bin-log-`date +%F_%T`.sql 根据上面查到的position位置进行增量备份
⑥继续向数据库中插入数据,没备份直接删除数据库
MariaDB [(none)]> use magedu; Database changed MariaDB [magedu]> insert into m26 values(008,'liuyifei'); Query OK, 1 row affected (0.03 sec) MariaDB [magedu]> drop database magedu;
⑦数据恢复,首先保护好最后的二进制日志,查看删除数据库之前的position值
BEGIN /*!*/; # at 714 #171116 15:07:56 server id 1 end_log_pos 817 Query thread_id=11 exec_time=0 error_code=0 SET TIMESTAMP=1510816076/*!*/; insert into m26 values(008,'liuyifei') /*!*/; # at 817 #171116 15:07:56 server id 1 end_log_pos 844 Xid = 439 COMMIT/*!*/; # at 844 #171116 15:08:16 server id 1 end_log_pos 929 Query thread_id=11 exec_time=0 error_code=0 SET TIMESTAMP=1510816096/*!*/; drop database magedu
⑧将最后操作的二进制日志备份
[root@centos7 /var/lib/mysql]# mysqlbinlog --stop-position=817 /var/lib/mysql/bin-log.000007 > /mysqlback/bin-log-`date +%F-%T`.sql
⑨导入之前的所有备份
[root@centos7 /var/lib/mysql]# cd /mysqlback/ [root@centos7 /mysqlback]# ls bin-log-2017-11-16_15:02:49.sql mysql-all-back-2017-11-16-14:47:03.sql bin-log-2017-11-16-15:16:41.sql mysql-magedu-back-2017-11-16-14:48:44.sql bin-log-.sql [root@centos7 /mysqlback]# mysql -uroot -p < mysql-all-back-2017-11-16-14:47:03.sql [root@centos7 /mysqlback]# mysql -uroot -p < mysql-magedu-back-2017-11-16-14:48:44.sql [root@centos7 /mysqlback]# mysql -uroot -p < bin-log-2017-11-16_15\:02\:49.sql [root@centos7 /mysqlback]# mysql -uroot -p < bin-log-2017-11-16-15:16:41.sql
⑩查看数据库及数据(恢复成功)
[root@centos7 /mysqlback]# mysql -uroot -p MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | magedu | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> use magedu; Database changed MariaDB [magedu]> select * from m26; +----+----------+ | id | name | +----+----------+ | 1 | one | | 2 | two | | 3 | three | | 5 | xiaolan | | 4 | xiaohong | | 6 | liuyifei | +----+----------+ 6 rows in set (0.00 sec)
方法2、lbm2快照+binlog
LVM快照简单来说就是将所快照源分区一个时间点所有文件的元数据(记录数据属性的数据)进行保存,如果源文件没有改变,那么访问快照卷的相应文件则直接指向源分区的源文件,如果源文件发生改变,则快照卷中与之对应的文件不会发生改变。快照卷主要用于辅助备份文件。
实验步骤如下:
①添加一块硬盘
②并划分磁盘类型为lvm类型
[root@centos7 ~]# echo '- - -' >/sys/class/scsi_host/host0/s 使主机识别硬盘 [root@centos7 ~]# fdisk -l 查看是否添加成功 Disk /dev/sde: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes 分区 [root@centos7 ~]# fdisk /dev/sde 对磁盘进行分区 Welcome to fdisk (util-linux 2.23.2).
③对此磁盘的操作如下
[root@centos7 ~]# partprobe 重读分区表 [root@centos7 ~]# fdisk -l 查看 Device Boot Start End Blocks Id System /dev/sde1 2048 209715199 104856576 8e Linux LVM [root@centos7 ~]# pvcreate /dev/sde1 初始化物理卷 Physical volume "/dev/sde1" successfully created. [root@centos7 ~]# vgcreate myvg /dev/sde1 Volume group "myvg" successfully created [root@centos7 ~]# lvcreate -n mydata -L 50G myvg 创建LVM卷组 Logical volume "mydata" created. [root@centos7 ~]# mkfs.ext4 /dev/mapper/myvg-mydata 格式化分区 mke2fs 1.42.9 (28-Dec-2013 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@centos7 ~]# mkdir /bak 创建一个挂载目录 [root@centos7 ~]# mount /dev/mapper/myvg-mydata /bak 挂载 [root@centos7 ~]# cd /bak [root@centos7 /bak]# ls lost+found
④对数据库的操作如下
[root@centos7 /bak]# systemctl stop mariadb [root@centos7 /bak]# cd /var/lo local/ lock/ log/ [root@centos7 /bak]# cd /var/lib/mysql/ [root@centos7 /var/lib/mysql]# mv * /bak [root@centos7 /var/lib/mysql]# cd /bak [root@centos7 /bak]# ls aria_log.00000001 bin-log.000004 ibdata1 magedu.sql mysql-bin.000004 aria_log_control bin-log.000005 ib_logfile0 mysql mysql-bin.000005 bin-log.000001 bin-log.000006 ib_logfile1 mysql-bin.000001 mysql-bin.index bin-log.000002 bin-log.000007 lost+found mysql-bin.000002 performance_schema bin-log.000003 bin-log.index magedu mysql-bin.000003 test [root@centos7 /bak]# chown mysql.mysql . -R
⑤整理
[root@centos7 /bak]# vim /etc/my.cnf datadir=/bak 使得数据文件在逻辑卷上 [root@centos7 /bak]# systemctl start mariadb [root@centos7 /bak]# mysql -uroot -p MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; 锁定表 Query OK, 0 rows affected (0.00 sec) [root@centos7 /bak]# lvcreate -L 1G -n mydata-snap -p r -s /dev/mapper/myvg-mydata 创建快照卷 Using default stripesize 64.00 KiB. Logical volume "mydata-snap" created. [root@centos7 /bak]# mysql -uroot -p MariaDB [(none)]> UNLOCK TABLES; 解锁所有表 Query OK, 0 rows affected (0.00 sec) [root@centos7 /bak]# mkdir /snap [root@centos7 /bak]# mount /dev/mapper/myvg-mydata /snap 挂载快照卷 [root@centos7 /bak]# cd /snap/ [root@centos7 /snap]# ls (这里是存储的元数据,真实的数据存储在/bak下) aria_log.00000001 bin-log.000004 bin-log.index magedu mysql-bin.000003 test aria_log_control bin-log.000005 ibdata1 magedu.sql mysql-bin.000004 bin-log.000001 bin-log.000006 ib_logfile0 mysql mysql-bin.000005 bin-log.000002 bin-log.000007 ib_logfile1 mysql-bin.000001 mysql-bin.index bin-log.000003 bin-log.000008 lost+found mysql-bin.000002 performance_schema
⑥打包备份并删库
[root@centos7 /snap]# tar -cjvf mysql-backup.tar.gz ./* 打包物理备份 [root@centos7 /snap]# cd /bak [root@centos7 /bak]# mv mysql-backup.tar.gz /root [root@centos7 /bak]# rm -rf * (删除bak下的数据,snap里也没有了) [root@centos7 /bak]# ls [root@centos7 /bak]# cd /snap/ [root@centos7 /snap]# ls [root@centos7 /snap]# cd /bak [root@centos7 /bak]# mv /root/mysql-backup.tar.gz ./ [root@centos7 /bak]# ls mysql-backup.tar.gz [root@centos7 /bak]# tar -xvf mysql-backup.tar.gz [root@centos7 /bak]# ls (bak和snap里的数据都恢复了) aria_log.00000001 bin-log.000005 ib_logfile0 mysql-backup.tar.gz mysql-bin.index aria_log_control bin-log.000006 ib_logfile1 mysql-bin.000001 performance_schema bin-log.000001 bin-log.000007 lost+found mysql-bin.000002 test bin-log.000002 bin-log.000008 magedu mysql-bin.000003 bin-log.000003 bin-log.index magedu.sql mysql-bin.000004 bin-log.000004 ibdata1 mysql mysql-bin.000005 [root@centos7 /bak]# cd /snap/ [root@centos7 /snap]# ls aria_log.00000001 bin-log.000005 ib_logfile0 mysql-backup.tar.gz mysql-bin.index aria_log_control bin-log.000006 ib_logfile1 mysql-bin.000001 performance_schema bin-log.000001 bin-log.000007 lost+found mysql-bin.000002 test bin-log.000002 bin-log.000008 magedu mysql-bin.000003 bin-log.000003 bin-log.index magedu.sql mysql-bin.000004 bin-log.000004 ibdata1 mysql mysql-bin.000005
⑦测试
[root@centos7 /snap]# systemctl start mariadb [root@centos7 /snap]# mysql -uroot -p (数据库都回来了) Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> use magedu; Database changed MariaDB [magedu]> select * from m26; +----+----------+ | id | name | +----+----------+ | 1 | one | | 2 | two | | 3 | three | | 5 | xiaolan | | 4 | xiaohong | | 5 | xiaolan | | 9 | aodaili | +----+----------+ 7 rows in set (0.07 sec)
总结:数据无价,学好备份与恢复很重要!!
来源:https://www.cnblogs.com/Qian-free/p/7846265.html