首先,默认情况下binlog功能是没有打开的,需要修改my.cnf来开启
[root@vmtest ~]# grep bin /disk2/mysql_multi_instances/3308/my.cnf
log-bin = /disk2/mysql_multi_instances/3308/mysql-bin <<最最重要就是这条命令来开启,而且必须写在[mysqld]下面
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
查看binlog的设置值,可以这样查看
[root@lab ~]# mysql -uroot -pjet-speed -e "show variables;"|grep bin
其次,模拟一次灾难恢复
库里的内容如下
mysql> select * from student;
+----+-----------+-------+ | id | name | score | +----+-----------+-------+ | 1 | coosh | 100 | | 2 | mingming | 90 | | 3 | xiaozhang | 60 | | 4 | mama | 63 | +----+-----------+-------+ 4 rows in set (0.00 sec)
全库备份一次;
[root@lab ~]# mysqldump -uroot -pjet-speed -B mydb -F -x --events --master-data=1|gzip >mydb.sql.gz
在student表里插入三条数据;
mysql> insert into student (name,score) values('hsooc',100), ('ginmginm',90), ('amam',63);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
再把整个student表删掉
mysql> drop table student;
Query OK, 0 rows affected (0.00 sec)
开始灾难恢复
查看一下备份文件mydb.sql.gz
[root@lab ~]# zcat mydb.sql.gz
-- MySQL dump 10.13 Distrib 5.1.71, for redhat-linux-gnu (x86_64)
--
-- Host: localhost Database: mydb
-- ------------------------------------------------------
-- Server version 5.1.71-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Position to start replication or point-in-time recovery from
--
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=106;
【一堆内容】
上面内容的最后一句,就是--master-data=1的效果,提醒DBA,下一个SQL点的位置是在文件mysql-bin.000005里,定位在106。
我们先来恢复这个备份文件的数据先,稍后使用mysqlbinlog进行增量恢复。
[root@lab ~]# gzip -d mydb.sql.gz <<得到文本文件mydb.sql
[root@lab ~]# mysql -uroot -p mydb <mydb.sql
[root@lab ~]# mysql -uroot -p -e "use mydb;select * from student;"
+----+-----------+-------+ | id | name | score | +----+-----------+-------+ | 1 | coosh | 100 | | 2 | mingming | 90 | | 3 | xiaozhang | 60 | | 4 | mama | 63 | +----+-----------+-------+
已恢复到第4 条记录
查看binlog文件
[root@lab ~]# mysqlbinlog -d mydb /var/lib/mysql/mysql-bin.000005|grep -B 20 'drop table' # at 106 #160122 22:16:13 server id 1 end_log_pos 134 Intvar SET INSERT_ID=5/*!*/; # at 134 #160122 22:16:13 server id 1 end_log_pos 279 Query thread_id=12 exec_time=0 error_code=0 use `mydb`/*!*/; SET TIMESTAMP=1453472173/*!*/; SET @@session.pseudo_thread_id=12/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C latin1 *//*!*/; SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; insert into student (name,score) values('hsooc',100), ('ginmginm',90), ('amam',63) /*!*/; # at 279 #160122 22:17:05 server id 1 end_log_pos 360 Query thread_id=12 exec_time=0 error_code=0 SET TIMESTAMP=1453472225/*!*/; drop table student
使用-d可以过滤输出指定数据库的内容。可以看到drop table student是at 279位置,再前一个位置134就是我们要恢复到的点。也就是说我们的恢复点从106到134;
[root@lab ~]# mysqlbinlog -d mydb /var/lib/mysql/mysql-bin.000005 --start-position=106 --stop-position=279 >mydb.binlog.sql [root@lab ~]# less mydb.binlog.sql /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #160122 22:13:53 server id 1 end_log_pos 106 Start: binlog v 4, server v 5.1.71-log created 160122 22:13:53 # Warning: this binlog is either in use or was not closed properly. BINLOG ' ITmiVg8BAAAAZgAAAGoAAAABAAQANS4xLjcxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC '/*!*/; # at 106 #160122 22:16:13 server id 1 end_log_pos 134 Intvar SET INSERT_ID=5/*!*/; # at 134 #160122 22:16:13 server id 1 end_log_pos 279 Query thread_id=12 exec_time=0 error_code=0 use `mydb`/*!*/; SET TIMESTAMP=1453472173/*!*/; SET @@session.pseudo_thread_id=12/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C latin1 *//*!*/; SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; insert into student (name,score) values('hsooc',100), ('ginmginm',90), ('amam',63) /*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; mydb.binlog.sql (END)
导入并查看,成功恢复。
[root@lab ~]# mysql -uroot -p mydb <mydb.binlog.sql [root@lab ~]# mysql -uroot -p -e "use mydb;select * from student;" +----+-----------+-------+ | id | name | score | +----+-----------+-------+ | 1 | coosh | 100 | | 2 | mingming | 90 | | 3 | xiaozhang | 60 | | 4 | mama | 63 | | 5 | hsooc | 100 | | 6 | ginmginm | 90 | | 7 | amam | 63 | +----+-----------+-------+