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

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

服务器之家 - 数据库 - Mysql - 转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

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

一、MySQL Shell 安装 1、下载 可以在MySQL官网进行下载,地址https://dev.mysql.com/downloads/shell/。 需要根据操作系统类型、版本及glibc版本选择对应的文件下载,例如: [root@VM-4-14-centos ~]# uname -a Linux VM-4-14-centos 3.10.0-1160.99.1.el7.x86_64 #1 SM

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

一、MySQL Shell 安装

1、下载 

可以在MySQL官网进行下载,地址https://dev.mysql.com/downloads/shell/。

需要根据操作系统类型、版本及glibc版本选择对应的文件下载,例如:

[root@VM-4-14-centos ~]# uname  -a 
Linux VM-4-14-centos 3.10.0-1160.99.1.el7.x86_64 #1 SMP Wed Sep 13 14:19:20 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
[root@VM-4-14-centos ~]# ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

因此可以选择如下版本下载。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

2、部署

上传文件至目标目录后解压文件。

解压后建议配置软链接。

tar -zxvf mysql-shell-8.0.35-linux-glibc2.17-x86-64bit.tar.gz

可以看到对应的工具了。

ln -s mysql-shell-8.0.35-linux-glibc2.17-x86-64bit mysql-shell

建议再配置一下环境变量。

将“/usr/local/mysql-shell/bin"追加至/etc/profile中。

在其他位置直接运行mysqlsh命令,即可得到如下结果:

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

此时,完成了mysql shell部署。

二、进行数据库备份

1、登录数据库

使用mysqlsh登录数据库,并列出库名,例如:

[root@VM-4-14-centos ~]# 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 10
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 > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
| testdb1            |
+--------------------+
6 rows in set (0.0008 sec)
 MySQL  localhost  SQL >

其中登录语法为:

mysqlsh -u root  -p -S /data/mysql/mysql3306/tmp/mysql.sock

输入密码后即可登录成功,输入密码后会确认是否保存密码,建议选择No(默认值)。

登录成功后,可以选择\sql ,即SQL命令模式。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

2、备份整个实例

 创建备份目录。

mkdir -p /data/backup

登录数据库。

mysqlsh -u root  -p -S /data/mysql/mysql3306/tmp/mysql.sock

登录后是在js模式下,备份数据是在JS模式下进行,因此不用切换。

备份整个实例

