当前位置:   article > 正文

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

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

    MySQL8.0后续版本中主推使用MySQL Shell进行相关日常管理及维护操作,如果后续移除了mysqldump等命令后,如何进行数据库备份等相关操作呢?本文开始进行数据库备份的操作。

1.  MySQL Shell 安装

1.1  下载 

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

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

  1. [root@VM-4-14-centos ~]# uname -a
  2. 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
  3. [root@VM-4-14-centos ~]# ldd --version
  4. ldd (GNU libc) 2.17
  5. Copyright (C) 2012 Free Software Foundation, Inc.
  6. This is free software; see the source for copying conditions. There is NO
  7. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. Written by Roland McGrath and Ulrich Drepper.

632c502a137e306acac04aa8cbf6c37f.png

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

d1df50cf5a3ede88783997583ef8028c.png

1.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命令,即可得到如下结果:

18528ce415c129b0d0874ee11d9c3b4c.png

此时,完成了mysql shell部署。

2.  进行数据库备份

2.1  登录数据库

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

  1. [root@VM-4-14-centos ~]# mysqlsh -u root -p -S /data/mysql/mysql3306/tmp/mysql.sock
  2. Please provide the password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock': *********
  3. Save password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No):
  4. MySQL Shell 8.0.35
  5. Copyright (c) 2016, 2023, Oracle and/or its affiliates.
  6. Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
  7. Other names may be trademarks of their respective owners.
  8. Type '\help' or '\?' for help; '\quit' to exit.
  9. Creating a session to 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'
  10. Fetching schema names for auto-completion... Press ^C to stop.
  11. Your MySQL connection id is 10
  12. Server version: 8.0.33-25 Percona Server (GPL), Release 25, Revision 60c9e2c5
  13. No default schema selected; type \use <schema> to set one.
  14. MySQL localhost JS > \sql
  15. Switching to SQL mode... Commands end with ;
  16. Fetching global names for auto-completion... Press ^C to stop.
  17. MySQL localhost SQL > show databases;
  18. +--------------------+
  19. | Database |
  20. +--------------------+
  21. | information_schema |
  22. | mysql |
  23. | performance_schema |
  24. | sys |
  25. | testdb |
  26. | testdb1 |
  27. +--------------------+
  28. 6 rows in set (0.0008 sec)
  29. MySQL localhost SQL >

其中登录语法为:

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

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

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

a6c9117e2854037837f73450423d6c73.png

2.2 备份整个实例

 创建备份目录

mkdir -p /data/backup

登录数据库

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

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

备份整个实例

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

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

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

f0713708d2ecca940f21e6617281d0ef.png

其中的主要文件解释:

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

c6ff90ee9af7ff1f7eb1928fd0e9c147.png

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

59bf4890ada2577afdf47e867f3abdb0.png

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

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

1aca1657bfb865ed8fdf9a57a95b87da.png

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

d1b1594146f8739fea6331dcd6cba8e8.png

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

477504f4f29a5da57e45366724e72283.png

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

f13b415602968e02562f0d53b80cd3dd.png

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

eb62fe7272e8ccaea3ef62f299956061.png

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

b46729ff93b8ff8f7c3aaad38b452441.png

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

2.3 备份指定库

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

mkdir -p data/backup/backup_schemas

