重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

MySQL完全备份与恢复概念和实际操作

MySQL完全备份与恢复

本文主要内容

站在用户的角度思考问题,与客户深入沟通,找到六合网站设计与六合网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、成都网站设计、成都外贸网站建设、企业官网、英文网站、手机端网站、网站推广、域名申请雅安服务器托管、企业邮箱。业务覆盖六合地区。

  1. 数据库备份的分类
  2. MySQL完全备份与恢复
  3. MySQL差异备份与恢复
  4. MySQL增量备份概念
  5. MySQL备份恢复操作

一、前言

​ 随着办公自动化和电子商务的飞速发展,企业对信息系统的依赖性越来越高,数据库作为信息系统的核心担当者主要的角色。数据库备份,是在数据库丢失的情况下,能及时恢复重要数据,防止数据丢失的一种重要手段。一个合理的数据库备份方案,应该能够在数据丢失时,有效地恢复数据,同时需要考虑技术实现难度和有效地利用资源。

二、数据库备份的概念与分类

2.1数据备份的重要性

​ 在生产环境中,数据库中数据的安全性是至关重要的,任何数据的丢失都可能产生严重的后果;造成数据丢失的原因如下:

2.2造成数据丢失的原因

  1. 程序错误;一般比较少
  2. 人为错误;这种情况最多
  3. 计算机失败;宕机
  4. 磁盘失败;存储——文件系统——分布式
  5. 灾难;attack或者自然灾害

​ 因此,我们需要尽可能地将数据库中的数据进行各种备份,从而避免因为数据丢失造成业务事故,给公司造成负面影响。

​ 数据库的备份可以从不同角度进行分类,下面我们介绍一下数据库的具体的分类及其概念。

2.3数据库备份的分类

2.3.1物理备份与逻辑备份

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

物理备份又可以分为脱机备份(冷备份)和联机备份(热备份)

​ 冷备份:是在关闭数据库的时候进行的;

​ 热备份:数据库处于运行状态,这种备份方法依赖于数据库的日志文件

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

2.3.2从数据库备份策略分为
  1. 完全备份:每次对数据进行完整的备份
  2. 差异备份:备份那些自从上次完全备份之后被修改过的文件
  3. 增量备份:只有那些在上次完全备份或者增量备份后被修改的文件才会被备份
完全备份的优缺点:
  • 优点:安全,数据的完整性;操作简单方便;
  • 缺点:备份的数据冗余,耗费系统资源;备份时间和恢复时间长
差异备份的优缺点:
  • 优点:一定程度上节约资源,安全性上比较折中;
  • 缺点:一但完全备份挂了就只剩与之有差异的备份了;
增量备份的优缺点:
  • 优点:没有需要备份的冗余数据;
  • 缺点:在安全性上比较低;恢复麻烦

我们使用一个比较形象的例子来说明这三者的区别:

备份方式完全备份差异备份增量备份
完全备份时的状态 table1,table2 table1,table2 table1,table2
第一次添加内容 创建table3 创建table3 创建table3
备份内容 备份table1,2,3 备份table3 备份table3
第二次添加内容 创建table4 创建table4 创建table4
备份内容 备份table1,2,3,4 备份table3,4 备份table4

​ 可以这样归纳:完全备份每次备份的是所有数据备份一次,差异备份的参考对象为第一次完全备份,而增量备份的参考对象为最近(相对之前的备份是最后一次)的一次备份。

三、MySQL完全备份

​ MySQL的备份方式主要有完全备份与增量备份。而完全备份是增量备份的基础。所以,生产环境中,这两种方式都会使用,需要制定合理高效的方案达到备份数据的目的。

3.1完全备份的概念细解

  1. 完全备份是对整个数据库的备份、数据库结构和文件结构的备份
  2. 完全备份保存的是备份完成时刻的数据库
  3. 完全备份是差异备份和增量备份的基础

3.2完全备份实际操作

​ MySQL数据库的备份可以采用两种方式,因为数据库实际上就是文件,可以直接打包数据库文件夹,或者使用专门的备份工具mysqldump进行备份操作。

3.2.1使用tar打包文件夹备份

