MySQL Shell 是 MySQL 的一个高级客户端和代码编辑器,是松玩第二代 MySQL 客户端。第一代 MySQL 客户端即我们常用的松玩 MySQL 。除了提供类似于 MySQL 的松玩 SQL 功能外,MySQL Shell 还提供 JavaScript 和 Python 脚本功能,松玩并包括与 MySQL 一起使用的松玩 API 。MySQL Shell 除了可以对数据库里的松玩数据进行操作,还可以对数据库进行管理,松玩特别是松玩对MGR的支持,使用MySQL Shell 可以非常方便的松玩对MGR进行搭建、管理、松玩配置等
MySQL Shell for GreatSQL 的出现是因为在 GreatSQL 8.0.25-16 版本的时候引入了MGR仲裁节点(投票节点)的新特性,MySQL提供的MySQL Shell无法识别该特性,因此我们提供了 MySQL Shell for GreatSQL 版本,以下就称为MySQL Shell for GreatSQL
但是!因为 JS 库中含有商业库,所以GreatSQL社区在编译的时候就没有加上 JS 的脚本功能。
大家使用的时候不要一直输入\js说怎么切换不过去了 :)
不过Python模式的语法和JavaScript模式的语法是大同小异的,举个例子:
JavaScript 语法 | Python 语法 |
var c=dba.getCluster() | c=dba.get_cluster() |
c.status() | c.statsu() |
c.setPrimaryInstance() | c.set_primary_instance() |
不过就是变量名命名风格些许不同而已,本质上是没有区别的。本文也将使用 GreatSQL Shell-8.0.25-16 中 Python 模式来带你玩转 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进入下载文件列表最下方就是我们的MySQL Shell for GreatSQL,大家按机器和架构下载对应版本
本文机器环境是CentOS7.9-x86-64所以下载第一个即可
$ cat /etc/system-releaseCentOS Linux release 7.9.2009 (Core)$ uname -aLinux 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 目录添加到环境变量中:
$ 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 的环境,如果没有环境的话,需要安装yum install python3 -y
$ python3 -VPython 3.6.8
一切准备就绪!就可以开始使用 MySQL Shell for GreatSQL 了
$ mysqlsh
MySQL Shell for GreatSQL 的界面如下:
细心的同学就会发现,有一个 WARNING ,没关系我们根据提示看下 mysqlsh.log
$ cat /root/.mysqlsh/mysqlsh.log
在日志中发现这样一段提示,意思就是少了一个 Python 的模块 certifi
ModuleNotFoundError: No module named 'certifi'
解决方法就是用pip来安装下这个缺失的模块即可:
$ pip3.6 install --user certifi
再次进入MySQL Shell for GreatSQL $ mysqlsh
现在就没有讨厌的 WARNING 了。
MySQL Shell for GreatSQL 同时也是支持定义自己的提示符的,在 promt 目录下,有许多的模板可供使用
$ ls /usr/local/GreatSQLshell/greatsql-shell-8.0.25-16-Linux-glibc2.17-x86_64/share/mysqlsh/promptprompt_16.json prompt_256.json prompt_256pl.json prompt_dbl_256.json prompt_dbl_256pl.json README.promptprompt_256inv.json prompt_256pl+aw.json prompt_classic.json prompt_dbl_256pl+aw.json prompt_nocolor.json
使用方式如下,例如我想换成这个模板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
再进入 MySQL Shell for GreatSQL 看看已经变了个样子
当然也可以自行修改.json文件,修改成你喜欢的自定义配置,这都是没问题的。
现在的 MySQL Shell for GreatSQL 是没法使用的,因为我们是用 $ mysqlsh 命令直接登录到 Shell 环境,由于未携带登录验证信息(user、host、password)等处于未连接服务状态,在内部使用 \c \h 等简易命令外,执行其它获取服务器信息的命令会报 Not Connected.
MySQL Shell for GreatSQL 提供了多种连接实例登录方式,可以根据自己喜好选择
$ 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的方式连接数据库实例
$ mysqlsh -S/data/GreatSQL/mgr01/mysql.sock root@localhost
用sock方式连接数据库实例会让输入密码,然后会问是否保存密码
Please provide the password for 'root@localhost': //这里输入密码Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No): y //是否保存密码
一旦存储了服务器 URL 的密码,每当 MySQL Shell for GreatSQL 打开会话时,它都会从已配置的 Secret Store Helper 中检索密码,登录到服务器,而无需交互输入密码。MySQL Shell for GreatSQL 执行的脚本也是如此。如果未配置任何 Secret Store Helper,则以交互方式请求密码。
注意!MySQL Shell for GreatSQL 仅通过 Secret Store 保留服务器 URL 和密码,而不自行保留密码。
密码只有在 手动输入 时才会保留。如果在运行 MySQL Shell for GreatSQL时使用类似于服务器 URI 的连接字符串或在命令行中提供了密码,则该密码不会保留。
连接到 MySQL Shell for GreatSQL 所接受的最大密码长度为128个字符。
MySQL Shell for GreatSQL 的由于命令需要独立于执行模式而可用,因此它们以转义序列 \ 字符开头,简单列举几个:
命令 | 别名或缩写 | 描述 |
\help | \h or ? | 帮助 |
\quit | \q or \exit | 退出 |
\status | \s | 显示当前状态 |
\js | 切换为 JavaScript 语言模式 | |
\py | 切换为 Python 语言模式 | |
\sql | 切换为 SQL 语言模式 | |
\history | 查看和编辑命令行历史记录 | |
\connet | \c | 连接到 MySQL 服务器 |
\reconnect | 重新连接到 MySQL 服务器 |
Ⅰ、切换SQL模式 \sql,在 SQL 模式下按 Tab键 可以实现自动补全哦!
GreatSQL Py > \sqlSwitching to SQL mode... Commands end with ;
Ⅱ、可在任何语言状态执行操作系统命令 \system
GreatSQL Py > \system ls /usr/localgreatsql-shell-8.0.25-16-Linux-glibc2.17-x86_64.tar.xz GreatSQL8.0.32
Ⅲ、查看历史命令 \history ,选项 history.maxSize 为 MySQL Shell for GreatSQL 的最大存储条数,默认为 1000 条轮替。
GreatSQL Py > \history 1 \system ls /usr/local 2 /history 3 history 4 help() 5 /hasattr()
默认历史只能保存当前会话命令,全局不可见,退出后自动删除。可通过启用 history.autoSave 选项保存会话之间的历史记录。
MySQL Shell for GreatSQL 启动时,可以使用以下模块和对象
MySQL Shell for GreatSQL有Dump & Load工具,比 mydumper 更快的逻辑备份工具,与 myloader 不一样的是,MySQL Shell for GreatSQL Load 是通过 LOAD DATA LOCAL INFILE 命令来导入数据的。而 LOAD DATA 操作,按照官方文档的说法,比 INSERT 操作快 20 倍。该序列工具包括:
我们来动手操作下,准备一个表空间有 724MB 的表内含七百万条数据
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
dump_instance(outputUrl[, options]),备份整个数据库实例,包括用户
options 有什么选项可以使用 \? dump_instance查看
GreatSQL Py > \? dump_instanceThe 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 bedumped with consistent data.
简单来说就是
话不多说,开始动手尝试吧
GreatSQL Py > util.dump_instance("/data/backups",{ "compression": "none","threads":"16"})Acquiring global read lockGlobal read lock acquiredGathering information - doneAll transactions have been startedLocking instance for backupGlobal read lock has been releasedWriting global DDL filesWriting 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” 指的是不压缩,这里设置为不压缩主要是为了方便查看数据文件的内容。线上使用建议开启压缩
开启16线程,速度还是蛮快的,接下来我们看下数据目录
$ ll /data/backups/#有好多这里就列举几个-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
我们看看数据文件的内容:
$ head -3 test@student1@0.tsv1 Kathleen Ford F 344 Jiangnan West Road, Haizhu District 139-1119-0424 163 lin4brNtHD 9182 David Mitchell M 355 Papworth Rd, Trumpington 5892 672144 702 qoA6axcT6u 2183 Lin Yunxi M 620 Hanover Street 7091 590385 194 Tl4LY3UmgY 765
TSV 格式,每一行储存一条记录,字段与字段之间用制表符(\t)分隔。
util.dump_schemas(schemas, outputUrl[, options])备份指定库的数据。
其中,第一个schemas参数必须为数组,第二个是备份目录
GreatSQL Py > util.dump_schemas(["test"],"/data/backup_schemas",{ "threads":"16"})Acquiring global read lockGlobal read lock acquiredGathering information - doneAll 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: 4Uncompressed data size: 3.05 GBCompressed data size: 1.57 GBCompression ratio: 1.9Rows written: 19000000Bytes written: 1.57 GBAverage uncompressed throughput: 52.35 MB/sAverage compressed throughput: 26.96 MB/s
当然从MySQL Shell 8.0.28版本开始,可直接使用 util.dumpInstance 中的 includeSchemas 选项进行指定库的备份。
下面展示下在MySQL Shell version 8.0.34版本下的内容和介绍
- 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编译安装
util.dump_tables(schema, tables, outputUrl[, options])备份指定表的数据
用法和上面两个相同,tables参数必须为数组
GreatSQL localhost Py > util.dump_tables("test",["student1"],"/data/backup_table",{ "threads":"16"})Acquiring global read lockGlobal read lock acquiredGathering information - doneAll 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:12sSchemas dumped: 1Tables dumped: 1Uncompressed data size: 572.88 MBCompressed data size: 242.92 MBCompression ratio: 2.4Rows written: 7000000Bytes written: 242.92 MBAverage uncompressed throughput: 44.50 MB/s Average compressed throughput: 18.87 MB/s
当然从 MySQL Shell 8.0.28 开始,可直接使用 util.dumpInstance 中的 includeTables 选项进行指定表的备份。
- 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"})
util.load_dump(url[, options])用于导入通过 dump 命令生成的备份集
导入前,记得先打开"local_infile"参数设置set global local_infile = ON;
GreatSQL Py > util.load_dump("/data/backup_table",{ "threads":"16"})
如果想再导入一次,要把 resetProgress 设置为 True
GreatSQL Py > util.load_dump("/data/backup_table",{ "threads":"16","resetProgress":True})
当然,我们也做过导入速度测试,下附测试结果,详细对比文章见 myloader导入更快吗?并没有。。。
从上面图表看出,虽然util.load_dump很快,但还是比GreatSQL 8.0.32-24 自带的并行load data速度慢了一些,并行load data适用于频繁导入大批量数据的应用场景,性能可提升约20+倍。详情可见➥https://gitee.com/GreatSQL/GreatSQL-Manual/blob/master/5-enhance/5-1-highperf-parallel-load.md
可以用MySQL Shell for GreatSQL来搭建 MGR集群 或接管现有集群非常的方便快捷。加上GreatSQL针对MGR做了大量的改进和提升工作,进一步提升MGR的高可靠等级。
快捷的部署 + 好用的GreatSQL MGR为什么不用呢?
IP | 端口 | 角色 |
172.17.139.77 | 3306 | mgr1 |
172.17.139.77 | 3307 | mgr2 |
采用的是一个单机多实例的部署方式,如何部署单机多实例可以前往➥https://gitee.com/GreatSQL/GreatSQL-Manual/blob/master/6-oper-guide/6-6-multi-instances.md
接下来再把MySQL Shell for GreatSQL下载安装完成,即可开始部署。
注意!本次部署皆采用Shell 的 Python 模式
利用MySQL Shell for GreatSQL构建MGR集群比较简单,主要有几个步骤:
首先,用管理员账号 root 连接到第一个节点:
$ mysqlsh -S/data/GreatSQL/mgr01/mysql.sock root@localhostMySQL Shell 8.0.25
使用\s命令查看当前节点状态,确保连接正常可用
执行 dba.configure_instance() 命令开始检查当前实例是否满足安装MGR集群的条件,如果不满足可以直接配置成为MGR集群的一个节点:
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#提示当前的用户是管理员,不能直接用于MGR集群,需要新建一个账号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 password2) Create a new admin account for InnoDB cluster with minimal required grants3) Ignore and continue4) CancelPlease select an option [1]: 2 #这里选择2,即创建一个最小权限账号
接着输入要创建用户的用户名、密码即可
Please provide an account name (e.g: icroot@%) to have it created with the necessaryprivileges or leave empty and press Enter to cancel.Account Name: GreatSQL #用户名Password for new account: #密码Confirm password: #确认密码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.# 这个警告消息是告诉你正在使用的系统变量@@slave_parallel_workers已经被弃用,将在未来的版本中被移除,建议你使用新的变量名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.
完成检查并创建完新用户后,退出当前的管理员账户,并用新创建的MGR专用账户登入,准备初始化创建一个新集群:
GreatSQL Py > exit()$ mysqlsh --uri GreatSQL@172.17.139.77:3306MySQL Shell 8.0.25
这时候就可以使用我们的dba工具了,定义一个变量名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:3306Instance 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 toone server failure.
这就完成了MGR集群的初始化并加入第一个节点(引导节点)。接下来,用同样方法先用 root 账号分别登入到另外两个节点,完成节点的检查并创建最小权限级别用户(此过程略过...注意各节点上创建的用户名、密码都要一致),之后回到第一个节点,执行 addInstance()添加另外两个节点。
GreatSQL 172.17.139.77:3306 ssl Py > c.add_instance('GreatSQL@172.17.139.77:3307');#这里要指定MGR专用账号...省略...Please select a recovery method [C]lone/[A]bort (default Abort): Clone <-- 选择用Clone方式从第一个节点全量复制数据Validating instance configuration at 172.17.139.77:3306......省略...The instance '172.17.139.77:3306' was successfully added to the cluster.
这样节点就加入成功了!用c.describe()看下集群状态,如果要显示更详细信息可以使用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对象所有的命令:
GreatSQL 172.17.139.77:3306 ssl Py > \help dba*Found several entries matching dba*
如果要更详细的某个命令的帮助手册,则可以 \help 后接具体的命令:
GreatSQL 172.17.139.77:3306 ssl Py > \help dba.get_cluster
我们GreatSQL社区有"深入浅出MGR系列文章"其中就有使用Shell部署[第四篇]MGR以及管理[第五篇]:
深入浅出MGR系列文章地址➥GreatSQL-Doc: GreatSQL-Doc - Gitee.com
4. 利用MySQL Shell安装部署MGR集群 | 深入浅出MGR
5. MGR管理维护 | 深入浅出MGR
有对MGR想了解的或深入学习的,可以去阅读下。
MySQL Shell for GreatSQL以其强大的功能、灵活性和先进的工具集,确实为数据库管理人员和开发者打开了全新的大门。从基本的数据库操作到复杂的集群管理。
对于想要充分利用 GreatSQL 功能的任何人来说,掌握MySQL Shell for GreatSQL都是一项必备技能。无论你是新手还是经验丰富的数据库专家,希望这篇文章都能为你的GreatSQL旅程提供宝贵的指导和灵感。
责任编辑:华轩 来源: GreatSQL社区 MySQL数据库(责任编辑:时尚)
北京汽车(01958.HK)年度净利跌59.4% 每股收益为人民币0.24元
风头正劲的白酒板块指数再创历史新高 有专家称未来上行空间可能非常有限
森特股份(603098.SH)总市值50.5亿元 隆基股份拟溢价三成收购总股本股的27.25%
国投转债近期收益怎么样 ?国投转债申购价值分析:初始转股价15.25元
124家公司股价年内跌幅超30% 31家公司一季报有望实现增长