MySQL數據庫的備份與恢復(3)——恢復數據

MySQL數據庫的備份與恢復(3)——恢復數據

使用MySQL自帶的備份工具mysqldump備份數據,然後進行數據恢復。

一、以SQL格式備份數據並且恢復

1、備份所有數據庫

[root@Mysql11 ~]# mysqldump -uroot -p123456 --all-databases > /tmp/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

2、備份指定的多個數據庫

[root@Mysql11 ~]# mysqldump -uroot -p123456 --databases wgx hist > /tmp/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

恢復wgx和hist數據庫

方法一:登錄mysql進行恢復

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

mysql> drop database wgx;
Query OK, 2 rows affected (0.03 sec)

mysql> drop database hist;
Query OK, 2 rows affected (0.03 sec)

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

mysql> source /tmp/all.sql;
Query OK, 0 rows affected (0.00 sec)
.....
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mysql              |
| performance_schema |
| sys                |
| wgx                |
+--------------------+
6 rows in set (0.00 sec)

方法二:不登錄mysql 進行恢復

[root@Mysql11 ~]# mysql -uroot -p123456 -e "drop database wgx;drop database hist;"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# mysql -uroot -p123456 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

[root@Mysql11 ~]# mysql -uroot -p123456 < /tmp/all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# mysql -uroot -p123456 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mysql              |
| performance_schema |
| sys                |
| wgx                |
+--------------------+

3、備份指定的一個數據庫

(1)指定–databases參數

[root@Mysql11 ~]# mysqldump -uroot -p123456 --databases wgx > /tmp/wgx.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

此命令生成的備份文件包含create database和use命令,如下:

[root@Mysql11 ~]# cat /tmp/wgx.sql
--
-- Current Database: `wgx`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `wgx` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `wgx`;

(2)也可以不指定–databases參數

[root@Mysql11 ~]# mysqldump -uroot -p123456 wgx > /tmp/wgx.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

當對一個數據庫進行備份時,可以省略- -databases參數,但是省略該參數後生成的備份文件中沒有CREATE DATABASE 和USE語句,那麼恢復備份文件時,必須指定一個默認的數據庫名。

此時可以使用一個和原始數據庫名稱不同的數據庫名。

[root@Mysql11 ~]# cat /tmp/wgx.sql
-- MySQL dump 10.13  Distrib 5.7.27, for Linux (x86_64)
--
-- Host: localhost    Database: wgx
-- ------------------------------------------------------
-- Server version	5.7.27

/*!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 `department`
--

DROP TABLE IF EXISTS `department`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `department` (
  `dept_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

4、恢復某個數據庫中的所有數據

方法一:如果備份數據庫時有–databases參數

[root@Mysql11 ~]# mysql -uroot -p123456 -e "drop database wgx;"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# mysql -uroot -p123456 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@Mysql11 ~]# mysql -uroot -p123456 < /tmp/wgx.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# mysql -uroot -p123456 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mysql              |
| performance_schema |
| sys                |
| wgx                |
+--------------------+

方法二:如果備份數據庫時沒有–databases參數

[root@Mysql11 ~]# mysql -uroot -p123456 < /tmp/wgx.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1046 (3D000) at line 22: No database selected

注意:如果沒有選擇數據庫,會出現錯誤:ERROR 1046 (3D000) at line 22: No database selected

因此,必須先使用use命令選擇要恢復的數據庫:

[root@Mysql11 ~]# mysql -uroot -p123456 -e "drop database wgx;create database wanggx;use wanggx;source /tmp/wgx.sql;"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# mysql -uroot -p123456 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mysql              |
| performance_schema |
| sys                |
| wanggx             |

可以看到,恢復數據的同時修改了數據庫名稱。

5、恢復某個數據庫中的若干張表

方法一:指定–databases和–tables參數

[root@Mysql11 ~]# mysqldump -uroot -p123456 --databases hist --tables dept stu > hist.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# cat hist.sql
............
--
-- Table structure for table `dept`
--

DROP TABLE IF EXISTS `dept`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `dept` (
  `dept_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `dept`
--

LOCK TABLES `dept` WRITE;
/*!40000 ALTER TABLE `dept` DISABLE KEYS */;
INSERT INTO `dept` VALUES (1,'guanli'),(2,'jingji'),(3,'jidian'),(4,'jisuanji');
/*!40000 ALTER TABLE `dept` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `stu`
--

DROP TABLE IF EXISTS `stu`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `stu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `phone` char(11) DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `stu`
--

LOCK TABLES `stu` WRITE;
/*!40000 ALTER TABLE `stu` DISABLE KEYS */;
INSERT INTO `stu` VALUES (1,'zhangsan',20,'Xinxiang','15578941258',1),(2,'tom',20,'Xinxiang','13778942222',1),(3,'jack',20,'Zhengzhou','13675871454',1),(4,'john',21,'Zhengzhou','13937681111',2),(5,'mark',22,'Aanyang','13055882233',2);
/*!40000 ALTER TABLE `stu` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

...........

-- Dump completed on 2020-07-02 10:07:03

方法二:不指定–databases參數

如果不指定–databases參數,則第一個名稱默認爲數據庫名,後面的其他名稱爲表名,格式如下:

mysqldump -uuser -p db_name t_name1 t_name2 > file_name

例如:

[root@Mysql11 ~]# mysqldump -uroot -p123456 hist dept stu > /tmp/hist-bak.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# cat /tmp/hist-bak.sql
...............

--
-- Table structure for table `dept`
--

DROP TABLE IF EXISTS `dept`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `dept` (
  `dept_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `dept`
--

LOCK TABLES `dept` WRITE;
/*!40000 ALTER TABLE `dept` DISABLE KEYS */;
INSERT INTO `dept` VALUES (1,'guanli'),(2,'jingji'),(3,'jidian'),(4,'jisuanji');
/*!40000 ALTER TABLE `dept` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `stu`
--

DROP TABLE IF EXISTS `stu`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `stu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `phone` char(11) DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `stu`
--

LOCK TABLES `stu` WRITE;
/*!40000 ALTER TABLE `stu` DISABLE KEYS */;
INSERT INTO `stu` VALUES (1,'zhangsan',20,'Xinxiang','15578941258',1),(2,'tom',20,'Xinxiang','13778942222',1),(3,'jack',20,'Zhengzhou','13675871454',1),(4,'john',21,'Zhengzhou','13937681111',2),(5,'mark',22,'Aanyang','13055882233',2);
/*!40000 ALTER TABLE `stu` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

.................

-- Dump completed on 2020-07-02 10:10:24

二、備份爲帶分隔符的文本文件格式並進行恢復

1.使用參數【–tab=目錄】備份

[root@Mysql11 tmp]# mysqldump -uroot -p123456 --tab=/tmp/ hist
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 tmp]# ls
dept.sql  dept.txt  stu.sql  stu.txt

調用mysqldump時,如果帶有- -tab=dir_name選項去備份數據庫,則dir_name表示輸出文件的目錄,在這個目錄中,要備份的每個表將會產生兩個文件,一個是sql文件,包含CREATE TABLE語句;另一個是txt文件,文件中的每一行爲數據表中的一條記錄,列值與列值之間以‘tab’分隔。

dept.sql文件的內容如下:

[root@Mysql11 tmp]# cat dept.sql
-- MySQL dump 10.13  Distrib 5.7.27, for Linux (x86_64)
--
-- Host: localhost    Database: hist
-- ------------------------------------------------------
-- Server version	5.7.27

/*!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' */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `dept`
--

