mysql数据库备份与恢复


目录

mysql数据库备份与恢复

数据库常用备份方案

数据库备份方案:

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

mysql备份工具mysqldump

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

备份course表

[root@localhost ~]# mysqldump -uroot -p123456 ljl0 course > course$(date '+%Y%m%d%H%M%S').sql
[root@localhost ~]# ls
anaconda-ks.cfg  course20220728215908.sql

备份ljl0数据库

[root@localhost ~]# mysqldump -uroot -p123456 --databases ljl0 > ljl$(date '+%Y%m%d%H%M%S').sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls
anaconda-ks.cfg  course20220728215908.sql  ljl20220728220059.sql

备份所有

[root@localhost ~]# mysqldump -uroot -p123456 --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@localhost ~]# ls
all20220728220324.sql  course20220728215908.sql
anaconda-ks.cfg        ljl20220728220059.sql

mysql数据恢复

删除ljl0数据库

mysql> drop database ljl0;
Query OK, 2 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crouce             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

恢复ljl0数据库

[root@localhost ~]# mysql -uroot -p123456 <ljl20220728220059.sql 
[root@localhost ~]# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crouce             |
| ljl0               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

删除ljl0数据库下的course表

mysql> use ljl0
mysql> drop  table course;
Query OK, 0 rows affected (0.10 sec)
mysql> show tables;
+----------------+
| Tables_in_ljl0 |
+----------------+
| students       |
+----------------+
1 row in set (0.00 sec)

恢复course表

mysql> use ljl0
mysql> source course20220728215908.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+----------------+
| Tables_in_ljl0 |
+----------------+
| course         |
| students       |
+----------------+
2 rows in set (0.00 sec)


删除整个数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crouce             |
| ljl0               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
mysql> drop database ljl0;
Query OK, 2 rows affected (0.01 sec)

恢复整个数据库

[root@localhost ~]# mysql -uroot -p123456 < all20220728220324.sql 
[root@localhost ~]# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crouce             |
| ljl0               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

差异备份与恢复

mysql差异备份

开启MySQL服务器的二进制日志功能

[root@localhost ~]# 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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
server-id=1
log-bin=mysql_bin

