使用bin log进行备份恢复一例

创建数据:

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)
发布了23 篇原创文章 · 获赞 2 · 访问量 1258

猜你喜欢

转载自blog.csdn.net/weixin_43424368/article/details/103704085
今日推荐