MySQL database backup and recovery (3)-restore data

MySQL database backup and recovery (3)-restore data

Use MySQL's own backup tool mysqldump to back up the data, and then restore the data.

1. Back up data in SQL format and restore

1. Back up all databases

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

2. Back up multiple specified databases

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

Restore wgx and hist databases

Method 1: Log in to mysql for recovery

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)

Method 2: Restore without logging in to 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. Back up a specified database

(1) Specify the -databases parameter

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

The backup file generated by this command contains the create database and use commands, as follows:

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

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

USE `wgx`;

(2) You can also not specify the -databases parameter

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

When backing up a database, you can omit the -databases parameter, but there is no CREATE DATABASE and USE statement in the backup file generated after omitting this parameter, then when restoring the backup file, you must specify a default database name.

You can use a database name that is different from the original database name.

[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. Restore all data in a database

Method 1: If there is the –databases parameter when backing up the database

[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                |
+--------------------+

Method 2: If there is no –databases parameter when backing up the database

[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

Note: If no database is selected, an error will occur: ERROR 1046 (3D000) at line 22: No database selected

Therefore, you must first use the use command to select the database to be restored:

[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             |

As you can see, the database name was modified while restoring the data.

5. Recover several tables in a database

Method 1: Specify the –databases and –tables parameters

[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

Method 2: Do not specify the –databases parameter

If you do not specify the -databases parameter, the first name defaults to the database name, and the rest of the names are table names, in the following format:

mysqldump -uuser -p db_name t_name1 t_name2 > file_name

E.g:

[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. The backup is in a delimited text file format and restored

1. Use the parameter [–tab=directory] to backup

[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

When calling mysqldump, if the option -tab=dir_name is used to back up the database, then dir_name indicates the directory of the output file. In this directory, each table to be backed up will generate two files, one is a sql file, including CREATE TABLE statement; the other is a txt file, each row in the file is a record in the data table, and the column value and the column value are separated by'tab'.

The contents of the dept.sql file are as follows:

[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]# 

The contents of the dept.txt file are as follows:

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

2. Restore data

When recovering data, first use the mysql command to process the .sql file to restore the table structure, and then process the .txt file to load the record.

(1) Restore the table structure

[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.

You can also use: mysql -u user name -p database name <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) Restore data

[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

Use the LOAD DATA INFILE command to restore the record:

[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.

Three, use select...into outfile command to export data and restore data

1. Export data

[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. Restore data

[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 |
+----+----------+------+-----------+-------------+---------+

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107080410