不同MySQL服务的表以及库的数据迁移(/备份)

目标:

将本地主机上username=root,password=root,port=3307的MySQL服务中migration_one数据库的table_11数据表导出到本地的D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11.bak

注意:目前D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL该文件夹未存在table_11.bak这个文件。

先看看数据表有什么数据:

mysql 复制代码
SELECT * FROM migration_one.`table_11`;

结果:

id      name    
------  --------
12321   hehe    

看来就只有一行

一、导出数据:

1、导出备份数据之.bak文件:

用我的mysql8的mysqldump工具,但是目前是需要去找到我的mysql8在哪里:

mysql 复制代码
 SHOW VARIABLES LIKE "character_sets_dir";

得到结果:

Variable_name       Value                                                       
------------------  ------------------------------------------------------------
character_sets_dir  D:\start_java\lwsmysql\mysql-8.0.17-winx64\share\charsets\  

于是来到D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin文件夹可以看到一个mysqldump.exe在里面

于是就可以执行命令来备份文件了,不过需要将原来的table_11.bak删掉。

管理员打开终端执行:

shell 复制代码
D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -h127.0.0.1 -P3307 -uroot -proot migration_one table_11 > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11.bak

得到执行结果:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -h127.0.0.1 -P3307 -uroot -proot migration_one table_11 > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11.bak
mysqldump: [Warning] Using a password on the command line interface can be insecure.

再打开table_11.bak文件:

mysql 复制代码
-- MySQL dump 10.13  Distrib 8.0.17, for Win64 (x86_64)
--
-- Host: 127.0.0.1    Database: migration_one
-- ------------------------------------------------------
-- Server version	8.0.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 */;
/*!50503 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 `table_11`
--

DROP TABLE IF EXISTS `table_11`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `table_11` (
  `id` varchar(200) NOT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `table_11`
--

LOCK TABLES `table_11` WRITE;
/*!40000 ALTER TABLE `table_11` DISABLE KEYS */;
INSERT INTO `table_11` VALUES ('12321','hehe');
/*!40000 ALTER TABLE `table_11` 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 2023-11-04 14:53:46

成功乐!这样才算正常的备份文件嘛。

2、导出数据和表结构------将特定数据库特定表中的数据和表格结构和数据全部返回

mysql 复制代码
D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -u  root -h 127.0.0.1 -proot  -P3307 migration_one table_11 > table_11_t_d.sql

管理员打开cmd进入D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL文件夹里面执行上面的命令

执行结果:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -u  root -h 127.0.0.1 -proot  -P3307 migration_one table_11 > table_11_t_d.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

发现D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL文件夹多了一个table_11_t_d.sql文件,里面是:

mysql 复制代码
-- MySQL dump 10.13  Distrib 8.0.17, for Win64 (x86_64)
--
-- Host: 127.0.0.1    Database: migration_one
-- ------------------------------------------------------
-- Server version	8.0.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 */;
/*!50503 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 `table_11`
--

DROP TABLE IF EXISTS `table_11`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `table_11` (
  `id` varchar(200) NOT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `table_11`
--

LOCK TABLES `table_11` WRITE;
/*!40000 ALTER TABLE `table_11` DISABLE KEYS */;
INSERT INTO `table_11` VALUES ('12321','hehe');
/*!40000 ALTER TABLE `table_11` 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 2023-11-04 14:44:55

成功了

3、导出表结构却不导出表数据------只返回特定数据库特定表格的表格结构,不返回数据,添加"-d"命令参数

shell 复制代码
D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -u  root -h 127.0.0.1 -proot  -P3307 -d migration_one table_11 > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_t.sql

执行结果:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -u  root -h 127.0.0.1 -proot  -P3307 -d migration_one table_11 > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_t.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

查看table_11_t.sql

mysql 复制代码
-- MySQL dump 10.13  Distrib 8.0.17, for Win64 (x86_64)
--
-- Host: 127.0.0.1    Database: migration_one
-- ------------------------------------------------------
-- Server version	8.0.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 */;
/*!50503 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 `table_11`
--

DROP TABLE IF EXISTS `table_11`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `table_11` (
  `id` varchar(200) NOT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`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 2023-11-04 15:04:43

3、导出数据却不导出表结构------只返回特定数据库中特定表格的数据,不返回表格结构,添加"-t"命令参数

shell 复制代码
D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -u  root -h 127.0.0.1 -proot  -P3307 -t migration_one table_11 > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_d.sql

执行结果:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe -u  root -h 127.0.0.1 -proot  -P3307 -t migration_one table_11 > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_d.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

查看table_11_d.sql

mysql 复制代码
    -- MySQL dump 10.13  Distrib 8.0.17, for Win64 (x86_64)
    --
    -- Host: 127.0.0.1    Database: migration_one
    -- ------------------------------------------------------
    -- Server version	8.0.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 */;
    /*!50503 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 */;

    --
    -- Dumping data for table `table_11`
    --

    LOCK TABLES `table_11` WRITE;
    /*!40000 ALTER TABLE `table_11` DISABLE KEYS */;
    INSERT INTO `table_11` VALUES ('12321','hehe');
    /*!40000 ALTER TABLE `table_11` 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 2023-11-04 15:12:00

确实是只插入数据。

4、导出特定数据库的所有表格的表结构及其数据,添加"--databases "命令参数

shell 复制代码
D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe  -u  root -h 127.0.0.1 -proot -P3307 --databases migration_one  > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\migration_one_database.sql

执行结果:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-8.0.17-winx64\bin\mysqldump.exe  -u  root -h 127.0.0.1 -proot -P3307 --databases migration_one  > D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\migration_one_database.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

查看migration_one_database.sql

mysql 复制代码
-- MySQL dump 10.13  Distrib 8.0.17, for Win64 (x86_64)
--
-- Host: 127.0.0.1    Database: migration_one
-- ------------------------------------------------------
-- Server version	8.0.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 */;
/*!50503 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 */;

