微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL数据备份与恢复

MysqL数据库备份与恢复

目录

数据库常用备份方案

数据库备份方案:

  • 全量备份
  • 增量备份
  • 差异备份
备份方案 特点
全量备份 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。 数据恢复快。 备份时间长
增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份 与前一次相比增加和者被修改文件。这就意味着,第一次增量备份的对象 是进行全备后所产生的增加修改文件;第二次增量备份的对象是进行第一次增量 备份后所产生的增加修改文件,如此类推。 没有重复的备份数据 备份时间短 恢复数据时必须按一定的顺序进行
差异备份 备份上一次的完全备份后发生变化的所有文件。 差异备份是指在一次全备份后到进行差异备份的这段时间内 对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。

MysqL备份工具MysqLdump

语法:

MysqLdump [OPTIONS] database [tables ...]
MysqLdump [OPTIONS] --all-databases [OPTIONS]
MysqLdump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]

常用OPTIONS:

-uUSERNAME //指定数据库用户名
-hHOST //指定服务器主机,请使用ip地址
-pPASSWORD //指定数据库用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307

现有以下数据库和表:

MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| centos             |
| linux              |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

MysqL> use linux;
Database changed

MysqL> show tables;
+-----------------+
| Tables_in_linux |
+-----------------+
| tb_1            |
| tb_2            |
+-----------------+
2 rows in set (0.00 sec)

MysqL> select * from tb_1;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   18 |
|  2 | lisi     |   20 |
|  3 | wangwu   |   22 |
|  4 | tom      |   24 |
|  5 | lisa     |   20 |
|  6 | jerry    |   22 |
|  7 | timi     |   16 |
+----+----------+------+
7 rows in set (0.00 sec)

MysqL> select * from tb_2;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | aa   |   10 |
|  2 | bb   |   20 |
|  3 | cc   |   30 |
|  4 | dd   |   40 |
|  5 | ee   |   50 |
+----+------+------+
5 rows in set (0.00 sec)

备份

#备份整个数据库
[root@rh1 ~]# MysqLdump -uroot -p1234 -h127.0.0.1 --all-databases > all-$(date '+%Y%m%d%H%M%s').sql
#下面不是报错内容,只是提示外部输入密码不安全
MysqLdump: [Warning] Using a password on the command line interface can be insecure.
[root@rh1 ~]# ls
all-20220730043700.sql
#添加以下配置文件备份时不用输入密码,而且不会提示密码安全性问题
[root@rh1 ~]# cat .my.cnf 
[MysqLdump]
user= root
password= 1234
#备份数据库Linux中的表tb_1和tb_2
[root@rh1 ~]# MysqLdump linux tb_1 tb_2 > tables-$(date '+%Y%m%d%H%M%s').sql
[root@rh1 ~]# ls
all-20220730043700.sql  tables-20220730044549.sql
#备份数据库centos
[root@rh1 ~]# MysqLdump --databases centos > centos-$(date '+%Y%m%d%H%M%s').sql
[root@rh1 ~]# ls
all-20220730043700.sql  centos-20220730044742.sql       tables-20220730044549.sql

恢复

#模拟误删除数据库centos
MysqL> drop database centos;
Query OK, 0 rows affected (0.01 sec)

MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux              |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
#恢复数据库centos
#方式一
MysqL> source centos-20220730044742.sql;
Query OK, 0 rows affected (0.00 sec)
#方式二
[root@rh1 ~]# MysqL -uroot -p1234 < centos-20220730044742.sql
MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| centos             |
| linux              |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

#模拟删除表tb_1里的内容
MysqL> delete from tb_1 where id = 6 or id = 7;
Query OK, 2 rows affected (0.00 sec)

MysqL> select * from tb_1;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   18 |
|  2 | lisi     |   20 |
|  3 | wangwu   |   22 |
|  4 | tom      |   24 |
|  5 | lisa     |   20 |
+----+----------+------+
5 rows in set (0.00 sec)
#恢复数据库linux中的表tb_1里的内容
#方式一
MysqL> source tables-20220730044549.sql;
#方式二
[root@rh1 ~]# MysqL -uroot -p1234 linux < tables-20220730044549.sql 
MysqL> select * from tb_1;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   18 |
|  2 | lisi     |   20 |
|  3 | wangwu   |   22 |
|  4 | tom      |   24 |
|  5 | lisa     |   20 |
|  6 | jerry    |   22 |
|  7 | timi     |   16 |
+----+----------+------+
7 rows in set (0.00 sec)

差异备份与恢复

备份

#开启MysqL服务器的二进制日志功能
[root@rh1 ~]# vim /etc/my.cnf 