(1)安装xz压缩格式工具,该格式的压缩率较大。

[root@localhost ~]# yum install -y xz
已加载插件:fastestmirror, langpacks
base                                      | 3.6 kB     00:00     
extras                                    | 2.9 kB     00:00     
updates                                   | 2.9 kB     00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
软件包 xz-5.2.2-1.el7.x86_64 已安装并且是最新版本
无须任何处理

(2)对数据库中的数据库文件打包操作

[root@localhost opt]# tar Pjcf /opt/data-$(date +%F).tar.xz /usr/local/mysql/data/fruit /usr/local/mysql/data/student/
[root@localhost opt]# ls
data-2020-01-07.tar.xz    mysql-5.7.17     rh

(3)对比占用的空间资源

[root@localhost opt]# ls -lh /usr/local/mysql/data/student/
总用量 112K
-rw-r-----. 1 mysql mysql   61 1月   7 14:11 db.opt
-rw-r-----. 1 mysql mysql 8.5K 1月   7 14:14 stu_info.frm
-rw-r-----. 1 mysql mysql  96K 1月   7 14:14 stu_info.ibd
[root@localhost opt]# ls -lh /usr/local/mysql/data/fruit/
总用量 148K
-rw-r-----. 1 mysql mysql   61 1月   6 16:37 db.opt
-rw-r-----. 1 mysql mysql 8.5K 1月   6 18:30 fruit_info.frm
-rw-r-----. 1 mysql mysql 129K 1月   7 12:04 fruit_info.ibd

[root@localhost opt]# ls -lh data-2020-01-07.tar.xz 
-rw-r--r--. 1 root root 1.4K 1月   7 14:32 data-2020-01-07.tar.xz

(4)如果原目录中数据丢失损坏,可以解压到数据目录下回复数据

[root@localhost opt]# cd /usr/local/mysql/data/
[root@localhost data]# ls
auto.cnf        ibdata1      ibtmp1              student
fruit           ib_logfile0  mysql               sys
ib_buffer_pool  ib_logfile1  performance_schema
[root@localhost data]# rm fruit/ -rf
[root@localhost data]# rm student/ -rf
[root@localhost data]# ls
auto.cnf        ib_logfile0  mysql               usr
ib_buffer_pool  ib_logfile1  performance_schema
ibdata1         ibtmp1       sys
[root@localhost data]# tar Pjxf /opt/data-2020-01-07.tar.xz -C .
[root@localhost data]# ls
auto.cnf        ibdata1      ibtmp1              student
fruit           ib_logfile0  mysql               sys
ib_buffer_pool  ib_logfile1  performance_schema  usr
3.2.2使用mysqldump工具备份

​ 我们知道使用打压缩包的方法其实在实际情况中并不是一个非常好的选择,因为这是备份数据库中所有的内容,而mysqldump工具可以更加灵活地控制备份的内容,比如将特定数据库,特定表进行备份。

​ 首先我们有一个如下的数据库系统,其中有两个自己创建的数据库:fruit student

其中fruit中有fruit_info表,student中有stu_info表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fruit              |
| mysql              |
| performance_schema |
| student            |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
mysql> use student ;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| stu_info          |
+-------------------+
1 row in set (0.00 sec)

mysql> use fruit ;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_fruit |
+-----------------+
| fruit_info      |
+-----------------+
1 row in set (0.00 sec)

(1)使用mysqldump命令对某表进行完全备份,命令格式如下:

mysqldump -uroot -p [选项] [数据库名] [数据表名] > /备份路径/备份文件名(要以.sql后缀名结尾)

实例:

[root@localhost ~]# mysqldump -uroot -p fruit fruit_info > /opt/fruit_bak_$(date +%F).sql
Enter password: 
[root@localhost ~]# ls /opt/
data-2020-01-07.tar.xz  fruit_bak_2020-01-07.sql  mysql-5.7.17  rh

(2)使用mysqldump命令对单个数据库进行完全备份,命令格式如下:

mysqldump -uroot -p [选项] [数据库名] > /备份路径/备份文件名(要以.sql后缀名结尾)

实例:

