帶你輕松玩轉(zhuǎn)MySQL Shell for GreatSQL
一、引言
1.1 什么是MySQL Shell ?
MySQL Shell 是 MySQL 的一個(gè)高級(jí)客戶端和代碼編輯器,是第二代 MySQL 客戶端。第一代 MySQL 客戶端即我們常用的 MySQL 。除了提供類似于 MySQL 的 SQL 功能外,MySQL Shell 還提供 JavaScript 和 Python 腳本功能,并包括與 MySQL 一起使用的 API 。MySQL Shell 除了可以對(duì)數(shù)據(jù)庫里的數(shù)據(jù)進(jìn)行操作,還可以對(duì)數(shù)據(jù)庫進(jìn)行管理,特別是對(duì)MGR的支持,使用MySQL Shell 可以非常方便的對(duì)MGR進(jìn)行搭建、管理、配置等
1.2 什么是MySQL Shell for GreatSQL ?
MySQL Shell for GreatSQL 的出現(xiàn)是因?yàn)樵?GreatSQL 8.0.25-16 版本的時(shí)候引入了MGR仲裁節(jié)點(diǎn)(投票節(jié)點(diǎn))的新特性,MySQL提供的MySQL Shell無法識(shí)別該特性,因此我們提供了 MySQL Shell for GreatSQL 版本,以下就稱為MySQL Shell for GreatSQL
但是!因?yàn)?JS 庫中含有商業(yè)庫,所以GreatSQL社區(qū)在編譯的時(shí)候就沒有加上 JS 的腳本功能。
大家使用的時(shí)候不要一直輸入\js說怎么切換不過去了 :)
不過Python模式的語法和JavaScript模式的語法是大同小異的,舉個(gè)例子:
JavaScript 語法 | Python 語法 |
var c=dba.getCluster() | c=dba.get_cluster() |
c.status() | c.statsu() |
c.setPrimaryInstance() | c.set_primary_instance() |
不過就是變量名命名風(fēng)格些許不同而已,本質(zhì)上是沒有區(qū)別的。本文也將使用 GreatSQL Shell-8.0.25-16 中 Python 模式來帶你玩轉(zhuǎn) MySQL Shell for GreatSQL
二、安裝與配置
2.1 安裝 MySQL Shell for GreatSQL
首先我們先下載MySQL Shell for GreatSQL,下載地址在GreatSQL的gitee倉庫,和我們的GreatSQL 8.0.32-24新版本放在一起:?https://gitee.com/GreatSQL/GreatSQL/releases/tag/GreatSQL-8.0.32-24進(jìn)入下載文件列表最下方就是我們的MySQL Shell for GreatSQL,大家按機(jī)器和架構(gòu)下載對(duì)應(yīng)版本
本文機(jī)器環(huán)境是CentOS7.9-x86-64所以下載第一個(gè)即可
$ cat /etc/system-release
CentOS Linux release 7.9.2009 (Core)
$ uname -a
Linux hy 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
下載完成后解壓:
$ tar -xvf greatsql-shell-8.0.25-16-Linux-glibc2.17-x86_64.tar.xz
接著把 bin 目錄添加到環(huán)境變量中:
$ echo "export PATH=$PATH:/usr/local/greatsql-shell-8.0.25-16-Linux-glibc2.17-x86_64/bin" >> /root/.bash_profile
MySQL Shell for GreatSQL 需要 Python 3.6 的環(huán)境,如果沒有環(huán)境的話,需要安裝yum install python3 -y
$ python3 -V
Python 3.6.8
一切準(zhǔn)備就緒!就可以開始使用 MySQL Shell for GreatSQL 了
$ mysqlsh
2.2 界面特征
MySQL Shell for GreatSQL 的界面如下:
細(xì)心的同學(xué)就會(huì)發(fā)現(xiàn),有一個(gè) WARNING ,沒關(guān)系我們根據(jù)提示看下 mysqlsh.log
$ cat /root/.mysqlsh/mysqlsh.log
在日志中發(fā)現(xiàn)這樣一段提示,意思就是少了一個(gè) Python 的模塊 certifi
ModuleNotFoundError: No module named 'certifi'
解決方法就是用pip來安裝下這個(gè)缺失的模塊即可:
$ pip3.6 install --user certifi
再次進(jìn)入MySQL Shell for GreatSQL $ mysqlsh
現(xiàn)在就沒有討厭的 WARNING 了。
MySQL Shell for GreatSQL 同時(shí)也是支持定義自己的提示符的,在 promt 目錄下,有許多的模板可供使用
$ ls /usr/local/GreatSQLshell/greatsql-shell-8.0.25-16-Linux-glibc2.17-x86_64/share/mysqlsh/prompt
prompt_16.json prompt_256.json prompt_256pl.json prompt_dbl_256.json prompt_dbl_256pl.json README.prompt
prompt_256inv.json prompt_256pl+aw.json prompt_classic.json prompt_dbl_256pl+aw.json prompt_nocolor.json
使用方式如下,例如我想換成這個(gè)模板prompt_16.json
$ export MYSQLSH_PROMPT_THEME=/usr/local/GreatSQLshell/greatsql-shell-8.0.25-16-Linux-glibc2.17-x86_64/share/mysqlsh/prompt/prompt_16.json
再進(jìn)入 MySQL Shell for GreatSQL 看看已經(jīng)變了個(gè)樣子
當(dāng)然也可以自行修改.json文件,修改成你喜歡的自定義配置,這都是沒問題的。
現(xiàn)在的 MySQL Shell for GreatSQL 是沒法使用的,因?yàn)槲覀兪怯?span> $ mysqlsh 命令直接登錄到 Shell 環(huán)境,由于未攜帶登錄驗(yàn)證信息(user、host、password)等處于未連接服務(wù)狀態(tài),在內(nèi)部使用 \c \h 等簡(jiǎn)易命令外,執(zhí)行其它獲取服務(wù)器信息的命令會(huì)報(bào) Not Connected.
三、基本操作和使用
3.1 連接數(shù)據(jù)庫實(shí)例
MySQL Shell for GreatSQL 提供了多種連接實(shí)例登錄方式,可以根據(jù)自己喜好選擇
$ mysqlsh --help
...上面省略部分...
Usage examples:
$ mysqlsh root@localhost/schema
$ mysqlsh mysqlx://root@some.server:3307/world_x
$ mysqlsh --uri root@localhost --py -f sample.py sample param
$ mysqlsh root@targethost:33070 -s world_x -f sample.js
$ mysqlsh -- util check-for-server-upgrade root@localhost --output-format=JSON
$ mysqlsh mysqlx://user@host/db --import ~/products.json shop
這里選擇MySQL Shell for GreatSQL sock的方式連接數(shù)據(jù)庫實(shí)例
$ mysqlsh -S/data/GreatSQL/mgr01/mysql.sock root@localhost
用sock方式連接數(shù)據(jù)庫實(shí)例會(huì)讓輸入密碼,然后會(huì)問是否保存密碼
Please provide the password for 'root@localhost': //這里輸入密碼
Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No): y //是否保存密碼
一旦存儲(chǔ)了服務(wù)器 URL 的密碼,每當(dāng) MySQL Shell for GreatSQL 打開會(huì)話時(shí),它都會(huì)從已配置的 Secret Store Helper 中檢索密碼,登錄到服務(wù)器,而無需交互輸入密碼。MySQL Shell for GreatSQL 執(zhí)行的腳本也是如此。如果未配置任何 Secret Store Helper,則以交互方式請(qǐng)求密碼。
注意!MySQL Shell for GreatSQL 僅通過 Secret Store 保留服務(wù)器 URL 和密碼,而不自行保留密碼。
密碼只有在 手動(dòng)輸入 時(shí)才會(huì)保留。如果在運(yùn)行 MySQL Shell for GreatSQL時(shí)使用類似于服務(wù)器 URI 的連接字符串或在命令行中提供了密碼,則該密碼不會(huì)保留。
連接到 MySQL Shell for GreatSQL 所接受的最大密碼長(zhǎng)度為128個(gè)字符。
3.2 基本命令
MySQL Shell for GreatSQL 的由于命令需要獨(dú)立于執(zhí)行模式而可用,因此它們以轉(zhuǎn)義序列 \ 字符開頭,簡(jiǎn)單列舉幾個(gè):
命令 | 別名或縮寫 | 描述 |
\help | \h or ? | 幫助 |
\quit | \q or \exit | 退出 |
\status | \s | 顯示當(dāng)前狀態(tài) |
\js | 切換為 JavaScript 語言模式 | |
\py | 切換為 Python 語言模式 | |
\sql | 切換為 SQL 語言模式 | |
\history | 查看和編輯命令行歷史記錄 | |
\connet | \c | 連接到 MySQL 服務(wù)器 |
\reconnect | 重新連接到 MySQL 服務(wù)器 |
3.3 基本用法
Ⅰ、切換SQL模式 \sql,在 SQL 模式下按 Tab鍵 可以實(shí)現(xiàn)自動(dòng)補(bǔ)全哦!
GreatSQL Py > \sql
Switching to SQL mode... Commands end with ;
Ⅱ、可在任何語言狀態(tài)執(zhí)行操作系統(tǒng)命令 \system
GreatSQL Py > \system ls /usr/local
greatsql-shell-8.0.25-16-Linux-glibc2.17-x86_64.tar.xz GreatSQL8.0.32
Ⅲ、查看歷史命令 \history ,選項(xiàng) history.maxSize 為 MySQL Shell for GreatSQL 的最大存儲(chǔ)條數(shù),默認(rèn)為 1000 條輪替。
GreatSQL Py > \history
1 \system ls /usr/local
2 /history
3 history
4 help()
5 /hasattr()
默認(rèn)歷史只能保存當(dāng)前會(huì)話命令,全局不可見,退出后自動(dòng)刪除。可通過啟用 history.autoSave 選項(xiàng)保存會(huì)話之間的歷史記錄。
3.4 全局對(duì)象
MySQL Shell for GreatSQL 啟動(dòng)時(shí),可以使用以下模塊和對(duì)象
- dba:用于InnoDB Cluster、ReplicaSet 管理;
- mysql:支持使用經(jīng)典 MySQL 協(xié)議連接到 MySQL 服務(wù)器,允許執(zhí)行 SQL;
- mysqlx:用于通過 MySQL X DevAPI 處理 X 協(xié)議會(huì)話;
- os:允許訪問允許與操作系統(tǒng)交互的功能;
- session:代表當(dāng)前打開的MySQL會(huì)話;
- shell:允許訪問通用功能和屬性;
- sys:允許訪問系統(tǒng)特定的參數(shù);
- util:對(duì)諸如升級(jí)檢查器和JSON導(dǎo)入之類的各種工具進(jìn)行了分組;
四、備份和恢復(fù)
MySQL Shell for GreatSQL有Dump & Load工具,比 mydumper 更快的邏輯備份工具,與 myloader 不一樣的是,MySQL Shell for GreatSQL Load 是通過 LOAD DATA LOCAL INFILE 命令來導(dǎo)入數(shù)據(jù)的。而 LOAD DATA 操作,按照官方文檔的說法,比 INSERT 操作快 20 倍。該序列工具包括:
- util.dump_instance():備份整個(gè)數(shù)據(jù)庫實(shí)例,包括用戶
- util.dump_schemas():備份指定數(shù)據(jù)庫 schema
- util.dump_tables():備份指定的表或視圖
- util.load_dump():恢復(fù)備份
我們來動(dòng)手操作下,準(zhǔn)備一個(gè)表空間有 724MB 的表內(nèi)含七百萬條數(shù)據(jù)
greatsql> select count(*) from student1;
+----------+
| count(*) |
+----------+
| 7000000 |
+----------+
1 row in set (2.62 sec)
greatsql> system ls -l /data/GreatSQL/mgr01/test
-rw-r----- 1 mysql mysql 759169024 7月 25 12:15 student1.ibd
4.1 備份整個(gè)數(shù)據(jù)庫實(shí)例,包括用戶
dump_instance(outputUrl[, options]),備份整個(gè)數(shù)據(jù)庫實(shí)例,包括用戶
- outputUrl 是備份目錄,不能為空。
- options 是可指定的選項(xiàng)
options 有什么選項(xiàng)可以使用 \? dump_instance查看
GreatSQL Py > \? dump_instance
The following options are supported://找到這句下面就是
使用起來也是有限制的,官方原文:
Requirements
- MySQL Server 5.7 or newer is required.
- File size limit for files uploaded to the OCI bucket is 1.2 TiB.
- Columns with data types which are not safe to be stored in text form
(i.e. BLOB) are converted to Base64, hence the size of such columns
cannot exceed approximately 0.74 * max_allowed_packet bytes, as
configured through that system variable at the target server.
- Schema object names must use latin1 or utf8 character set.
- Only tables which use the InnoDB storage engine are guaranteed to be
dumped with consistent data.
簡(jiǎn)單來說就是
- "最好是INNODB的數(shù)據(jù)引擎"
- "版本在5.7及以上"
- "必須使用latin1或utf8字符集"
- "要有BACKUP_ADMIN權(quán)限"
話不多說,開始動(dòng)手嘗試吧
GreatSQL Py > util.dump_instance("/data/backups",{"compression": "none","threads":"16"})
Acquiring global read lock
Global read lock acquired
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
...中間省略
1 thds dumping - 109% (19.00M rows / ~17.37M rows), 263.77K rows/s, 54.42 MB/s
Duration: 00:01:05s
Schemas dumped: 3
Tables dumped: 11
Data size: 3.05 GB
Rows written: 19000005
Bytes written: 3.05 GB
Average throughput: 46.80 MB/s
注意!compression: “none” 指的是不壓縮,這里設(shè)置為不壓縮主要是為了方便查看數(shù)據(jù)文件的內(nèi)容。線上使用建議開啟壓縮
開啟16線程,速度還是蠻快的,接下來我們看下數(shù)據(jù)目錄
$ ll /data/backups/
#有好多這里就列舉幾個(gè)
-rw-r----- 1 root root 5773 8月 2 11:28 @.done.json
-rw-r----- 1 root root 1119 8月 2 11:27 @.json
-rw-r----- 1 root root 231 8月 2 11:27 @.post.sql
-rw-r----- 1 root root 231 8月 2 11:27 @.sql
-rw-r----- 1 root root 458 8月 2 11:27 test.json
-rw-r----- 1 root root 24536863 8月 2 11:27 test@student1@0.tsv
- @.done.json:會(huì)記錄備份的結(jié)束時(shí)間,備份集的大小,備份結(jié)束時(shí)生成。
- @.json:會(huì)記錄備份的一些元數(shù)據(jù)信息,包括備份時(shí)的一致性位置點(diǎn)信息:binlogFile,binlogPosition 和 gtidExecuted,這些信息可用來建立復(fù)制。
- @.sql,@.post.sql:這兩個(gè)文件只有一些注釋信息。不過在通過 util.loadDump 導(dǎo)入數(shù)據(jù)時(shí),我們可以通過這兩個(gè)文件自定義一些 SQL。其中,@.sql 是數(shù)據(jù)導(dǎo)入前執(zhí)行,@.post.sql 是數(shù)據(jù)導(dǎo)入后執(zhí)行。
- sbtest.json:記錄 sbtest 中已經(jīng)備份的表、視圖、定時(shí)器、函數(shù)和存儲(chǔ)過程。
- *.tsv:數(shù)據(jù)文件。
我們看看數(shù)據(jù)文件的內(nèi)容:
$ head -3 test@student1@0.tsv
1 Kathleen Ford F 344 Jiangnan West Road, Haizhu District 139-1119-0424 163 lin4brNtHD 918
2 David Mitchell M 355 Papworth Rd, Trumpington 5892 672144 702 qoA6axcT6u 218
3 Lin Yunxi M 620 Hanover Street 7091 590385 194 Tl4LY3UmgY 765
TSV 格式,每一行儲(chǔ)存一條記錄,字段與字段之間用制表符(\t)分隔。
- test@student1.json:記錄了表相關(guān)的一些元數(shù)據(jù)信息,如列名,字段之間的分隔符(fieldsTerminatedBy)等。
- test@student1.sql:sbtest.sbtest1 的建表語句。
- test.sql:建庫語句。如果這個(gè)庫中存在存儲(chǔ)過程、函數(shù)、定時(shí)器,也是寫到這個(gè)文件中。
- @.users.sql:創(chuàng)建賬號(hào)及授權(quán)語句。默認(rèn)不會(huì)備份 mysql.infoschema,mysql.session,mysql.sys 這三個(gè)內(nèi)部賬號(hào)。
4.2 備份指定數(shù)據(jù)庫
util.dump_schemas(schemas, outputUrl[, options])備份指定庫的數(shù)據(jù)。
其中,第一個(gè)schemas參數(shù)必須為數(shù)組,第二個(gè)是備份目錄
GreatSQL Py > util.dump_schemas(["test"],"/data/backup_schemas",{"threads":"16"})
Acquiring global read lock
Global read lock acquired
Gathering information - done
All transactions have been started
...中間省略...
1 thds dumping - 109% (19.00M rows / ~17.37M rows), 530.98K rows/s, 49.34 MB/s uncompressed, 22.02 MB/s compressed
Duration: 00:00:58s
Schemas dumped: 1
Tables dumped: 4
Uncompressed data size: 3.05 GB
Compressed data size: 1.57 GB
Compression ratio: 1.9
Rows written: 19000000
Bytes written: 1.57 GB
Average uncompressed throughput: 52.35 MB/s
Average compressed throughput: 26.96 MB/s
當(dāng)然從MySQL Shell 8.0.28版本開始,可直接使用 util.dumpInstance 中的 includeSchemas 選項(xiàng)進(jìn)行指定庫的備份。
下面展示下在MySQL Shell version 8.0.34版本下的內(nèi)容和介紹
- includeSchemas: list of strings (default: empty) - List of schemas to
be included in the dump.
GreatSQL Py > util.dump_instance("/data/backups",{"includeSchemas":["test"],"threads":"16"})
如果想要更高的版本的MySQL Shell for GreatSQL,可以參考文章 MySQL Shell 8.0.32 for GreatSQL編譯安裝
4.3 備份指定表
util.dump_tables(schema, tables, outputUrl[, options])備份指定表的數(shù)據(jù)
用法和上面兩個(gè)相同,tables參數(shù)必須為數(shù)組
GreatSQL localhost Py > util.dump_tables("test",["student1"],"/data/backup_table",{"threads":"16"})
Acquiring global read lock
Global read lock acquired
Gathering information - done
All transactions have been started
...中間省略...
1 thds dumping - 110% (7.00M rows / ~6.31M rows), 539.12K rows/s, 44.17 MB/s uncompressed, 18.75 MB/s compressed
Duration: 00:00:12s
Schemas dumped: 1
Tables dumped: 1
Uncompressed data size: 572.88 MB
Compressed data size: 242.92 MB
Compression ratio: 2.4
Rows written: 7000000
Bytes written: 242.92 MB
Average uncompressed throughput: 44.50 MB/s
Average compressed throughput: 18.87 MB/s
當(dāng)然從 MySQL Shell 8.0.28 開始,可直接使用 util.dumpInstance 中的 includeTables 選項(xiàng)進(jìn)行指定表的備份。
- includeTables: list of strings (default: empty) - List of tables or
views to be included in the dump in the format of schema.table.
GreatSQL Py > util.dump_instance("/data/backups",{"includeTables":["test.test"],"threads":"16"})
4.4 導(dǎo)入生成的備份
util.load_dump(url[, options])用于導(dǎo)入通過 dump 命令生成的備份集
導(dǎo)入前,記得先打開"local_infile"參數(shù)設(shè)置set global local_infile = ON;
GreatSQL Py > util.load_dump("/data/backup_table",{"threads":"16"})
如果想再導(dǎo)入一次,要把 resetProgress 設(shè)置為 True
GreatSQL Py > util.load_dump("/data/backup_table",{"threads":"16","resetProgress":True})
當(dāng)然,我們也做過導(dǎo)入速度測(cè)試,下附測(cè)試結(jié)果,詳細(xì)對(duì)比文章見 myloader導(dǎo)入更快嗎?并沒有。。。
從上面圖表看出,雖然util.load_dump很快,但還是比GreatSQL 8.0.32-24 自帶的并行l(wèi)oad data速度慢了一些,并行l(wèi)oad data適用于頻繁導(dǎo)入大批量數(shù)據(jù)的應(yīng)用場(chǎng)景,性能可提升約20+倍。詳情可見?https://gitee.com/GreatSQL/GreatSQL-Manual/blob/master/5-enhance/5-1-highperf-parallel-load.md
4.5 參數(shù)解析
- analyzeTables:可選參數(shù) on/off/histogram;表加載完畢后,是否執(zhí)行 ANALYZE TABLE 操作。默認(rèn)是 off(不執(zhí)行),histogram(只對(duì)有直方圖信息的表執(zhí)行)
- characterSet:字符集,無需顯式設(shè)置,默認(rèn)會(huì)從備份集中獲取。
- createInvisiblePKs:是否創(chuàng)建隱式主鍵,默認(rèn)從備份集中獲取。
- deferTableIndexes:可選參數(shù) off(不延遲)/fulltext(只延遲創(chuàng)建全文索引,默認(rèn)值)/all(延遲創(chuàng)建所有索引);是否延遲(數(shù)據(jù)加載完畢后)創(chuàng)建二級(jí)索引。
- dryRun:試運(yùn)行。此時(shí)只會(huì)打印備份信息,不會(huì)執(zhí)行備份操作。
- excludeSchemas:忽略某些庫的備份,多個(gè)庫之間用逗號(hào)隔開excludeSchemas: ["db1", "db2"]
- excludeTables:忽略某些表的備份,表必須是 庫名.表名 的格式,多個(gè)表之間用逗號(hào)隔開excludeTables: ["sbtest.sbtest1", "sbtest.sbtest2"]
- excludeUsers:忽略某些賬號(hào)的備份,可指定多個(gè)賬號(hào)。
- ignoreExistingObjects:是否忽略已經(jīng)存在的對(duì)象,默認(rèn)為 off。
- ignoreVersion:忽略 MySQL 的版本檢測(cè)。默認(rèn)情況下,要求備份實(shí)例和導(dǎo)入實(shí)例的大版本一致。
- includeSchemas:指定某些庫的備份。
- includeTables:指定某些表的備份。
- includeUsers:指定某些賬號(hào)的備份,可指定多個(gè)賬號(hào)。
- loadData:是否導(dǎo)入數(shù)據(jù),默認(rèn)為 true。
- loadDdl:是否導(dǎo)入 DDL 語句,默認(rèn)為 true。
- loadIndexes:與 deferTableIndexes 一起使用,用來決定數(shù)據(jù)加載完畢后,最后的二級(jí)索引是否創(chuàng)建,默認(rèn)為 true。
- loadUsers:是否導(dǎo)入賬號(hào),默認(rèn)為 false。注意,即使將 loadUsers 設(shè)置為 true,也不會(huì)導(dǎo)入當(dāng)前正在執(zhí)行導(dǎo)入操作的用戶。
- progressFile:在導(dǎo)入的過程中,會(huì)在備份目錄生成一個(gè)progressFile,用于記錄加載過程中的進(jìn)度信息,這個(gè)進(jìn)度信息可用來實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能。默認(rèn)為 load-progress…progress。
- resetProgress:如果備份目錄中存在progressFile,默認(rèn)會(huì)從上次完成的地方繼續(xù)執(zhí)行。如果要從頭開始執(zhí)行,需將 resetProgress 設(shè)置為 true。該參數(shù)默認(rèn)為 off。
- schema:將表導(dǎo)入到指定 schema 中,適用于通過 util.dumpTables 創(chuàng)建的備份。
- showMetadata:導(dǎo)入時(shí)是否打印一致性備份時(shí)的位置點(diǎn)信息。
- showProgress:是否打印進(jìn)度信息。
- skipBinlog:是否設(shè)置 sql_log_bin=0 ,默認(rèn) false。這一點(diǎn)與 mysqldump、mydumper 不同,后面這兩個(gè)工具默認(rèn)會(huì)禁用 Binlog。
- threads:并發(fā)線程數(shù),默認(rèn)為 4。
- updateGtidSet:更新 GTID_PURGED。可設(shè)置:off(不更新,默認(rèn)值), replace(替代目標(biāo)實(shí)例的 GTID_PURGED), append(追加)。
- waitDumpTimeout:util.loadDump 可導(dǎo)入當(dāng)前正在備份的備份集。處理完所有文件后,如果備份還沒有結(jié)束(具體來說,是備份集中沒有生成 @.done.json),util.loadDump 會(huì)報(bào)錯(cuò)退出,可指定 waitDumpTimeout 等待一段時(shí)間,單位秒。
- osBucketName:osNamespace,ociConfigFile,ociProfile,ociParManifest,ociParExpireTime:OCI 對(duì)象存儲(chǔ)相關(guān)。
- osNamespace:OCI 對(duì)象存儲(chǔ)相關(guān)。
- ociConfigFile:OCI 對(duì)象存儲(chǔ)相關(guān)。
- ociProfile:OCI 對(duì)象存儲(chǔ)相關(guān)。
4.6 使用注意事項(xiàng)
- 導(dǎo)入時(shí),注意 max_allowed_packet 的限制,導(dǎo)入之前,需將目標(biāo)實(shí)例的 local_infile 設(shè)置為 ON。
- 該工具屬于客戶端工具,生成的文件在客戶端。
- 導(dǎo)出的時(shí)候,導(dǎo)出路徑下不能有文件。
- 表上存在主鍵或唯一索引才能進(jìn)行 chunk 級(jí)別的并行備份。字段的數(shù)據(jù)類型不限。不像 mydumper,分片鍵只能是整數(shù)類型。
- 對(duì)于不能進(jìn)行并行備份的表,目前會(huì)備份到一個(gè)文件中。如果該文件過大,不用擔(dān)心大事務(wù)的問題,util.loadDump 在導(dǎo)入時(shí)會(huì)自動(dòng)進(jìn)行切割。
- util.dumpInstance 只能保證 InnoDB 表的備份一致性。
- 默認(rèn)不會(huì)備份 information_schema,mysql,ndbinfo,performance_schema,sys。
- 備份實(shí)例支持 GreatSQL/MySQL 5.6 及以上版本,導(dǎo)入實(shí)例支持 GreatSQL/MySQL 5.7 及以上版本。
- 備份的過程中,會(huì)將 BLOB 等非文本安全的列轉(zhuǎn)換為 Base64,由此會(huì)導(dǎo)致轉(zhuǎn)換后的數(shù)據(jù)大小超過原數(shù)據(jù)。
五、快速搭建MGR集群
可以用MySQL Shell for GreatSQL來搭建 MGR集群 或接管現(xiàn)有集群非常的方便快捷。加上GreatSQL針對(duì)MGR做了大量的改進(jìn)和提升工作,進(jìn)一步提升MGR的高可靠等級(jí)。
快捷的部署 + 好用的GreatSQL MGR為什么不用呢?
5.1 部署準(zhǔn)備
IP | 端口 | 角色 |
172.17.139.77 | 3306 | mgr1 |
172.17.139.77 | 3307 | mgr2 |
采用的是一個(gè)單機(jī)多實(shí)例的部署方式,如何部署單機(jī)多實(shí)例可以前往?https://gitee.com/GreatSQL/GreatSQL-Manual/blob/master/6-oper-guide/6-6-multi-instances.md
接下來再把MySQL Shell for GreatSQL下載安裝完成,即可開始部署。
注意!本次部署皆采用Shell 的 Python 模式
5.2 開始部署
利用MySQL Shell for GreatSQL構(gòu)建MGR集群比較簡(jiǎn)單,主要有幾個(gè)步驟:
- 檢查實(shí)例是否滿足條件。
- 創(chuàng)建并初始化一個(gè)集群。
- 逐個(gè)添加實(shí)例。
首先,用管理員賬號(hào) root 連接到第一個(gè)節(jié)點(diǎn):
$ mysqlsh -S/data/GreatSQL/mgr01/mysql.sock root@localhost
MySQL Shell 8.0.25
使用\s命令查看當(dāng)前節(jié)點(diǎn)狀態(tài),確保連接正常可用
執(zhí)行 dba.configure_instance() 命令開始檢查當(dāng)前實(shí)例是否滿足安裝MGR集群的條件,如果不滿足可以直接配置成為MGR集群的一個(gè)節(jié)點(diǎn):
GreatSQL Py > dba.configure_instance()
Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster...
This instance reports its own address as 172.17.139.77:3306
#提示當(dāng)前的用戶是管理員,不能直接用于MGR集群,需要新建一個(gè)賬號(hào)
ERROR: User 'root' can only connect from 'localhost'. New account(s) with proper source address specification to allow remote connection from all instances must be created to manage the cluster.
1) Create remotely usable account for 'root' with same grants and password
2) Create a new admin account for InnoDB cluster with minimal required grants
3) Ignore and continue
4) Cancel
Please select an option [1]: 2 #這里選擇2,即創(chuàng)建一個(gè)最小權(quán)限賬號(hào)
接著輸入要?jiǎng)?chuàng)建用戶的用戶名、密碼即可
Please provide an account name (e.g: icroot@%) to have it created with the necessary
privileges or leave empty and press Enter to cancel.
Account Name: GreatSQL #用戶名
Password for new account: #密碼
Confirm password: #確認(rèn)密碼
applierWorkerThreads will be set to the default value of 4.
The instance '172.17.139.77:3306' is valid to be used in an InnoDB cluster.
Cluster admin user 'GreatSQL'@'%' created.
The instance '172.17.139.77:3306' is already ready to be used in an InnoDB cluster.
# 這個(gè)警告消息是告訴你正在使用的系統(tǒng)變量@@slave_parallel_workers已經(jīng)被棄用,將在未來的版本中被移除,建議你使用新的變量名replica_parallel_workers來替換。
WARNING: '@@slave_parallel_workers' is deprecated and will be removed in a future release. Please use replica_parallel_workers instead. (Code 1287).
Successfully enabled parallel appliers.
完成檢查并創(chuàng)建完新用戶后,退出當(dāng)前的管理員賬戶,并用新創(chuàng)建的MGR專用賬戶登入,準(zhǔn)備初始化創(chuàng)建一個(gè)新集群:
GreatSQL Py > exit()
$ mysqlsh --uri GreatSQL@172.17.139.77:3306
MySQL Shell 8.0.25
這時(shí)候就可以使用我們的dba工具了,定義一個(gè)變量名c,方便下面引用
GreatSQL 172.17.139.77:3306 ssl Py > c = dba.create_cluster('MGR1');
A new InnoDB cluster will be created on instance '172.17.139.77:3306'.
Validating instance configuration at 172.17.139.77:3306...
This instance reports its own address as 172.17.139.77:3306
Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using '172.17.139.77:33061'. Use the localAddress option to override.
Creating InnoDB cluster 'MGR1' on '172.17.139.77:3306'...
Adding Seed Instance...
Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.
At least 3 instances are needed for the cluster to be able to withstand up to
one server failure.
這就完成了MGR集群的初始化并加入第一個(gè)節(jié)點(diǎn)(引導(dǎo)節(jié)點(diǎn))。接下來,用同樣方法先用 root 賬號(hào)分別登入到另外兩個(gè)節(jié)點(diǎn),完成節(jié)點(diǎn)的檢查并創(chuàng)建最小權(quán)限級(jí)別用戶(此過程略過...注意各節(jié)點(diǎn)上創(chuàng)建的用戶名、密碼都要一致),之后回到第一個(gè)節(jié)點(diǎn),執(zhí)行 addInstance()添加另外兩個(gè)節(jié)點(diǎn)。
GreatSQL 172.17.139.77:3306 ssl Py > c.add_instance('GreatSQL@172.17.139.77:3307');
#這里要指定MGR專用賬號(hào)
...省略...
Please select a recovery method [C]lone/[A]bort (default Abort): Clone <-- 選擇用Clone方式從第一個(gè)節(jié)點(diǎn)全量復(fù)制數(shù)據(jù)
Validating instance configuration at 172.17.139.77:3306...
...省略...
The instance '172.17.139.77:3306' was successfully added to the cluster.
這樣節(jié)點(diǎn)就加入成功了!用c.describe()看下集群狀態(tài),如果要顯示更詳細(xì)信息可以使用c.status()
GreatSQL 172.17.139.77:3306 ssl Py > c.describe()
{
"clusterName": "mgr1",
"defaultReplicaSet": {
"name": "default",
"topology": [
{
"address": "172.17.139.77:3306",
"label": "172.17.139.77:3306",
"role": "HA"
},
{
"address": "172.17.139.77:3307",
"label": "172.17.139.77:3307",
"role": "HA"
}
],
"topologyMode": "Single-Primary"
}
}
列出下DBA對(duì)象所有的命令:
GreatSQL 172.17.139.77:3306 ssl Py > \help dba*
Found several entries matching dba*
- dba:命令本身提供對(duì)集群管理和實(shí)例管理的高級(jí)功能的訪問。
- dba.check_instance_configuration:檢查GreatSQL實(shí)例的配置是否符合InnoDB集群的要求。
- dba.configure_instance:配置實(shí)例以加入InnoDB集群,調(diào)整設(shè)置以滿足集群要求。
- dba.configure_local_instance:配置本地GreatSQL實(shí)例以便用于InnoDB集群。
- dba.configure_replica_set_instance:配置副本集實(shí)例以滿足副本集的要求。
- dba.create_cluster:創(chuàng)建一個(gè)新的InnoDB集群。
- dba.create_replica_set:創(chuàng)建一個(gè)新的GreatSQL副本集。
- dba.delete_sandbox_instance:刪除一個(gè)現(xiàn)有的沙箱實(shí)例。
- dba.deploy_sandbox_instance:在本地計(jì)算機(jī)上部署一個(gè)沙箱GreatSQL Server實(shí)例。
- dba.drop_metadata_schema:刪除現(xiàn)有的InnoDB集群元數(shù)據(jù)模式。
- dba.get_cluster:獲取現(xiàn)有的InnoDB集群的引用。
- dba.get_replica_set:取現(xiàn)有GreatSQL副本集的引用。
- dba.help:顯示dba模塊的幫助信息。
- dba.kill_sandbox_instance:殺死沙箱GreatSQL實(shí)例。
- dba.reboot_cluster_from_complete_outage:從完全停機(jī)狀態(tài)重新啟動(dòng)InnoDB集群。
- dba.session:獲取當(dāng)前GreatSQL會(huì)話的引用。
- dba.start_sandbox_instance:?jiǎn)?dòng)沙箱GreatSQL實(shí)例。
- dba.stop_sandbox_instance:停止沙箱GreatSQL實(shí)例。
- dba.upgrade_metadata:升級(jí)集群的元數(shù)據(jù)模式以使其與當(dāng)前版本的MySQL Shell for GreatSQL兼容。
- dba.verbose:用于控制dba命令的詳細(xì)輸出。
如果要更詳細(xì)的某個(gè)命令的幫助手冊(cè),則可以 \help 后接具體的命令:
GreatSQL 172.17.139.77:3306 ssl Py > \help dba.get_cluster
我們GreatSQL社區(qū)有"深入淺出MGR系列文章"其中就有使用Shell部署[第四篇]MGR以及管理[第五篇]:
深入淺出MGR系列文章地址?GreatSQL-Doc: GreatSQL-Doc - Gitee.com
4. 利用MySQL Shell安裝部署MGR集群 | 深入淺出MGR
有對(duì)MGR想了解的或深入學(xué)習(xí)的,可以去閱讀下。
六、總結(jié)
MySQL Shell for GreatSQL以其強(qiáng)大的功能、靈活性和先進(jìn)的工具集,確實(shí)為數(shù)據(jù)庫管理人員和開發(fā)者打開了全新的大門。從基本的數(shù)據(jù)庫操作到復(fù)雜的集群管理。
對(duì)于想要充分利用 GreatSQL 功能的任何人來說,掌握MySQL Shell for GreatSQL都是一項(xiàng)必備技能。無論你是新手還是經(jīng)驗(yàn)豐富的數(shù)據(jù)庫專家,希望這篇文章都能為你的GreatSQL旅程提供寶貴的指導(dǎo)和靈感。