[MysqLd]
basedir = /usr/local/MysqL
datadir = /opt/data
socket = /tmp/MysqL.sock
port = 3306
pid-file = /opt/data/MysqL.pid
user = MysqL
skip-name-resolve
#添加以下两行
server-id=10	#设置服务器的标识id
log-bin=MysqL_bin	#开启二进制日志功能
#重启MysqL服务
[root@rh1 ~]# service MysqLd restart 
Shutting down MysqL.. SUCCESS! 
Starting MysqL. SUCCESS! 
#首先进行一次全量备份
[root@rh1 ~]# MysqLdump  --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-$(date '+%Y%m%d').sql
[root@rh1 ~]# ls
all-20220730.sql
#往表tb_1内插入一条数据
MysqL> insert tb_1(name,age) value('keli',18);
Query OK, 1 row affected (0.00 sec)

MysqL> select * from tb_1
    -> ;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   18 |
|  2 | lisi     |   20 |
|  3 | wangwu   |   22 |
|  4 | tom      |   24 |
|  5 | lisa     |   20 |
|  6 | jerry    |   22 |
|  7 | timi     |   16 |
|  8 | keli     |   18 |
+----+----------+------+
8 rows in set (0.00 sec)

恢复

#模拟误删除数据库
MysqL> drop database linux;
Query OK, 2 rows affected (0.01 sec)

MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| centos             |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
#查看配置文件存放目录下的日志文件
[root@rh1 ~]# cd /opt/data/
[root@rh1 data]# ll
-rw-r-----. 1 MysqL MysqL      581 7月  30 06:37 MysqL_bin.000003
-rw-r-----. 1 MysqL MysqL       19 7月  30 06:36 MysqL_bin.index
[root@rh1 data]# cat MysqL_bin.index 
./MysqL_bin.000003
#刷新创建新的日志,避免MysqL_bin.000003日志内容太多不便于查看和实验
[root@rh1 ~]# MysqLadmin -uroot -p1234 flush-logs
#在数据库下查看日志,
MysqL> show binlog events in 'MysqL_bin.000003'
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| MysqL_bin.000003 |   4 | Format_desc    |        10 |         123 | Server ver: 5.7.38-log, binlog ver: 4 |
| MysqL_bin.000003 | 123 | PrevIoUs_gtids |        10 |         154 |                                       |
| MysqL_bin.000003 | 154 | Anonymous_Gtid |        10 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| MysqL_bin.000003 | 219 | Query          |        10 |         292 | BEGIN                                 |
| MysqL_bin.000003 | 292 | Table_map      |        10 |         344 | table_id: 140 (linux.tb_1)            |
| MysqL_bin.000003 | 344 | Write_rows     |        10 |         390 | table_id: 140 flags: STMT_END_F       |
| MysqL_bin.000003 | 390 | Xid            |        10 |         421 | COMMIT /* xid=486 */                  |
| MysqL_bin.000003 | 421 | Anonymous_Gtid |        10 |         486 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| MysqL_bin.000003 | 486 | Query          |        10 |         581 | drop database linux                   |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
9 rows in set (0.00 sec)
#恢复全量备份
[root@rh1 ~]# MysqL -uroot -p1234 <all-20220730.sql 
MysqL: [Warning] Using a password on the command line interface can be insecure.
#恢复了数据库,但是表内新添加的数据并没有恢复
MysqL> select * from tb_1;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   18 |
|  2 | lisi     |   20 |
|  3 | wangwu   |   22 |
|  4 | tom      |   24 |
|  5 | lisa     |   20 |
|  6 | jerry    |   22 |
|  7 | timi     |   16 |
+----+----------+------+
7 rows in set (0.00 sec)
#差异备份恢复
[root@rh1 ~]# MysqLbinlog --stop-position=486 /opt/data/MysqL_bin.000006 | MysqL -uroot -p1234
MysqL> select * from tb_1;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   18 |
|  2 | lisi     |   20 |
|  3 | wangwu   |   22 |
|  4 | tom      |   24 |
|  5 | lisa     |   20 |
|  6 | jerry    |   22 |
|  7 | timi     |   16 |
|  8 | keli     |   18 |
+----+----------+------+
7 rows in set (0.00 sec)
#将日志提取为txt文件然后查看,根据详细时间进行恢复
[root@rh1 data]# MysqLbinlog --no-defaults --base64-output=decode-rows -v MysqL_bin.000003 > /opt/MysqL_bin003.txt
[root@rh1 data]# MysqLbinlog –stop-datetime=’22-07-28 11:42:34’ /opt/data/MysqL_bin.000003 | MysqL -uroot -p123456

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