[root@localhost ~]# mysqldump -uroot -p fruit > /opt/fruit_db_backup-$(date +%F).sql
Enter password: 
[root@localhost ~]# ls /opt/
data-2020-01-07.tar.xz    fruit_db_backup-2020-01-07.sql  rh
fruit_bak_2020-01-07.sql  mysql-5.7.17

(3)使用mysqldump命令对多个数据库进行完全备份,命令格式如下:

mysqldump -uroot -p [选项] --databases [数据库名列表] > /备份路径/备份文件名(要以.sql后缀名结尾)

实例:

[root@localhost ~]# mysqldump -uroot -p --databases fruit student > /opt/fruit_and_student_db_backup-$(date +%F).sql
Enter password: 
[root@localhost ~]# ls /opt/
data-2020-01-07.tar.xz
fruit_and_student_db_backup-2020-01-07.sql
fruit_bak_2020-01-07.sql
fruit_db_backup-2020-01-07.sql
mysql-5.7.17
rh

(4)使用mysqldump命令对所有数据库进行完全备份,命令格式如下:

mysqldump -uroot -p [选项] --all-databases > /备份路径/备份文件名(要以.sql后缀名结尾)

实例:

[root@localhost ~]# mysqldump -uroot -p --all-databases > /opt/all_db_backup-$(date +%F).sql
Enter password: 
[root@localhost ~]# ls /opt/
all_db_backup-2020-01-07.sql
data-2020-01-07.tar.xz
fruit_and_student_db_backup-2020-01-07.sql
fruit_bak_2020-01-07.sql
fruit_db_backup-2020-01-07.sql
mysql-5.7.17
rh

(5)使用mysqldump命令直接备份表结构或者整个数据表,命令格式如下:

mysqldump -uroot -p [-d] [数据库名] [数据表名] > /备份路径/备份文件名(要以.sql后缀名结尾)

实例:

[root@localhost ~]# mysqldump -uroot -p -d fruit fruit_info > /opt/table_structure.sql
Enter password: 
[root@localhost ~]# ls /opt/
all_db_backup-2020-01-07.sql
data-2020-01-07.tar.xz
fruit_and_student_db_backup-2020-01-07.sql
fruit_bak_2020-01-07.sql
fruit_db_backup-2020-01-07.sql
mysql-5.7.17
rh
table_structure.sql
[root@localhost ~]# mysqldump -uroot -p  fruit fruit_info > /opt/table_structure-$(date +%F).sql
Enter password: 
[root@localhost ~]# ls /opt/
all_db_backup-2020-01-07.sql
data-2020-01-07.tar.xz
fruit_and_student_db_backup-2020-01-07.sql
fruit_bak_2020-01-07.sql
fruit_db_backup-2020-01-07.sql
mysql-5.7.17
rh
table_structure-2020-01-07.sql
table_structure.sql

我们来对比一下加了选项-d和不加的备份文件的内容

有-d选项:

[root@localhost ~]# cat /opt/table_structure.sql
-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64)

-- Host: localhost Database: fruit


-- Server version 5.7.17

/!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/!40101 SET NAMES utf8 /;
/!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE /;
/!40103 SET TIME_ZONE='+00:00' /;
/!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 /;
/!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 /;
/!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' /;
/!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 /;

--
-- Table structure for table fruit_info

