MySQL之备份与还原

MySQL之备份与还原

1、产生一个全新的二进制文件

root@(none) 10:33  sc-mysql>show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| sc-mysql-bin.000004 |    23054 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

root@(none) 10:34  sc-mysql>flush logs;
Query OK, 0 rows affected (0.01 sec)

root@(none) 10:35  sc-mysql>show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| sc-mysql-bin.000005 |      154 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

root@(none) 10:35  sc-mysql>

2、给数据库做全备

[root@sc-mysql mysql]# mysqldump -uroot -p'Sanchuang123#' --databases sanchuang >/backup/sanchuang.sql

3、让数据发生变化,进行insert 和删除操作等

root@sanchuang 10:40  sc-mysql>insert into emp(id,name,deptid) values(3,'苏文洋',20);
Query OK, 1 row affected (0.00 sec)

root@sanchuang 10:40  sc-mysql>insert into emp(id,name,deptid) values(4,'qinjiahui',20);
Query OK, 1 row affected (0.00 sec)

root@sanchuang 10:40  sc-mysql>insert into emp(id,name,deptid) values(5,'chenran',20);
Query OK, 1 row affected (0.00 sec)

root@sanchuang 10:41  sc-mysql>show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| sc-mysql-bin.000005 |      998 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

root@sanchuang 10:41  sc-mysql>

4、模拟出现故障,删除数据库

root@sanchuang 10:41  sc-mysql>drop database sanchuang;

5、开始取恢复数据

  • 第1步:恢复全备
[root@sc-mysql backup]# mysql -uroot -p'Sanchuang123#' < sanchuang.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@sc-mysql backup]#
  • 第2步:查看二进制日志找到删除数据库之前的position 位置号
[root@sc-mysql mysql]# mysqlbinlog  -v sc-mysql-bin.000005|egrep -C 5 "drop database"
#210401 10:42:39 server id 1  end_log_pos 1063 CRC32 0x8623d686 	Anonymous_GTID	last_committed=3	sequence_number=4	rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1063
#210401 10:42:39 server id 1  end_log_pos 1170 CRC32 0xf9eee347 	Query	thread_id=14	exec_time=0	error_code=0
SET TIMESTAMP=1617244959/*!*/;
drop database sanchuang
/*!*/;
# at 1170
#210401 10:44:13 server id 1  end_log_pos 1235 CRC32 0xa92ef3c4 	Anonymous_GTID	last_committed=4	sequence_number=5	rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1235
[root@sc-mysql mysql]#
  • 第3步:使用二进制日志去恢复
[root@sc-mysql mysql]# mysqlbinlog   --start-position=154 --stop-position=1063  sc-mysql-bin.000005|mysql -uroot -p'Sanchuang123#'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@sc-mysql mysql]#

6、查看数据是否恢复

root@sanchuang 10:54  sc-mysql>select * from emp;
+----+-----------+--------+
| id | NAME      | deptid |
+----+-----------+--------+
|  1 | 张三      |     10 |
|  2 | 李四      |     10 |
|  3 | 苏文洋    |     20 |
|  4 | qinjiahui |     20 |
|  5 | chenran   |     20 |
| 19 | 吴佩      |     10 |
+----+-----------+--------+
6 rows in set (0.00 sec)

root@sanchuang 10:55  sc-mysql>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pod️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值