--
-- Current Database: `migration_one`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `migration_one` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */;

USE `migration_one`;

--
-- Table structure for table `table_11`
--

DROP TABLE IF EXISTS `table_11`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `table_11` (
  `id` varchar(200) NOT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `table_11`
--

LOCK TABLES `table_11` WRITE;
/*!40000 ALTER TABLE `table_11` DISABLE KEYS */;
INSERT INTO `table_11` VALUES ('12321','hehe');
/*!40000 ALTER TABLE `table_11` 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 2023-11-04 15:18:42

5、mysql导出具特定条件的表数据(--where="文本"

mysql根据条件导出表数据(--where="文本")-CSDN博客

二、导入(恢复)数据

导入(恢复)数据就用我在本地机MySQL5.7.19中名为demo这个库来实验吧。

上面导出(备份)数据是在mysql8进行的,但是本地不满意安装第二个MySQL8所以就用之前的MySQL5.7.19来做数据的导入。

先启动MySQL5.7.19:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>net start MYSQL
MySQL 服务正在启动 .
MySQL 服务已经启动成功。

输入登录命令,我的就用sqlyog连接工具来理解吧,账号密码也是要输入的:

mysql 复制代码
SELECT VERSION();

结果:

version()  
-----------
5.7.19     

的确是5.7.19版本

1、恢复整个数据库

先查看当前是否存在库migration_one

mysql 复制代码
SELECT 1 FROM information_schema.schemata  WHERE schema_name='migration_one';

不存在那就恢复,恢复前先找到mysql-5.7.19的mysql.exe:

mysql 复制代码
 SHOW VARIABLES LIKE "character_sets_dir";

得到结果:

Variable_name       Value                                                       
------------------  ------------------------------------------------------------
character_sets_dir  D:\start_java\lwsmysql\mysql-5.7.19-winx64\share\charsets\  

于是mysql-5.7.19的mysql.exe在D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe

管理员打开终端输入:

shell 复制代码
D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\migration_one_database.sql

执行结果:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\migration_one_database.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

在数据库连接工具sqlyog执行查询可以验证确实是恢复成功了:

mysql 复制代码
 SELECT 1 FROM information_schema.schemata  WHERE schema_name='migration_one';

得到:

-- 1个结果:

     1  
--------
       1

-- 3条信息:
返回了 1 行

执行耗时   : 0 sec
传送时间   : 0 sec
总耗时      : 0 sec
mysql 复制代码
 SELECT * FROM migration_one.table_11;
-- 1个结果:

id      name    
------  --------
12321   hehe    

-- 3条信息:
返回了 1 行

执行耗时   : 0 sec
传送时间   : 0 sec
总耗时      : 0 sec

nice看来是在mysql-5.7.19恢复成功了。

2、恢复数据表结果及其数据:

这次我们要将D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_t_d.sql执行以在mysql-5.7.19的demo这个库中恢复migration_one.table_11这个表及其数据。

恢复前先看看demo是否存在table_11表:

mysql 复制代码
SELECT * FROM information_schema.tables WHERE table_schema='demo';

结果:

TABLE_CATALOG  TABLE_SCHEMA  TABLE_NAME         TABLE_TYPE  ENGINE  VERSION  ROW_FORMAT  TABLE_ROWS  AVG_ROW_LENGTH  DATA_LENGTH  MAX_DATA_LENGTH  INDEX_LENGTH  DATA_FREE  AUTO_INCREMENT  CREATE_TIME          UPDATE_TIME  CHECK_TIME  TABLE_COLLATION  CHECKSUM  CREATE_OPTIONS  TABLE_COMMENT  
-------------  ------------  -----------------  ----------  ------  -------  ----------  ----------  --------------  -----------  ---------------  ------------  ---------  --------------  -------------------  -----------  ----------  ---------------  --------  --------------  ---------------
def            demo          persistent_logins  BASE TABLE  InnoDB       10  Dynamic              0               0        16384                0             0          0          (NULL)  2022-11-17 10:28:14  (NULL)       (NULL)      utf8_general_ci    (NULL)                                 
def            demo          users              BASE TABLE  InnoDB       10  Dynamic              2            8192        16384                0             0          0               3  2022-11-15 19:12:49  (NULL)       (NULL)      utf8_general_ci    (NULL)                                 

发现table_11不存在,于是可以安心在demo这个数据库恢复table_11表了:

管理员打开终端执行

shell 复制代码
D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 demo < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_t_d.sql

执行成功:

shell 复制代码
D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 demo < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_t_d.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

验证table_11_t_d表及其数据是否已在demo恢复:

mysql 复制代码
SELECT * FROM information_schema.tables WHERE table_schema='demo';
TABLE_CATALOG  TABLE_SCHEMA  TABLE_NAME         TABLE_TYPE  ENGINE  VERSION  ROW_FORMAT  TABLE_ROWS  AVG_ROW_LENGTH  DATA_LENGTH  MAX_DATA_LENGTH  INDEX_LENGTH  DATA_FREE  AUTO_INCREMENT  CREATE_TIME          UPDATE_TIME          CHECK_TIME  TABLE_COLLATION  CHECKSUM  CREATE_OPTIONS  TABLE_COMMENT  
-------------  ------------  -----------------  ----------  ------  -------  ----------  ----------  --------------  -----------  ---------------  ------------  ---------  --------------  -------------------  -------------------  ----------  ---------------  --------  --------------  ---------------
def            demo          persistent_logins  BASE TABLE  InnoDB       10  Dynamic              0               0        16384                0             0          0          (NULL)  2022-11-17 10:28:14  (NULL)               (NULL)      utf8_general_ci    (NULL)                                 
def            demo          table_11           BASE TABLE  InnoDB       10  Dynamic              0               0        16384                0             0          0          (NULL)  2023-11-04 16:24:28  2023-11-04 16:24:28  (NULL)      utf8_general_ci    (NULL)                                 
def            demo          users              BASE TABLE  InnoDB       10  Dynamic              2            8192        16384                0             0          0               3  2022-11-15 19:12:49  (NULL)               (NULL)      utf8_general_ci    (NULL)                                 

再验证:

mysql 复制代码
SELECT * FROM demo.table_11;

结果:

id      name    
------  --------
12321   hehe    

数据恢复成功!

这里是恢复数据表结构及其数据,看一眼table_11_t_d.sql即可发现里面的逻辑就是【判断表是否存在,存在即删除该表->创建该表->加锁->插入所有数据->释放锁】

很好懂的,看sql见其应用场景。

3、仅仅恢复表数据

这里便是之前导出的table_11_d.sql的应用场景了,看里面的逻辑:【加锁->插入所有数据->释放锁】就知道首先得存在一张名为table_11的表。

先干掉demo.table_11的数据:

mysql 复制代码
TRUNCATE demo.table_11;
SELECT * FROM demo.table_11;

接下来管理员打开终端执行:

shell 复制代码
D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 demo < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_d.sql

执行结果:

D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 demo < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_d.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

再用MySQL命令执行:

mysql 复制代码
SELECT * FROM demo.table_11;

即可看到表数据恢复。

4、仅仅恢复表结构

这里便是之前导出的table_11_t.sql的应用场景了,看里面的逻辑:【判断表是否存在,存在即删除该表->创建该表】

就知道首先删除demo库里存在名为table_11的表:

mysql 复制代码
DROP TABLE demo.table_11;
SELECT 1 FROM information_schema.tables WHERE table_schema='demo' AND table_name ='table_11';

SELECT * FROM information_schema.tables WHERE table_schema='demo';

通过结果得知demo.table_11已祭天。

接下来管理员打开终端执行:

shell 复制代码
D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 demo < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_t.sql

执行结果:

D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL>D:\start_java\lwsmysql\mysql-5.7.19-winx64\bin\mysql.exe -u root -h 127.0.0.1 -pxinqi -P3306 demo < D:\start_java\XinQiUtilsOrDemo\testMigrationMySQL\table_11_t.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

通过以下语句得知验证恢复表结构成功:

mysql 复制代码
SELECT 1 FROM information_schema.tables WHERE table_schema='demo' AND table_name ='table_11';

SELECT * FROM information_schema.tables WHERE table_schema='demo';

SELECT * FROM demo.table_11;

nice!

参考

MySql数据库备份与恢复------使用mysqldump 导入与导出方法总结_mysql备份-CSDN博客

MySQL数据库(表)的导入导出(备份和还原) mysql 根据一张表数据更新另一张表-腾讯云开发者社区-腾讯云 (tencent.com)

MySQL判断库、表、列是否存在 - 钟小嘿 - 博客园 (cnblogs.com)

其他可能有用或可以学习得参考:

mysql mysqldump 命令导出指定表的数据 - Ruthless - 博客园 (cnblogs.com)

相关推荐
The_Ticker17 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
Elastic 中国社区官方博客23 分钟前
Elasticsearch 开放推理 API 增加了对 IBM watsonx.ai Slate 嵌入模型的支持
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
企鹅侠客27 分钟前
ETCD调优
数据库·etcd
Json_1817901448033 分钟前
电商拍立淘按图搜索API接口系列,文档说明参考
前端·数据库
煎饼小狗1 小时前
Redis五大基本类型——Zset有序集合命令详解(命令用法详解+思维导图详解)
数据库·redis·缓存
永乐春秋1 小时前
WEB-通用漏洞&SQL注入&CTF&二次&堆叠&DNS带外
数据库·sql
打鱼又晒网2 小时前
【MySQL】数据库精细化讲解:内置函数知识穿透与深度学习解析
数据库·mysql
大白要努力!2 小时前
android 使用SQLiteOpenHelper 如何优化数据库的性能
android·数据库·oracle
tatasix2 小时前
MySQL UPDATE语句执行链路解析
数据库·mysql
南城花随雪。3 小时前
硬盘(HDD)与固态硬盘(SSD)详细解读
数据库