DROP TABLE IF EXISTS fruit_info;
/!40101 SET @saved_cs_client = @@character_set_client /;
/!40101 SET character_set_client = utf8 /;
CREATE TABLE fruit_info (
id int(4) NOT NULL,
price decimal(3,2) NOT NULL,
newtype varchar(6) DEFAULT NULL,
UNIQUE KEY id_index_new (id),
UNIQUE KEY type_index (newtype),
KEY id_index (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/!40101 SET character_set_client = @saved_cs_client /;
/!40103 SET TIME_ZONE=@OLD_TIME_ZONE /;

/!40101 SET SQL_MODE=@OLD_SQL_MODE /;
/!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS /;
/!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS /;
/!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;
/!40111 SET SQL_NOTES=@OLD_SQL_NOTES /;

-- Dump completed on 2020-01-07 14:59:25

没有-d选项:

[root@localhost ~]# cat /opt/table_structure-2020-01-07.sql
-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64)

-- Host: localhost Database: fruit


-- Server version 5.7.17

/!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/!40101 SET NAMES utf8 /;
/!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE /;
/!40103 SET TIME_ZONE='+00:00' /;
/!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 /;
/!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 /;
/!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' /;
/!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 /;

--
-- Table structure for table fruit_info

DROP TABLE IF EXISTS fruit_info;
/!40101 SET @saved_cs_client = @@character_set_client /;
/!40101 SET character_set_client = utf8 /;
CREATE TABLE fruit_info (
id int(4) NOT NULL,
price decimal(3,2) NOT NULL,
newtype varchar(6) DEFAULT NULL,
UNIQUE KEY id_index_new (id),
UNIQUE KEY type_index (newtype),
KEY id_index (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/!40101 SET character_set_client = @saved_cs_client /;

--
-- Dumping data for table fruit_info

LOCK TABLES fruit_info WRITE;
/!40000 ALTER TABLE fruit_info DISABLE KEYS /;
INSERT INTO fruit_info VALUES (1,2.50,'banana'),(2,5.50,'apple'),(3,6.00,'peach');
/!40000 ALTER TABLE fruit_info ENABLE KEYS /;
UNLOCK TABLES;
/!40103 SET TIME_ZONE=@OLD_TIME_ZONE /;

/!40101 SET SQL_MODE=@OLD_SQL_MODE /;
/!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS /;
/!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS /;
/!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;
/!40111 SET SQL_NOTES=@OLD_SQL_NOTES /;

-- Dump completed on 2020-01-07 15:01:04

结论:

​ 可以看出加了-d选项的只是将表的结构进行备份,而不加-d参数的就会将整个数据表进行备份

四、数据库完全恢复

​ 上文介绍了数据库完全备份的具体操作,那么当数据出现错误时,可以使用以下几种方式对其进行恢复操作

4.1恢复整库操作

当需要恢复整库的时候,可以使用source命令和mysql命令

4.1.1source命令整库恢复

命令格式:source 备份脚本路径(绝对路径)

实例:

利用上面备份fruit数据库的例子,模拟删除该库并进行恢复操作

方法:生成备份数据文件——》登录数据库——》删除数据库——》使用source命令恢复

具体操作:

[root@localhost data]# mysqldump -uroot -p student > /opt/student.sql
Enter password: 
[root@localhost data]# ls /opt/
data-2020-01-07.tar.xz  mysql-5.7.17  rh  student.sql
mysql> drop database student;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fruit              |
| mysql              |
| performance_schema |
| sys                |
| usr                |
+--------------------+
6 rows in set (0.00 sec)
mysql> create database student
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> use student;
Database changed
mysql> source /opt/student.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)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| stu_info          |
+-------------------+
1 row in set (0.00 sec)
4.1.2mysql命令整库恢复

不需要登录mysql数据库系统,可以使用mysql命令直接恢复整库。

实例如下:

[root@localhost data]# mysqldump -uroot -p student > /opt/student.sql
Enter password: 
[root@localhost data]# ls /opt/
data-2020-01-07.tar.xz  rh            student.sql
mysql-5.7.17            student1.sql

模拟数据库丢失:

[root@localhost data]# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fruit              |
| mysql              |
| performance_schema |
| student            |
| sys                |
| usr                |
+--------------------+
7 rows in set (0.00 sec)

mysql> drop database student;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fruit              |
| mysql              |
| performance_schema |
| sys                |
| usr                |
+--------------------+
6 rows in set (0.00 sec)

整库恢复:

[root@localhost data]# mysql -uroot -p < /opt/student.sql 
Enter password: 
ERROR 1046 (3D000) at line 22: No database selected
[root@localhost data]# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 43
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database student;
Query OK, 1 row affected (0.01 sec)

mysql> exit
Bye
[root@localhost data]# mysql -uroot -p student < /opt/student.sql 
Enter password: 
[root@localhost data]# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fruit              |
| mysql              |
| performance_schema |
| student            |
| sys                |
| usr                |
+--------------------+
7 rows in set (0.00 sec)

mysql> use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from stu_info;
+----+-------+---------+
| id | name  | address |
+----+-------+---------+
|  1 | zhsan | bj      |
|  2 | wawu  | nj      |
+----+-------+---------+
2 rows in set (0.00 sec)
4.2.3小结

​ 在进行整库恢复时,MySQL5.7版本中都需要先创建库才可以使用source或者mysql命令进行整库恢复。

4.2恢复数据表操作(与整库恢复类似)

4.2.1使用source命令恢复表

(1)创建恢复文件目录存放备份表文件:

[root@localhost opt]# mkdir abc
[root@localhost opt]# cd -
/usr/local/mysql/data
[root@localhost data]# ls
auto.cnf        ibdata1      ibtmp1              student
fruit           ib_logfile0  mysql               sys
ib_buffer_pool  ib_logfile1  performance_schema  usr
[root@localhost data]# mysqldump -uroot -p student stu_info > /opt/abc/table.sql
Enter password: 
[root@localhost data]# ls /opt/abc/
table.sql

(2)查看数据库数据表中的内容

[root@localhost data]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 52
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| stu_info          |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from stu_info;
+----+-------+---------+
| id | name  | address |
+----+-------+---------+
|  1 | zhsan | bj      |
|  2 | wawu  | nj      |
+----+-------+---------+
2 rows in set (0.00 sec)

(3)模拟表文件数据丢失

mysql> drop table stu_info;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

(4)source命令恢复数据表

mysql> source /opt/abc/table.sql;
Query OK, 0 rows affected (0.00 sec)

...#省略部分内容

Query OK, 0 rows affected (0.00 sec
mysql> select * from stu_info;
+----+-------+---------+
| id | name  | address |
+----+-------+---------+
|  1 | zhsan | bj      |
|  2 | wawu  | nj      |
+----+-------+---------+
2 rows in set (0.00 sec)
4.2.2使用mysql命令恢复表

​ 使用mysql命令恢复表的操作的时候,当备份文件中只包含表的备份,而不包括创建库的语句时,必须指定库名,其目标库存在。

​ 实例如下:前面的步骤与source命令的演示类似,就不过多说明了

[root@localhost data]# mysqldump -uroot -p student stu_info > /opt/abc/table1.sql
Enter password: 
[root@localhost data]# ls /opt/abc/
table1.sql  table.sql

[root@localhost data]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 57
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from stu_info;
+----+-------+---------+
| id | name  | address |
+----+-------+---------+
|  1 | zhsan | bj      |
|  2 | wawu  | nj      |
+----+-------+---------+
2 rows in set (0.00 sec)

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

mysql> exit
Bye

恢复表操作:

[root@localhost data]# mysql -uroot -p student < /opt/abc/table1.sql 
Enter password: 
[root@localhost data]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 59
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from stu_info;
+----+-------+---------+
| id | name  | address |
+----+-------+---------+
|  1 | zhsan | bj      |
|  2 | wawu  | nj      |
+----+-------+---------+
2 rows in set (0.00 sec)

4.2.3小结

​ 在恢复表时命令的操作基本一致,只不过使用环境不同而已,而恢复表和恢复库的区别就在于恢复库需要先创建使用库,而恢复数据表则不需要,这是因为备份文件的内容决定的。

五、MySQL数据库备份思路

​ MySQL需要定期实施备份,制定合适的备份计划或者策略,并且严格遵守。除了进行完全备份,开启MySQL服务器的日志功能也是重中之重,完全备份配合日志,可以对MySQL进行最大化还原。

​ 备份文件的名字需要使用统一和易理解的名字,推荐使用库名配合时间的命名方法,这样方便他人和自己使用。

六、本文总结

​ 本文主要讲述数据库备份的分类及其制作备份和恢复数据的实操。数据库备份,从物理与逻辑的角度,备份分为物理(冷热备份)和逻辑备份;从数据库的备份策略角度,备份可以分为完全备份、差异备份与增量备份。

​ MySQL中有专门的mysqldump工具备份,生成的是SQL的脚本文件。而数据库的恢复操作使用mysql或source命令。


当前题目:MySQL完全备份与恢复概念和实际操作
网站路径:http://cqcxhl.cn/article/pghegh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP