服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - 数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

2023-12-20 01:00未知服务器之家 Mysql

上篇文章( 转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作! )简单介绍了使用MySQL Shell进行数据库备份,本文基于上文的备份进行数据恢复演示操作。 一、恢复单表 因为上次备份的表是testdb1.test1表,如果恢复到当前库,

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

上篇文章(转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!)简单介绍了使用MySQL Shell进行数据库备份,本文基于上文的备份进行数据恢复演示操作。

一、恢复单表

因为上次备份的表是testdb1.test1表,如果恢复到当前库,则可以先删除该库中的表,再恢复。

1、先删除库里的表

# mysqlsh -u root  -p -S /data/mysql/mysql3306/tmp/mysql.sock
Please provide the password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock': *********
Save password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No): 
MySQL Shell 8.0.35
Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 83
Server version: 8.0.33-25 Percona Server (GPL), Release 25, Revision 60c9e2c5
No default schema selected; type \use <schema> to set one.
 MySQL  localhost  JS > \sql
Switching to SQL mode... Commands end with ;
Fetching global names for auto-completion... Press ^C to stop.
 MySQL  localhost  SQL > use testdb1;
Default schema set to `testdb1`.
Fetching global names, object names from `testdb1` for auto-completion... Press ^C to stop.
 MySQL  localhost  testdb1  SQL > show tables;
+-------------------+
| Tables_in_testdb1 |
+-------------------+
| test1             |
+-------------------+
1 row in set (0.0010 sec)
 MySQL  localhost  testdb1  SQL > drop table test1;
Query OK, 0 rows affected (0.0518 sec)
 MySQL  localhost  testdb1  SQL >

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

2、修改参数

进行数据恢复时local_infile参数需要修改为on,因此需先调整参数,否则将会报错,例如:

MySQL  localhost  testdb1  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost  testdb1  JS > util.loadDump('/data/backup/backup_tables');
ERROR: The 'local_infile' global system variable must be set to ON in the target server, after the server is verified to be trusted.
Util.loadDump: local_infile disabled in server (MYSQLSH 53025)
 MySQL  localhost  testdb1  JS > \sql
Switching to SQL mode... Commands end with ;
 MySQL  localhost  testdb1  SQL > set global local_infile=on;
Query OK, 0 rows affected (0.0002 sec)

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

3、进行单表恢复

上面已经删除了原库里的表,现在通过备份进行恢复。

MySQL  localhost  testdb1  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost  testdb1  JS >  util.loadDump('/data/backup/backup_tables');
Loading DDL and Data from '/data/backup/backup_tables' using 4 threads.
Opening dump...
Target is MySQL 8.0.33-25. Dump was produced from MySQL 8.0.33-25
Scanning metadata - done       
Checking for pre-existing objects...
Executing common preamble SQL
Executing DDL - done       
Executing view DDL - done       
Starting data load
Executing common postamble SQL                       
100% (157 bytes / 157 bytes), 0.00 B/s, 1 / 1 tables done
Recreating indexes - done       
1 chunks (8 rows, 157 bytes) for 1 tables in 1 schemas were loaded in 0 sec (avg throughput 157.00 B/s)
0 warnings were reported during the load.                
 MySQL  localhost  testdb1  JS >

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

查看恢复结果:表及数据已恢复。

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

恢复过程中,对应的备份路径下会生成一个load-progress.*.json文件,该文件记录了恢复进度及结果,以便于断点续处理,文件存储的具体内容如下:

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

注:如果需多次进行相同操作时,注意修改改文件内容或删除该文件,或者自定义一个文件,例如:

util.loadDump("PrefixPARURL", progressFile: "progress.json"})

4、恢复至其他库

很多实际情况下的数据恢复是为了将备份中的一部分数据恢复至目标表,或进行数据对比,因此不能将已存在的表删除。那么建议创建一个临时恢复用的库或在其他实例上创建新库进行恢复。那么,恢复至其他库(库名不一样),该如何操作呢?具体操作如下:

先创建一个空库:

MySQL  localhost  testdb1  SQL > create database rec;
Query OK, 1 row affected (0.0220 sec)
 MySQL  localhost  testdb1  SQL > use rec;