DROP TABLE IF EXISTS `dept`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `dept` (
  `dept_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 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 */;
/*!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-07-02 10:23:16
[root@Mysql11 tmp]# 

dept.txt文件的內容如下:

[root@Mysql11 tmp]# cat dept.txt
1	guanli
2	jingji
3	jidian
4	jisuanji

2、恢復數據

恢復數據時,首先用mysql命令處理.sql文件去還原表結構,然後處理.txt文件去載入記錄。

(1)還原表結構

[root@Mysql11 tmp]# mysql -uroot -p123456 -e "use hist;source /tmp/dept.sql;"
mysql: [Warning] Using a password on the command line interface can be insecure.

也可使用:mysql –u 用戶名 –p 數據庫名 < 表名.sql

[root@Mysql11 tmp]# mysql -uroot -p123456 hist < dept.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

(2)恢復數據

[root@Mysql11 tmp]# mysqlimport -uroot -p123456 hist /tmp/dept.txt
mysqlimport: [Warning] Using a password on the command line interface can be insecure.
hist.dept: Records: 4  Deleted: 0  Skipped: 0  Warnings: 0

使用LOAD DATA INFILE命令恢復記錄:

[root@Mysql11 tmp]# mysql -uroot -p123456 -e "use hist;load data infile '/tmp/dept.txt' into table dept;"
mysql: [Warning] Using a password on the command line interface can be insecure.

三、使用select…into outfile命令導出數據並恢復數據

1、導出數據

[root@Mysql11 tmp]# mysql -uroot -p123456 hist -e "select * from stu into outfile '/tmp/stu.txt' fields terminated by ',';"
mysql: [Warning] Using a password on the command line interface can be insecure.

[root@Mysql11 tmp]# cat /tmp/stu.txt
1,zhangsan,20,Xinxiang,15578941258,1
2,tom,20,Xinxiang,13778942222,1
3,jack,20,Zhengzhou,13675871454,1
4,john,21,Zhengzhou,13937681111,2
5,mark,22,Aanyang,13055882233,2

2、恢復數據

[root@Mysql11 tmp]# mysql -uroot -p123456 hist -e "load data infile '/tmp/stu.txt' into table stu fields terminated by ',';"
mysql: [Warning] Using a password on the command line interface can be insecure.

[root@Mysql11 tmp]# mysql -uroot -p123456 hist -e "select * from stu;" 
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+----------+------+-----------+-------------+---------+
| id | name     | age  | address   | phone       | dept_id |
+----+----------+------+-----------+-------------+---------+
|  1 | zhangsan |   20 | Xinxiang  | 15578941258 |       1 |
|  2 | tom      |   20 | Xinxiang  | 13778942222 |       1 |
|  3 | jack     |   20 | Zhengzhou | 13675871454 |       1 |
|  4 | john     |   21 | Zhengzhou | 13937681111 |       2 |
|  5 | mark     |   22 | Aanyang   | 13055882233 |       2 |
+----+----------+------+-----------+-------------+---------+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章