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
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/277967.html