Linux---MySQL完全备份及其恢复

Linux—MySQL完全备份及其恢复

一.数据备份的重要性

1.在生产环境中,数据的安全性是至关重要的,任何数据的丢失都可能产生严重的后果。
2.造成数据丢失的原因

程序错误、人为错误、计算机失败、磁盘失败、物理灾难

二.数据库备份的分类

1.从物理与逻辑的角度区分

物理备份:

指对数据库操作系统的物理文件(如数据文件、日志文件等)的备份。

物理备份又可以脱机备份(冷备份:在关闭数据库时进行的备份操作,能够较好地保证数据库的完整性)和联机备份(热备份:在数据库运行状态中进行操作,这种备份方法依赖于数据库的日志文件

逻辑备份:

指对数据库的逻辑组件(如表等数据库对象)的备份

2.从数据库的备份策略角度区分

完全备份:

每次都对数据库进行完整的备份

差异备份:

备份那些自从上次完全备份之后被修改过的文件

增量备份:

只有那些在上次完全备份或者增量备份后被修改的文件才会被备份

三.完全备份与恢复

1.完全备份是对整个数据库的备份,数据库结构和文件结构的备份
2.完全备份保存的是备份完成时刻的数据库
3.完全备份时增量备份的基础
4.优点:

备份与恢复操作简单方便

5.缺点:

数据存在大量重复,占用大量的备份空间,备份时间长

四.备份操作

首先创建数据库数据:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

mysql> create database school;

mysql> use school;

mysql> create table info (
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> score decimal(4,1) not null);

mysql> desc info;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(4)       | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)  | NO   |     | NULL    |                |
| score | decimal(4,1) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

mysql> insert into info (name,score) values ('stu01',88),('stu02',77);

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
1.使用tar打包文件夹备份
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin  COPYING  COPYING-test  data  docs  include  lib  man  mysqld.pid  mysql.sock  mysql.sock.lock  mysql-test  README  README-test  share  support-files  usr
[root@localhost mysql]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/

[root@localhost mysql]# ls /opt
mysql-2020-01-09.tar.xz  mysql-5.7.20  rh
2.mysqldump命令对单个库进行完全备份
[root@localhost mysql]# mysqldump -uroot -p school > /opt/school.sql
Enter password: 
[root@localhost mysql]# ls /opt
mysql-2020-01-09.tar.xz  mysql-5.7.20  rh  school.sql
3.mysqldump命令对多个库进行完全备份
[root@localhost mysql]# mysqldump -u root -p --databases school mysql > /opt/db_school_mysql.sql
Enter password: 
[root@localhost mysql]# ls /opt
db_school_mysql.sql  mysql-2020-01-09.tar.xz  mysql-5.7.20  rh  school.sql
4.对所有库进行完全备份
[root@localhost mysql]# mysqldump -uroot -p --opt --all-databases > /opt/all.sql
Enter password: 
[root@localhost mysql]# ls /opt
all.sql  db_school_mysql.sql  mysql-2020-01-09.tar.xz  mysql-5.7.20  rh  school.sql
5.对一张表的备份
[root@localhost mysql]# mysqldump -uroot -p school info > /opt/school_info.sql
Enter password: 
[root@localhost mysql]# ls /opt
all.sql  db_school_mysql.sql  mysql-2020-01-09.tar.xz  mysql-5.7.20  rh  school_info.sql  school.sql
6.对表结构的备份
[root@localhost mysql]# mysqldump -uroot -p -d school info > /opt/info.sql
Enter password: 
[root@localhost mysql]# ls /opt
all.sql  db_school_mysql.sql  info.sql  mysql-2020-01-09.tar.xz  mysql-5.7.20  rh  school_info.sql  school.sql
7.使用source恢复
mysql> use school;
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| info             |
+------------------+

mysql> drop table info;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> source /opt/school.sql

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| info             |
+------------------+

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
8.使用外部mysql命令进行恢复
mysql> drop table info;

mysql> show tables;
Empty set (0.00 sec)

[root@localhost mysql]# mysql -uroot -pabc123 school < /opt/school.sql 
[root@localhost mysql]# mysql -uroot -p
Enter password: 
mysql> use school;
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| info             |
+------------------+

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
原创文章 84 获赞 95 访问量 5910

猜你喜欢

转载自blog.csdn.net/obsessiveY/article/details/103914373