Default schema set to `rec`.
Fetching global names, object names from `rec` for auto-completion... Press ^C to stop.
 MySQL  localhost  rec  SQL > show tables;
Empty set (0.0011 sec)
 MySQL  localhost  rec  SQL >

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

恢复rec库并查看结果:

MySQL  localhost  rec  JS > util.loadDump('/data/backup/backup_tables',{"schema":"rec"});
Loading DDL and Data from '/data/backup/backup_tables' using 4 threads.
Opening dump...
Target is MySQL 8.0.33-25. Dump was produced from MySQL 8.0.33-25
NOTE: Load progress file detected. Load will be resumed from where it was left, assuming no external updates were made.
You may enable the 'resetProgress' option to discard progress for this MySQL instance and force it to be completely reloaded.
Scanning metadata - done       
Executing common preamble SQL
Executing DDL - done       
Executing view DDL - done       
Starting data load
100% (157 bytes / 157 bytes), 0.00 B/s, 0 / 1 tables done
Recreating indexes - done       
Executing common postamble SQL                           
1 chunks (8 rows, 314 bytes) for 1 tables in 1 schemas were loaded in 0 sec (avg throughput 157.00 B/s)
0 warnings were reported during the load.                
 MySQL  localhost  rec  JS > \sql
Switching to SQL mode... Commands end with ;
 MySQL  localhost  rec  SQL > use rec;
Default schema set to `rec`.
Fetching global names, object names from `rec` for auto-completion... Press ^C to stop.
 MySQL  localhost  rec  SQL > show tables;
+---------------+
| Tables_in_rec |
+---------------+
| test1         |
+---------------+
1 row in set (0.0012 sec)
 MySQL  localhost  rec  SQL > select  count(*) from test1;
+----------+
| count(*) |
+----------+
|        8 |
+----------+
1 row in set (0.0033 sec)
 MySQL  localhost  rec  SQL >

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

数据已恢复,且数据条数一致。

此时的备份文件目录里的load-progress文件内容会增加一批:

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

二、恢复单个schema

因为恢复至和备份库名一致的库需删除原库,因此就不演示该操作,而采用恢复至其他库的方式演示。

1、创建一个新库

MySQL  localhost  rec  SQL > use rec1;
Default schema set to `rec1`.
Fetching global names, object names from `rec1` for auto-completion... Press ^C to stop.
 MySQL  localhost  rec1  SQL > show tables;
Empty set (0.0011 sec)
 MySQL  localhost  rec1  SQL >

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

2、进行数据恢复

从上期备份的schema中恢复至rec1库,添加schema参数即可。

具体步骤如下:

MySQL  localhost  rec1  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost  rec1  JS > util.loadDump('/data/backup/backup_schemas',{"schema":"rec1"})
Loading DDL and Data from '/data/backup/backup_schemas' using 4 threads.
Opening dump...
Target is MySQL 8.0.33-25. Dump was produced from MySQL 8.0.33-25
Scanning metadata - done       
Checking for pre-existing objects...
Executing common preamble SQL
Executing DDL - done       
Executing view DDL - done       
Starting data load
1 thds loading | 100% (14.06 MB / 14.06 MB), 27.90 MB/s, 11 / 11 tables done
Executing common postamble SQL                                              
Recreating indexes - done       
12 chunks (862 rows, 14.06 MB) for 11 tables in 1 schemas were loaded in 1 sec (avg throughput 14.06 MB/s)
0 warnings were reported during the load.                                   
 MySQL  localhost  rec1  JS >

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

3、查看恢复结果

可见,表已恢复至rec1库中。

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

执行恢复操作时,对应的备份目录也生成了load-progress.*.json文件,内容如下:

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

三、从整库备份中恢复

1、恢复整个实例的库

恢复整库时,如果目标库已经存在,则需要先删除(同恢复单表或单库类似),之后才可以进行恢复,否则会报库及表已经存在的错误。本文为了演示,先删除之前的库,生产环境千万别删库!!!