15771d678ac8cfe43576feef7ca02f3d.png

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

  1. # mysqlsh -u root -p -S /data/mysql/mysql3306/tmp/mysql.sock
  2. Please provide the password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock': *********
  3. Save password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No):
  4. MySQL Shell 8.0.35
  5. Copyright (c) 2016, 2023, Oracle and/or its affiliates.
  6. Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
  7. Other names may be trademarks of their respective owners.
  8. Type '\help' or '\?' for help; '\quit' to exit.
  9. Creating a session to 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'
  10. Fetching schema names for auto-completion... Press ^C to stop.
  11. Your MySQL connection id is 28
  12. Server version: 8.0.33-25 Percona Server (GPL), Release 25, Revision 60c9e2c5
  13. No default schema selected; type \use <schema> to set one.
  14. MySQL localhost JS > \sql
  15. Switching to SQL mode... Commands end with ;
  16. Fetching global names for auto-completion... Press ^C to stop.
  17. MySQL localhost SQL > show databases;
  18. +--------------------+
  19. | Database |
  20. +--------------------+
  21. | information_schema |
  22. | mysql |
  23. | performance_schema |
  24. | sys |
  25. | testdb |
  26. | testdb1 |
  27. +--------------------+
  28. 6 rows in set (0.0010 sec)

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

  1. MySQL localhost SQL > \js
  2. Switching to JavaScript mode...
  3. MySQL localhost JS > util.dumpSchemas(['testdb'],'/data/backup/backup_schemas')
  4. Acquiring global read lock
  5. Global read lock acquired
  6. Initializing - done
  7. 1 schemas will be dumped and within them 11 tables, 0 views.
  8. Gathering information - done
  9. All transactions have been started
  10. Locking instance for backup
  11. Global read lock has been released
  12. Writing global DDL files
  13. Running data dump using 4 threads.
  14. NOTE: Progress information uses estimated values and may not be accurate.
  15. Writing schema metadata - done
  16. Writing DDL - done
  17. Writing table metadata - done
  18. Starting data dump
  19. 130% (862 rows / ~662 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed
  20. Dump duration: 00:00:00s
  21. Total duration: 00:00:00s
  22. Schemas dumped: 1
  23. Tables dumped: 11
  24. Uncompressed data size: 14.06 MB
  25. Compressed data size: 4.88 MB
  26. Compression ratio: 2.9
  27. Rows written: 862
  28. Bytes written: 4.88 MB
  29. Average uncompressed throughput: 14.06 MB/s
  30. Average compressed throughput: 4.88 MB/s
  31. MySQL localhost JS >

以上则备份完成。

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

4c0aeef2d01f2134c14ce816e0ac1b77.png

2.4 备份指定表

再次先创建目录

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

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

  1. [root@VM-4-14-centos backup_tables]# mysqlsh -u root -p -S /data/mysql/mysql3306/tmp/mysql.sock
  2. Please provide the password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock': *********
  3. Save password for 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No):
  4. MySQL Shell 8.0.35
  5. Copyright (c) 2016, 2023, Oracle and/or its affiliates.
  6. Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
  7. Other names may be trademarks of their respective owners.
  8. Type '\help' or '\?' for help; '\quit' to exit.
  9. Creating a session to 'root@/data%2Fmysql%2Fmysql3306%2Ftmp%2Fmysql.sock'
  10. Fetching schema names for auto-completion... Press ^C to stop.
  11. Your MySQL connection id is 35
  12. Server version: 8.0.33-25 Percona Server (GPL), Release 25, Revision 60c9e2c5
  13. No default schema selected; type \use <schema> to set one.
  14. MySQL localhost JS > \sql
  15. Switching to SQL mode... Commands end with ;
  16. Fetching global names for auto-completion... Press ^C to stop.
  17. MySQL localhost SQL > show databases;
  18. +--------------------+
  19. | Database |
  20. +--------------------+
  21. | information_schema |
  22. | mysql |
  23. | performance_schema |
  24. | sys |
  25. | testdb |
  26. | testdb1 |
  27. +--------------------+
  28. 6 rows in set (0.0009 sec)
  29. MySQL localhost SQL > use testdb1
  30. Default schema set to `testdb1`.
  31. Fetching global names, object names from `testdb1` for auto-completion... Press ^C to stop.
  32. MySQL localhost testdb1 SQL > show tables;
  33. +-------------------+
  34. | Tables_in_testdb1 |
  35. +-------------------+
  36. | test1 |
  37. +-------------------+
  38. 1 row in set (0.0014 sec)
  39. MySQL localhost testdb1 SQL >

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

  1. MySQL localhost testdb1 JS >  util.dumpTables('testdb1',['test1'],'/data/backup/backup_tables')
  2. Acquiring global read lock
  3. Global read lock acquired
  4. Initializing - done
  5. 1 tables and 0 views will be dumped.
  6. Gathering information - done
  7. All transactions have been started
  8. Locking instance for backup
  9. Global read lock has been released
  10. Writing global DDL files
  11. Running data dump using 4 threads.
  12. NOTE: Progress information uses estimated values and may not be accurate.
  13. Writing schema metadata - done
  14. Writing DDL - done
  15. Writing table metadata - done
  16. Starting data dump
  17. 100% (8 rows / ~8 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed
  18. Dump duration: 00:00:00s
  19. Total duration: 00:00:00s
  20. Schemas dumped: 1
  21. Tables dumped: 1
  22. Uncompressed data size: 157 bytes
  23. Compressed data size: 90 bytes
  24. Compression ratio: 1.7
  25. Rows written: 8
  26. Bytes written: 90 bytes
  27. Average uncompressed throughput: 157.00 B/s
  28. Average compressed throughput: 90.00 B/s
  29. MySQL localhost testdb1 JS >

查看备份结果:

098ced79b9b3b4ba7e04cbb00f69c550.png

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

1b204552f26870b7d00bf272200683e1.png

往期精彩回顾

1.  MySQL高可用之MHA集群部署

2.  mysql8.0新增用户及加密规则修改的那些事

3.  比hive快10倍的大数据查询利器-- presto

4.  监控利器出鞘:Prometheus+Grafana监控MySQL、Redis数据库

5.  PostgreSQL主从复制--物理复制

6.  MySQL传统点位复制在线转为GTID模式复制

7.  MySQL敏感数据加密及解密

8.  MySQL数据备份及还原(一)

9.  MySQL数据备份及还原(二)

da249d222debcbe3b613693584efb272.png

扫码关注     

879fdbce7120ff28eb4747b02975ce8a.jpeg

d381e425dce1b76ad9d3b00e91476537.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/1006220?site
推荐阅读
相关标签
  

闽ICP备14008679号