MySQLデータベースのバックアップとリカバリ(3)-データの復元
MySQL独自のバックアップツールmysqldumpを使用してデータをバックアップし、データを復元します。
1.データを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データベースを復元する
方法1: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)
方法2: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.データベース内のすべてのデータを復元します
方法1:データベースのバックアップ時に–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 |
+--------------------+
方法2:データベースのバックアップ時に–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
注:データベースが選択されていない場合、エラーが発生します:22行目のERROR 1046(3D000):データベースが選択されていません
したがって、最初に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.データベース内のいくつかのテーブルを回復する
方法1:–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
方法2:–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
2.バックアップは区切られたテキストファイル形式で復元されます
1.パラメータ[–tab = directory]を使用してバックアップします
[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は出力ファイルのディレクトリを示します。このディレクトリでは、バックアップされる各テーブルが2つのファイルを生成します。1つはCREATEを含むsqlファイルです。 TABLEステートメント。もう1つはtxtファイルで、ファイルの各行はデータテーブルのレコードで、列の値と列の値は「タブ」で区切られています。
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データベース名<table name.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.
3、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 |
+----+----------+------+-----------+-------------+---------+