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命令。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章