创建数据:
use scott;
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(8) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ;
插入一条记录
insert into person(name) values (‘li’);
备份数据库scott
# mysqldump -uroot -pdingjia -lF --log-error=./mydump.err -B scott > dbscott.sql
由于使用了-F选项,当备份工作刚开始时系统会刷新log日志,产生新的binlog日志来记录备份之后的数据库“增删改”操作,查看一下:
mysql> show master status
-> ;
+----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| localhost-bin.000018 | 154 | | | |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> use scott;
Database changed
mysql> insert into person(name) values ('zhang');
Query OK, 1 row affected (0.01 sec)
mysql> flush logs;
Query OK, 0 rows affected (0.07 sec)
从前面的操作中可以看出在二进制日志( localhost-bin.000018)中,只有插入“zhang”的这条记录。
灾难发生
# rm /var/lib/mysql/scott/person.*
再查看表,发现已经没有了。
mysql> use scott;
Database changed
mysql> show tables;
Empty set (0.00 sec)
恢复数据库
# mysql -uroot -pdingjia -v < dbscott.sql;
查看,已经恢复了一条记录
mysql> select * from scott.person;
+----+------+----------+
| id | name | birthday |
+----+------+----------+
| 1 | li | NULL |
+----+------+----------+
1 row in set (0.01 sec)
恢复二进制日志
# mysqlbinlog /var/lib/mysql/localhost-bin.000018 | mysql -uroot -pdingjia scott -v
再查看,两条记录都恢复了。
mysql> select * from scott.person;
+----+-------+----------+
| id | name | birthday |
+----+-------+----------+
| 1 | li | NULL |
| 2 | zhang | NULL |
+----+-------+----------+
2 rows in set (0.00 sec)