MySQL  localhost  JS > util.dumpInstance("/data/backup")
Acquiring global read lock
Global read lock acquired
Initializing - done 
2 out of 6 schemas will be dumped and within them 12 tables, 0 views.
2 out of 5 users will be dumped.
Gathering information - done 
All transactions have been started
Locking instance for backup
Global read lock has been released
Writing global DDL files
Writing users DDL
Running data dump using 4 threads.
NOTE: Progress information uses estimated values and may not be accurate.
Writing schema metadata - done       
Writing DDL - done         
Writing table metadata - done         
Starting data dump
129% (870 rows / ~670 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed
Dump duration: 00:00:00s                                                            
Total duration: 00:00:00s                                                           
Schemas dumped: 2                                                                   
Tables dumped: 12                                                                   
Uncompressed data size: 14.06 MB                                                    
Compressed data size: 4.88 MB                                                       
Compression ratio: 2.9                                                              
Rows written: 870                                                                   
Bytes written: 4.88 MB                                                              
Average uncompressed throughput: 14.06 MB/s                                         
Average compressed throughput: 4.88 MB/s                                            
MySQL  localhost  JS >

如无异常,即完成了实例备份。可见,备份效率比较高(4线程处理)。

备份后,备份目录结果里可以查看结果如下:

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

其中的主要文件解释:

@.done.json: 该文件记录了备份结束时间,每个库下每个表的大小等信息,例如:

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

@.json:该文件记录了客户端版本,备份类型(实例、库或表等),元数据信息以及binlog信息(点位及GTID)。例如:

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

@.sql, @.post.sql:这两个文件记录注释信息. 导入数据时, 我们可以通过这两个文件自定义的SQL. 在数据导入前和数据导入后执行,本次为全量备份,因此只有版本等注释信息。

库名.json: 记录的是对应库名、表等信息。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

库名.sql: 具体的建库SQL脚本。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

库名@表名.json:记录了对于的表的元数据信息,包括库名,表名,字段名,主键等信息。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

库名@表名.sql: 具体的建表SQL脚本。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

库名@表名@@*.tsv.zst: 具体数据文件。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

库名@表名@@*.tsv.zst.idx: 具体索引文件。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

@.users.sql : 数据库用户信息,包含创建用户以及授权的SQL脚本。

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

3、备份指定库

创建备份目录:重新创建一个专用于备份指定库的目录。

mkdir -p data/backup/backup_schemas

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

使用shellsh登录数据库,并查看当前有哪些库。

# 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 28
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 > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
| testdb1            |
+--------------------+
6 rows in set (0.0010 sec)

备份指定的库(schema),如果多个库,则用逗号分隔。

MySQL  localhost  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost  JS > util.dumpSchemas(['testdb'],'/data/backup/backup_schemas')
Acquiring global read lock
Global read lock acquired
Initializing - done 
1 schemas will be dumped and within them 11 tables, 0 views.
Gathering information - done 
All transactions have been started
Locking instance for backup
Global read lock has been released
Writing global DDL files
Running data dump using 4 threads.
NOTE: Progress information uses estimated values and may not be accurate.
Writing schema metadata - done       
Writing DDL - done         
Writing table metadata - done         
Starting data dump
130% (862 rows / ~662 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed
Dump duration: 00:00:00s                                                            
Total duration: 00:00:00s                                                           
Schemas dumped: 1                                                                   
Tables dumped: 11                                                                   
Uncompressed data size: 14.06 MB                                                    
Compressed data size: 4.88 MB                                                       
Compression ratio: 2.9                                                              
Rows written: 862                                                                   
Bytes written: 4.88 MB                                                              
Average uncompressed throughput: 14.06 MB/s                                         
Average compressed throughput: 4.88 MB/s                                            
 MySQL  localhost  JS >

以上则备份完成。

到对于目录下查看备份结果如下:

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

4、备份指定表

再次先创建目录。

[root@VM-4-14-centos ~]# mkdir -p /data/backup/backup_tables
[root@VM-4-14-centos ~]# cd /data/backup/backup_tables
[root@VM-4-14-centos backup_tables]#

登录数据库,并查看库及表名。

[root@VM-4-14-centos backup_tables]# 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 35
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 > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
| testdb1            |
+--------------------+
6 rows in set (0.0009 sec)
 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.0014 sec)
 MySQL  localhost  testdb1  SQL >

进行指定表的备份,如果多个表,则表名用逗号分隔。

MySQL  localhost  testdb1  JS >  util.dumpTables('testdb1',['test1'],'/data/backup/backup_tables')
Acquiring global read lock
Global read lock acquired
Initializing - done 
1 tables and 0 views will be dumped.
Gathering information - done 
All transactions have been started
Locking instance for backup
Global read lock has been released
Writing global DDL files
Running data dump using 4 threads.
NOTE: Progress information uses estimated values and may not be accurate.
Writing schema metadata - done       
Writing DDL - done       
Writing table metadata - done       
Starting data dump
100% (8 rows / ~8 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed
Dump duration: 00:00:00s                                                        
Total duration: 00:00:00s                                                       
Schemas dumped: 1                                                               
Tables dumped: 1                                                                
Uncompressed data size: 157 bytes                                               
Compressed data size: 90 bytes                                                  
Compression ratio: 1.7                                                          
Rows written: 8                                                                 
Bytes written: 90 bytes                                                         
Average uncompressed throughput: 157.00 B/s                                     
Average compressed throughput: 90.00 B/s                                        
 MySQL  localhost  testdb1  JS >

查看备份结果:

转战MySQL Shell!数据库备份新姿势,轻松搞定备份操作!

至此,备份数据库实例、库、表的命令已简单演示完毕,实际生产使用过程基本会用脚本实现,大家可以自行编写。

延伸 · 阅读

精彩推荐
  • Mysql阿里云数据库是基于MySQL吗?都有什么版本呢?

    阿里云数据库是基于MySQL吗?都有什么版本呢?

    阿里云数据库是基于 MySQL 吗?都有什么版本呢? 阿里云数据库是基于MySQL吗?阿里云云数据库有多个版本可选,如云数据库RDS MySQL版、云数据库RDS SQL Server版及...

    赵一八笔记7862020-10-20
  • MysqlMySQL truncate table语句的使用

    MySQL truncate table语句的使用

    这篇文章主要介绍了MySQL truncate table语句的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    丁海龙9342021-04-25
  • Mysqlwindows下安装mysql8.0.18的教程(社区版)

    windows下安装mysql8.0.18的教程(社区版)

    本文章简单介绍一下mysql在windows下的安装方式,主要介绍了mysql社区版8.0.18版本,本文给大家介绍的非常详细,需要的朋友参考下吧...

    qq_199090999215782020-12-30
  • Mysqlmysql group_concat()函数用法总结

    mysql group_concat()函数用法总结

    这篇文章主要介绍了mysql group_concat()函数用法,结合实例形式较为详细的group_concat()函数的功能、使用方法与相关注意事项,需要的朋友可以参考下...

    wangtaotao10132020-06-15
  • Mysqlmysql数据库备份设置延时备份方法(mysql主从配置)

    mysql数据库备份设置延时备份方法(mysql主从配置)

    这篇文章主要介绍了mysql数据库延时备份的方法,也就是mysql主从服务器备份,可以同步到多个服务器,最后我们提供了一个备份脚本,大家参考使用吧 ...

    MYSQL教程网5002020-01-16
  • MysqlMysql隔离性之Read View的用法说明

    Mysql隔离性之Read View的用法说明

    这篇文章主要介绍了Mysql隔离性之Read View的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Drogon Brother5362021-04-28
  • Mysqlmysql多表联合查询返回一张表的内容实现代码

    mysql多表联合查询返回一张表的内容实现代码

    在使用mysql多表联合查询时怎样可以做到只返回返回一张表的内容,本文将详细介绍,需要了解的朋友可以参考下 ...

    MYSQL教程网3632019-12-13
  • Mysqldeepin 2014系统下安装mysql数据库的方法步骤

    deepin 2014系统下安装mysql数据库的方法步骤

    这篇文章主要给大家介绍了在deepin 2014系统下安装mysql数据库的方法步骤,文中通过图文介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下...

    big_big_snail4522020-07-24