轉(zhuǎn)戰(zhàn)MySQL Shell!數(shù)據(jù)庫備份新姿勢,輕松搞定備份操作!
一、MySQL Shell 安裝
1、下載
可以在MySQL官網(wǎng)進行下載,地址https://dev.mysql.com/downloads/shell/。
需要根據(jù)操作系統(tǒng)類型、版本及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.
因此可以選擇如下版本下載。
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
建議再配置一下環(huán)境變量。
將“/usr/local/mysql-shell/bin"追加至/etc/profile中。
在其他位置直接運行mysqlsh命令,即可得到如下結(jié)果:
此時,完成了mysql shell部署。
二、進行數(shù)據(jù)庫備份
1、登錄數(shù)據(jù)庫
使用mysqlsh登錄數(shù)據(jù)庫,并列出庫名,例如:
[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命令模式。
2、備份整個實例
創(chuàng)建備份目錄。
mkdir -p /data/backup
登錄數(shù)據(jù)庫。
mysqlsh -u root -p -S /data/mysql/mysql3306/tmp/mysql.sock
登錄后是在js模式下,備份數(shù)據(jù)是在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線程處理)。
備份后,備份目錄結(jié)果里可以查看結(jié)果如下:
其中的主要文件解釋:
@.done.json: 該文件記錄了備份結(jié)束時間,每個庫下每個表的大小等信息,例如:
@.json:該文件記錄了客戶端版本,備份類型(實例、庫或表等),元數(shù)據(jù)信息以及binlog信息(點位及GTID)。例如:
@.sql, @.post.sql:這兩個文件記錄注釋信息. 導入數(shù)據(jù)時, 我們可以通過這兩個文件自定義的SQL. 在數(shù)據(jù)導入前和數(shù)據(jù)導入后執(zhí)行,本次為全量備份,因此只有版本等注釋信息。
庫名.json: 記錄的是對應庫名、表等信息。
庫名.sql: 具體的建庫SQL腳本。
庫名@表名.json:記錄了對于的表的元數(shù)據(jù)信息,包括庫名,表名,字段名,主鍵等信息。
庫名@表名.sql: 具體的建表SQL腳本。
庫名@表名@@*.tsv.zst: 具體數(shù)據(jù)文件。
庫名@表名@@*.tsv.zst.idx: 具體索引文件。
@.users.sql : 數(shù)據(jù)庫用戶信息,包含創(chuàng)建用戶以及授權的SQL腳本。
3、備份指定庫
創(chuàng)建備份目錄:重新創(chuàng)建一個專用于備份指定庫的目錄。
mkdir -p data/backup/backup_schemas
使用shellsh登錄數(shù)據(jù)庫,并查看當前有哪些庫。
# 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 >
以上則備份完成。
到對于目錄下查看備份結(jié)果如下:
4、備份指定表
再次先創(chuàng)建目錄。
[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]#
登錄數(shù)據(jù)庫,并查看庫及表名。
[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 >
查看備份結(jié)果:
至此,備份數(shù)據(jù)庫實例、庫、表的命令已簡單演示完畢,實際生產(chǎn)使用過程基本會用腳本實現(xiàn),大家可以自行編寫。