MySQL  localhost  rec1  JS > \sql
Switching to SQL mode... Commands end with ;
 MySQL  localhost  rec1  SQL > drop database testdb;
Query OK, 11 rows affected (0.2389 sec)
 MySQL  localhost  rec1  SQL > drop database testdb1;
Query OK, 1 row affected (0.0276 sec)
 MySQL  localhost  rec1  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost  rec1  JS > util.loadDump('/data/backup');
Loading DDL and Data from '/data/backup' using 4 threads.
Opening dump...
Target is MySQL 8.0.33-25. Dump was produced from MySQL 8.0.33-25
NOTE: Load progress file detected. Load will be resumed from where it was left, assuming no external updates were made.
You may enable the 'resetProgress' option to discard progress for this MySQL instance and force it to be completely reloaded.
Scanning metadata - done       
Executing common preamble SQL
Executing DDL - done       
Executing view DDL - done       
Starting data load
1 thds loading | 100% (14.06 MB / 14.06 MB), 8.39 MB/s, 12 / 12 tables done
Executing common postamble SQL                                             
Recreating indexes - done       
13 chunks (870 rows, 14.06 MB) for 12 tables in 2 schemas were loaded in 1 sec (avg throughput 14.06 MB/s)
0 warnings were reported during the load.                                  
 MySQL  localhost  rec1  JS > \sql
Switching to SQL mode... Commands end with ;
 MySQL  localhost  rec1  SQL > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| rec                |
| rec1               |
| sys                |
| testdb             |
| testdb1            |
+--------------------+
8 rows in set (0.0009 sec)
 MySQL  localhost  rec1  SQL >

数据恢复新姿势:使用MySQL Shell进行更高效灵活的数据恢复

可见,以上被删除的库已恢复。

四、结语

MySQL Shell的数据恢复操作有很多可选参数便于灵活操作,大家可以自己实操实验一下。也可以通过修改并发参数来对比其他逻辑备份工具的效率。

延伸 · 阅读

精彩推荐
  • MysqlERROR: Error in Log_event::read_log_event()

    ERROR: Error in Log_event::read_log_event()

    ERROR: Error in Log_event::read_log_event(): read error, data_len: 438, event_type: 2 ...

    MYSQL教程网6412020-03-13
  • MysqlMySQL锁的知识点总结

    MySQL锁的知识点总结

    在本篇文章里小编给大家整理了关于MySQL锁的知识点总结以及实例内容,需要的朋友们学习下。...

    别人放弃我坚持吖4362020-12-14
  • Mysql浅谈mysql 树形结构表设计与优化

    浅谈mysql 树形结构表设计与优化

    在诸多的管理类,办公类等系统中,树形结构展示随处可见,本文主要介绍了mysql 树形结构表设计与优化,具有一定的参考价值,感兴趣的小伙伴们可以参...

    小码农叔叔5242021-11-16
  • Mysql解决MySQl查询不区分大小写的方法讲解

    解决MySQl查询不区分大小写的方法讲解

    今天小编就为大家分享一篇关于解决MySQl查询不区分大小写的方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起...

    Veir_dev5592019-06-25
  • MysqlMySQL 数据备份与还原的示例代码

    MySQL 数据备份与还原的示例代码

    这篇文章主要介绍了MySQL 数据备份与还原的相关知识,本文通过示例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...

    逆心2972019-06-23
  • MysqlMySQL数据库varchar的限制规则说明

    MySQL数据库varchar的限制规则说明

    本文我们主要介绍了MySQL数据库中varchar的限制规则,并以一个实际的例子对限制规则进行了说明,希望能够对您有所帮助。 ...

    mysql技术网4192019-11-23
  • Mysqlmysql 不能插入中文问题

    mysql 不能插入中文问题

    当向mysql5.5插入中文时,会出现类似错误 ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column ...

    MYSQL教程网5722019-11-25
  • Mysql详解MySQL中的分组查询与连接查询语句

    详解MySQL中的分组查询与连接查询语句

    这篇文章主要介绍了MySQL中的分组查询与连接查询语句,同时还介绍了一些统计函数的用法,需要的朋友可以参考下 ...

    GALAXY_ZMY5442020-06-03