binlog日志用于记录所有更新了数据或者已经潜在更新了数据的所有语句。语句以“事件”的形式保存,它描述数据更改。当我们因为某种原因导致数据库出现故障时,就可以利用binlog日志来挽回(前提是已经配置好了binlog),接下来我们来配置
一、开启mysql-binlog日志
在mysql配置文件my.cnf加上如下配置
[mysqld] log-bin=mysql-bin
重启mysql
service mysqld restart
二、备份数据库
1)先查看一下当前数据库情况
mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec)
2)备份数据到/tmp/test.sql
[root@localhost ~]# whereis mysqldump mysqldump: /usr/bin/mysqldump /usr/share/man/man1/mysqldump.1.gz [root@localhost ~]# /usr/bin/mysqldump -uroot -p123456 test > /tmp/test.sql
三、这时模拟误操作(插入3条数据,删除数据库)
mysql> insert into t1 values(3); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values(4); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values(5); Query OK, 1 row affected (0.00 sec) mysql> flush logs;#关闭当前的二进制日志文件并创建一个新文件,新的二进制日志文件的名字在当前的二进制文件的编号上加1。 Query OK, 0 rows affected (0.05 sec)
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 106 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.01 sec)
删除数据
mysql> truncate t1; Query OK, 0 rows affected (0.00 sec) mysql> select * from t1; Empty set (0.01 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | t1 | +----------------+ 1 row in set (0.00 sec)
此时突然数据库损坏或者人为删除
mysql> drop table t1; Query OK, 0 rows affected (0.00 sec) mysql> show tables; Empty set (0.00 sec)
四、此时数据库已经被完全破坏
1)用已经备份的/tmp/test.sql来恢复数据
[root@localhost]# mysql -uroot -p123456 test
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | t1 | +----------------+ 1 row in set (0.00 sec) mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec)
2)还有三条数据没有恢复,怎么办。只能用bin-log来恢复
[root@localhost]# /usr/bin/mysqlbinlog --no-defaults /var/lib/mysql/mysql-bin.000001 | more [root@localhost]# /usr/bin/mysqlbinlog --no-defaults /var/lib/mysql/mysql-bin.000001 | /usr/bin/mysql -uroot -p123456 test
mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | +------+ 5 rows in set (0.00 sec)
3)恢复成功
【总结】:mysql备份和bin-log日志
备份数据:
mysqldump -uroot -p123456 test -l -F '/tmp/test.sql' -l:读锁(只能读取,不能更新) -F:即flush logs,可以重新生成新的日志文件,当然包括log-bin日志
查看binlog日志:
mysql>show master status
导入之前备份数据:
mysql -uroot -p123456 test -v -f
恢复binlog-file二进制日志文件:
mysqlbinlog --no-defaults binlog-file | mysql -uroot -p123456
从某一(367)点开始恢复:
mysqlbinlog --no-defaults --stop-position="367" mysql-bin.000001| mysql -uroot -p123456 test
先查好那一点(用more来查看)
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | more
然后恢复:
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | /usr/bin/mysql -uroot -p123456 test
重置binlog日志
mysql> reset master; Query OK, 0 rows affected (0.01 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 106 | | | +------------------+----------+--------------+------------------+
mysql> flush logs;#关闭当前的二进制日志文件并创建一个新文件,新的二进制日志文件的名字在当前的二进制文件的编号上加1。