日期:2014-05-16  浏览次数:20878 次

MySQL--binlog日志恢复数据
恢复数据的重要命令如下
mysql> flush logs;  默认的日志是mysql-bin.000001,现在刷新了重新开启一个就多了一个mysql-bin.000002
                                ./mysqlbinlog --no-defaults binlog日志名,来查看日志
[root@localhost bin]# ./mysqlbinlog --no-defaults ../var/mysql-bin.000001 | more     //查看bin-log日志的内容
[root@localhost bin]# ./mysqlbinlog --no-defaults ../var/mysql-bin.000001 | ./mysql -uroot -p //恢复mysql-bin.000001日志的内容
如果需要从某个点恢复到某个点,用以下操作
定位: --start-position 开始点
--stop-position 结束点
--start-date 开始时间
--stop-date  结束时间
现在恢复mysql-bin.000002恢复,从134点开始到386结束   
[root@localhost bin]# ./mysqlbinlog --no-defaults --start-position 134 --stop-position=386 ../var/mysql-bin.000002 | ./mysql -uroot -p
 
/**        mysqlbinlog日志恢复数据实验         ****/
//查看一下var下面的内容,现在是没有mysql-log.000001类似的binlog日志的
[root@localhost var]# ls
brocms       ibdata1      ib_logfile1    localhost.pid  mysql-bin.index
brotherblog  ib_logfile0  localhost.err  mysql          test
[root@localhost var]# ../bin/mysql -uroot -p //登录数据库
mysql> use test; //使用test数据库
mysql> flush logs; //刷新binlog日志,新开一个,现在会在var目录下面生成一个mysql-bin.000001的文件,以下的操作都会记录其中
 
//创建一个表
mysql> create table user(
    -> id int auto_increment primary key,
    -> username char(30),
    -> password char(32))
    -> engine=myisam default charset=utf8;
//插入几条测试数据
mysql> insert into user(username,password) values(1,2);
mysql> insert into user(username,password) values(1,2);
mysql> insert into user(username,password) values(1,2);
//新开一个binlog日志,现在会生成一个名为mysql-bin.000002的文件,下面的操作会记录在mysql-bin.000002的文件中
 
mysql> flush logs;
//查询一下内容
mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | 1        | 2        |
|  2 | 1        | 2        |
|  3 | 1        | 2        |
+----+----------+----------+
mysql> delete from user; //现在将数据删除
mysql> drop table user; //将表删除
mysql> select * from user; //查看表里面的内容 
mysql> \q
[root@localhost var]# ls
brocms       ibdata1      ib_logfile1    localhost.pid  mysql-bin.000001  mysql-bin.index
brotherblog  ib_logfile0  localhost.err  mysql          mysql-bin.000002  test
[root@localhost var]# ../bin/mysqlbinlog --no-defaults mysql-bin.000001 | more //查看mysql-bin.000001里面的内容
[root@localhost var]# ../bin/mysqlbinlog --no-defaults mysql-bin.000002 | more //查看mysql-bin.000002里面的内容
[root@localhost var]# ../bin/mysqlbinlog --no-defaults mysql-bin.000001 | ../bin/mysql -uroot -p //用mysql-bin.000001来恢复数据
Enter password:
[root@localhost var]# ../bin/mysql -uroot -p //进数据库查看
mysql> use test;
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user           |
+----------------+
1 row in set (0.00 sec)
 
mysql> select * from user; //查看数据,数据回来了
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | 1        | 2        |
|  2 | 1        | 2        |
|  3 | 1        | 2        |
+----+----------+----------+
3 rows in set (0.00 sec)
 
mysql> \q
如果需要从某个点恢复到某个点,用以下操作
定位: --start-position 开始点
--stop-position 结束点
--start-date 开始时间
--stop-date  结束时间
 
现在恢复mysql-bin.000002恢复,从134点开始到386结束   
[root@localhost bin]# ./mysqlbinlog --no-defaults --start-position 134 --stop-position=386 ../var/mysql-bin.000002 | ./mysql -uroot -p