MySQL利用binlog来恢复数据库
1.根据binlog解析出所有ring数据库的所有sql
[mysql@localhost ]$ mysqlbinlog --no-defaults --database=ring --start-datetime="2005-04-20 9:55:00" --stop-datetim="2009-04-08 08:05:00" /u01/mysql/log/mysql-bin.000005 > /u01/mysql/log/mysql_restore5.sql
[mysql@localhost ]$ ls -l /u01/mysql/log/mysql_restore5.sql
-rw-r--r-- 1 mysql dba 407 Apr 8 15:33 /u01/mysql/log/mysql_restore5.sql
--start-datetime=datetime 从哪个点开始解析
--stop-datetim=datetime 从哪个点停止解析
--database=ring 指定需要解析哪个数据库,只提取某个数据库的sql语句
--如果有多个binlog的话需要解析多次,需要解析所有的binlog
2.重新在数据库中执行
[mysql@localhost ]$ mysql -u root < /u01/mysql/log/mysql_restore5.sql
ERROR 1062 (23000) at line 2559580: Duplicate entry 175754263-140 for key 1 --恢复过程中出现主键冲突,导入失败
3.删除已经恢复的数据,重新恢复一遍
[mysql@localhost ]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 7647
Server version: 5.0.67-log Source distribution
Type help; or h for help. Type c to clear the buffer.
root@(none) 02:55:41>drop database ring;
Query OK, 8 rows affected (0.04 sec)
[mysql@localhost ]$ mysql -u root -f < /u01/mysql/log/mysql_restore5.sql -f参数:忽略错误继续
ERROR 1062 (23000) at line 2559580: Duplicate entry 175754263-140 for key 1
ERROR 1062 (23000) at line 2564671: Duplicate entry 138 for key 1
ERROR 1062 (23000) at line 2566216: Duplicate entry 139 for key 1
ERROR 1062 (23000) at line 2566224: Duplicate entry 140 for key 1
ERROR 1062 (23000) at line 2566232: Duplicate entry 141 for key 1
ERROR 1062 (23000) at line 2566240: Duplicate entry 142 for key 1
ERROR 1062 (23000) at line 2648410: Duplicate entry 143 for key 1
ERROR 1062 (23000) at line 2648418: Duplicate entry 144 for key 1
ERROR 1062 (23000) at line 2648581: Duplicate entry 145 for key 1
ERROR 1062 (23000) at line 2648589: Duplicate entry 146 for key 1
ERROR 1062 (23000) at line 2648597: Duplicate entry 147 for key 1
ERROR 1062 (23000) at line 2648605: Duplicate entry 148 for key 1
ERROR 1062 (23000) at line 2649279: Duplicate entry 149 for key 1
ERROR 1062 (23000) at line 2649287: Duplicate entry 150 for key 1
ERROR 1062 (23000) at line 2649295: Duplicate entry 151 for key 1
ERROR 1062 (23000) at line 2649303: Duplicate entry 152 for key 1
ERROR 1062 (23000) at line 2649311: Duplicate entry 153 for key 1
ERROR 1062 (23000) at line 2649319: Duplicate entry 154 for key 1
ERROR 1062 (23000) at line 2649327: Duplicate entry 155 for key 1
ERROR 1062 (23000) at line 2649335: Duplicate entry 156 for key 1
恢复成功,有部分数据冲突需要开发自己去处理了。