[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

完全备份

[root@localhost ~]# mysqldump  -uroot -p123456  --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all1-$(date '+%Y%m%d%H%M%S').sql

增加新内容

[root@localhost ~]# mysql -uroot -p123456
mysql> use ljl0;
mysql> insert course(course_name) values('english'),('math');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | go          |
|  5 | c++         |
|  6 | english     |
|  7 | math        |
+----+-------------+
7 rows in set (0.00 sec)

mysql差异备份恢复

模拟误删数据

mysql> drop database ljl0;
Query OK, 2 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crouce             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

刷新创建新的二进制日志

[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
-rw-r-----. 1 mysql mysql      589 Jul 28 22:32 mysql_bin.000002
-rw-r-----. 1 mysql mysql       19 Jul 28 22:24 mysql_bin.index
[root@localhost data]# cat mysql_bin.index 
./mysql_bin.000002

[root@localhost data]# mysqladmin -uroot -p123456 flush-logs
-rw-r-----. 1 mysql mysql      636 Jul 28 22:36 mysql_bin.000002
-rw-r-----. 1 mysql mysql      154 Jul 28 22:36 mysql_bin.000003
-rw-r-----. 1 mysql mysql       38 Jul 28 22:36 mysql_bin.index
[root@localhost data]# cat mysql_bin.index 
./mysql_bin.000002
./mysql_bin.000003

恢复完全备份

[root@localhost ~]# mysql -uroot -p123456 < all1-20220728222421.sql 
[root@localhost ~]# mysql -uroot -p123456 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crouce             |
| ljl0               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use ljl0;

mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | go          |
|  5 | c++         |
+----+-------------+
5 rows in set (0.00 sec)

恢复差异备份

[root@localhost data]# ll
-rw-r-----. 1 mysql mysql      636 Jul 28 22:36 mysql_bin.000002
-rw-r-----. 1 mysql mysql   858142 Jul 28 22:39 mysql_bin.000003
-rw-r-----. 1 mysql mysql       38 Jul 28 22:36 mysql_bin.index
[root@localhost data]# mysql -uroot -p123456 

mysql> show binlog events in 'mysql_bin.000002';
mysql> show binlog events in 'mysql_bin.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000002 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.37-log, Binlog ver: 4 |
| mysql_bin.000002 | 123 | Previous_gtids |         1 |         154 |                                       |
| mysql_bin.000002 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000002 | 219 | Query          |         1 |         291 | BEGIN                                 |
| mysql_bin.000002 | 291 | Table_map      |         1 |         343 | table_id: 140 (ljl0.course)           |
| mysql_bin.000002 | 343 | Write_rows     |         1 |         401 | table_id: 140 flags: STMT_END_F       |
| mysql_bin.000002 | 401 | Xid            |         1 |         432 | COMMIT /* xid=489 */                  |
| mysql_bin.000002 | 432 | Anonymous_Gtid |         1 |         497 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000002 | 497 | Query          |         1 |         589 | drop database ljl0                    |
| mysql_bin.000002 | 589 | Rotate         |         1 |         636 | mysql_bin.000003;pos=4                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
10 rows in set (0.00 sec)

[root@localhost data]# mysqlbinlog --stop-position=497 /opt/data/mysql_bin.000002 |mysql -uroot -p123456
[root@localhost data]# mysql -uroot -p123456 
mysql> use ljl0;

mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | go          |
|  5 | c++         |
|  6 | english     |
|  7 | math        |
+----+-------------+
7 rows in set (0.01 sec)

模拟删除多条

mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | go          |
|  5 | c++         |
+----+-------------+
5 rows in set (0.00 sec)


mysql> delete from course where id =1 or id =2 or id = 3 or id = 4;
Query OK, 4 rows affected (0.01 sec)

mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  5 | c++         |
+----+-------------+
1 row in set (0.00 sec)

mysql> insert course(course_name) values('das'),('hehe'),('haha');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  5 | c++         |
|  6 | das         |
|  7 | hehe        |
|  8 | haha        |
+----+-------------+
4 rows in set (0.00 sec)

刷新新的二进制日志

[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
-rw-r-----. 1 mysql mysql      734 Jul 28 23:20 mysql_bin.000002
-rw-r-----. 1 mysql mysql       19 Jul 28 23:17 mysql_bin.index

[root@localhost data]# mysqladmin -uroot -p123456 flush-logs
-rw-r-----. 1 mysql mysql      781 Jul 28 23:22 mysql_bin.000002
-rw-r-----. 1 mysql mysql      154 Jul 28 23:22 mysql_bin.000003
-rw-r-----. 1 mysql mysql       38 Jul 28 23:22 mysql_bin.index


导出为文本文件

[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql_bin.000004 > /opt/mysql_bin004.txt
[root@localhost data]# cat /opt/mysql_bin004.txt 

恢复完全备份

[root@localhost data]# mysql -uroot -p123456 < /root/all1-20220728222421.sql 
mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | go          |
|  5 | c++         |
+----+-------------+
5 rows in set (0.00 sec)

恢复差异备份

mysql> show binlog events in 'mysql_bin.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000002 |   4 | Format_desc    |        10 |         123 | Server ver: 5.7.37-log, Binlog ver: 4 |
| mysql_bin.000002 | 123 | Previous_gtids |        10 |         154 |                                       |
| mysql_bin.000002 | 154 | Anonymous_Gtid |        10 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000002 | 219 | Query          |        10 |         291 | BEGIN                                 |
| mysql_bin.000002 | 291 | Table_map      |        10 |         343 | table_id: 140 (ljl0.course)           |
| mysql_bin.000002 | 343 | Delete_rows    |        10 |         419 | table_id: 140 flags: STMT_END_F       |
| mysql_bin.000002 | 419 | Xid            |        10 |         450 | COMMIT /* xid=487 */                  |
| mysql_bin.000002 | 450 | Anonymous_Gtid |        10 |         515 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000002 | 515 | Query          |        10 |         587 | BEGIN                                 |
| mysql_bin.000002 | 587 | Table_map      |        10 |         639 | table_id: 140 (ljl0.course)           |
| mysql_bin.000002 | 639 | Write_rows     |        10 |         703 | table_id: 140 flags: STMT_END_F       |
| mysql_bin.000002 | 703 | Xid            |        10 |         734 | COMMIT /* xid=488 */                  |
| mysql_bin.000002 | 734 | Rotate         |        10 |         781 | mysql_bin.000003;pos=4                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
13 rows in set (0.00 sec)

[root@localhost data]# mysqlbinlog --stop-position=734 /opt/data/mysql_bin.000002 |mysql -uroot -p123456
mysql> select * from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  5 | c++         |
|  6 | das         |
|  7 | hehe        |
|  8 | haha        |
+----+-------------+
4 rows in set (0.00 sec)

二进制日志转换文本文件:

mysqlbinlog –no-defaults –base64-output=decode-rows -v mysql_bin.000004 > /opt/mysql_bin004.txt

根据时间恢复:

mysqlbinlog –stop-datetime=’22-07-28 15:42:44′ /opt/data/mysql_bin.000004 | mysql -uroot -p123456

根据操作id号恢复:

mysqlbinlog –stop-position=769 /opt/data/mysql_bin.000004 | mysql -uroot -p123456

原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/tech/database/277673.html

(0)
上一篇 2022年7月29日 09:49
下一篇 2022年7月29日 09:49

相关推荐

发表回复

登录后才能评论