通过防火墙禁止web等应用向主库写数据或者锁表,让主库暂时停止更新,然后进行恢复
模拟整个场景
1、登录数据库
[root@promote 3306]# mysql -uroot -S /data/3306/mysql.sock
创建一个库,并创建一个表,里面适当的加入一些初始数据
insert into nima(id,name) values(4,'hahh');
2、将我本机的时间设置为早上0点
[root@promote ~]# date -s '2017/7/27'
2017年 07月 27日 星期四 00:00:00 CST
3、完全备份
[root@promote 3306]# mysqldump -uroot -S /data/3306/mysql.sock -F -B --master-data=2 test >/opt/test.sql
[root@promote 3306]# ll
总用量 36
drwxr-xr-x. 5 mysql mysql 4096 7月 20 15:15 data
-rw-r--r--. 1 root root 1897 7月 18 20:11 my.cnf
-rwx------. 1 root root 1299 7月 18 20:29 mysql
-rw-rw----. 1 mysql mysql 1047 7月 27 00:02 mysql-bin.000002
-------------------------------------------------------------------------------备份点 //最好提前看一下从哪里开始备份的,就是查看新的bin-log
-rw-rw----. 1 mysql mysql 107 7月 27 00:02 mysql-bin.000003 --------------新内容
-rw-rw----. 1 mysql mysql 56 7月 27 00:02 mysql-bin.index
-rw-rw----. 1 mysql mysql 6 7月 20 15:15 mysqld.pid
-rw-r-----. 1 mysql root 4184 7月 20 15:15 mysql_oldboy3306.err
srwxrwxrwx. 1 mysql mysql 0 7月 20 15:15 mysql.sock
4、在创建的表中加入新的数据
没加入数据前
mysql> select * from nima;
+------+--------+
| id | name |
+------+--------+
| 1 | 尼玛 |
| NULL | hahh |
| 3 | hahh |
| 4 | hahh |
+------+--------+
4 rows in set (0.00 sec)
mysql> insert into nima(id,name) values(100,'hahh');
Query OK, 1 row affected (0.00 sec)
mysql> insert into nima(id,name) values(101,'hahh');
mysql> select * from nima;
+------+--------+
| id | name |
+------+--------+
| 1 | 尼玛 |
| NULL | hahh |
| 3 | hahh |
| 4 | hahh |
| 100 | hahh |
| 101 | hahh |
+------+--------+
6 rows in set (0.00 sec)
5、删除创建的数据库
mysql> drop database test;
Query OK, 1 row affected (0.07 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
6、此时就出现了问题,test库没有了,然后去查看bin-log
[root@promote 3306]# mysqlbinlog mysql-bin.000003
# at 511
#170727 0:08:56 server id 1 end_log_pos 592 Querythread_id=3exec_time=0error_code=0
SET TIMESTAMP=1501085336/*!*/;
drop database test <==发现了是此条语句的问题
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
7、此时刷新一下bin-log
[root@promote 3306]# mysqladmin -uroot -S /data/3306/mysql.sock flush-logs
[root@promote 3306]# ll
总用量 40
drwxr-xr-x. 4 mysql mysql 4096 7月 27 00:08 data
-rw-r--r--. 1 root root 1897 7月 18 20:11 my.cnf
-rwx------. 1 root root 1299 7月 18 20:29 mysql
-rw-rw----. 1 mysql mysql 1047 7月 27 00:02 mysql-bin.000002
-rw-rw----. 1 mysql mysql 635 7月 27 00:18 mysql-bin.000003
---------------------------------------------------------------------------需要恢复03的内容,然后04就是新内容
-rw-rw----. 1 mysql mysql 107 7月 27 00:18 mysql-bin.000004
-rw-rw----. 1 mysql mysql 84 7月 27 00:18 mysql-bin.index
-rw-rw----. 1 mysql mysql 6 7月 20 15:15 mysqld.pid
-rw-r-----. 1 mysql root 4184 7月 20 15:15 mysql_oldboy3306.err
srwxrwxrwx. 1 mysql mysql 0 7月 20 15:15 mysql.sock
8、开始恢复手续
[root@promote 3306]# cp mysql-bin.000003 /tmp
[root@promote 3306]# cd /tmp
[root@promote tmp]# ll
总用量 4
-rw-r-----. 1 root root 635 7月 27 00:20 mysql-bin.000003
drwx------. 2 root root 24 6月 22 13:45 ssh-sq615X9ctBqx
drwx------. 3 root root 16 6月 22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-colord.service-FFHLPi
drwx------. 3 root root 16 6月 22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-cups.service-jm0Csy
drwx------. 3 root root 16 6月 22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-rtkit-daemon.service-90fMAb
drwx------. 3 root root 16 6月 22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-vmtoolsd.service-HfIX2Y
[root@promote tmp]# mysqlbinlog -d test mysql-bin.000003 >bin.sql
[root@promote tmp]# vim bin.sql //删除有问题的语句
[root@promote tmp]# mysql -uroot -S /data/3306/mysql.sock < /opt/test.sql //恢复全备
[root@promote tmp]# mysql -uroot -S /data/3306/mysql.sock test
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from nima;
+------+--------+
| id | name |
+------+--------+
| 1 | 尼玛 |
| NULL | hahh |
| 3 | hahh |
| 4 | hahh |
| 100 | hahh |
| 101 | hahh |
+------+--------+
6 rows in set (0.00 sec)
mysql>
恢复结束