MySQL Shell for GreatSQL 8.0.25-16编译安装及使用
* GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
0. 前言
1. 修改说明
2. 编译mysql-shell
2.1 环境准备
2.2 开始编译mysql & mysql-shell
2.3 利用patchelf修改mysqlsh二进制文件
3. 运行mysql-shell for GreatSQL
0. 前言
由于GreatSQL 8.0.25-16版本中引入MGR仲裁节点(投票节点,ARBITRATOR)特性,MySQL提供的mysql-shell无法识别该特性,因此提供mysql-shell for GreatSQL版本。
1. 修改说明
需要修改mysql-shell源码,增加对仲裁节点(投票节点)角色类型的支持,涉及到两个文件:
mysqlshdk/libs/mysql/group_replication.h mysqlshdk/libs/mysql/group_replication.cc 在 Member_role
对象中增加 ARBITRATOR 角色类型即可。整个patch文件只有35行,很简单:
$ cat mysqlsh-for-greatsql-8.0.25-16.patch
diff --git a/mysqlshdk/libs/mysql/group_replication.cc b/mysqlshdk/libs/mysql/group_replication.cc
index ef6a8e1b9..9edbab628 100644
--- a/mysqlshdk/libs/mysql/group_replication.cc
+++ b/mysqlshdk/libs/mysql/group_replication.cc
@@ -108,6 +108,8 @@ std::string to_string(const Member_role role) {
return "PRIMARY";
case Member_role::SECONDARY:
return "SECONDARY";
+ case Member_role::ARBITRATOR:
+ return "ARBITRATOR";
case Member_role::NONE:
return "NONE";
}
@@ -119,6 +121,8 @@ Member_role to_member_role(const std::string &role) {
return Member_role::PRIMARY;
} else if (shcore::str_casecmp("SECONDARY", role.c_str()) == 0) {
return Member_role::SECONDARY;
+ } else if (shcore::str_casecmp("ARBITRATOR", role.c_str()) == 0) {
+ return Member_role::ARBITRATOR;
} else if (role.empty()) {
return Member_role::NONE;
} else {
diff --git a/mysqlshdk/libs/mysql/group_replication.h b/mysqlshdk/libs/mysql/group_replication.h
index c76385e4b..1d957eae5 100644
--- a/mysqlshdk/libs/mysql/group_replication.h
+++ b/mysqlshdk/libs/mysql/group_replication.h
@@ -73,7 +73,7 @@ enum class Member_state {
MISSING
};
-enum class Member_role { PRIMARY, SECONDARY, NONE };
+enum class Member_role { PRIMARY, SECONDARY, ARBITRATOR, NONE };
enum class Topology_mode { SINGLE_PRIMARY, MULTI_PRIMARY, NONE };
2. 编译mysql-shell
从MySQL官网下载的mysql-shell 8.0.27源码包是有问题的,会导致编译失败,需要自己手动修改 CMakeLists.txt
文件。我已经提交bug(#106730)了,升级到8.0.28就可以了。
官方提供的参考文档 https://github.com/mysql/mysql-shell/blob/master/INSTALL 版本太老了(最后更新时间 Aug 19, 2020),几乎完全不可用。
只好我自己摸索了。
2.1 环境准备
下载几个相关的安装包:
protobuf-all-3.11.4.tar.gz,https://github.com/protocolbuffers/protobuf/releases?page=5 mysql-8.0.25.tar.gz,https://downloads.mysql.com/archives/community/ mysql-shell-8.0.25-src.tar.gz,https://downloads.mysql.com/archives/shell/ boost_1_73_0.tar.gz,https://boostorg.jfrog.io/artifactory/main/release/1.73.0/source/boost_1_73_0.tar.gz patchelf-0.14.5.tar.gz,https://github.com/NixOS/patchelf/releases/download/0.14.5/patchelf-0.14.5.tar.gz rpcsvc-proto, https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz
利用下面的Dockerfile构建一个CentOS 8的docker镜像,专门用于编译mysql-shell:
FROM docker.io/arm64v8/centos
RUN rm -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Linux-AppStream.repo /etc/yum.repos.d/CentOS-Linux-BaseOS.repo && \
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
RUN yum install -y epel-release && \
yum clean all && \
yum makecache
RUN yum install -y --skip-broken yum-utils wget diffutils net-tools vim git gcc gcc-c++ automake libtool cmake cmake3 \
make psmisc openssl openssl-devel zlib-devel readline-devel bzip2-devel expat-devel \
bison flex wget unzip libcurl-devel libevent-devel libffi-devel lz4-devel \
file clang bzip2 libxml2-devel libtirpc libtirpc-devel numactl-devel numactl-libs \
numactl openldap-devel openldap-clients pam-devel valgrind boost-devel \
libzstd libzstd-devel patchelf perl perl-Env perl-JSON perl-Memoize perl-Time-HiRes time libaio-devel libarchive \
ncurses-devel ncurses-libs pam redhat-lsb-core scl-utils-build pkg-config ccache \
jemalloc jemalloc-devel libicu-devel re2-devel redhat-lsb-core rpm* tar libssh \
cyrus-sasl-devel cyrus-sasl-scram python36 python36-devel
RUN dnf install -y dnf
RUN dnf install -y gcc-toolset-10 && source /opt/rh/gcc-toolset-10/enable
RUN echo 'source /opt/rh/gcc-toolset-10/enable' >> /root/.bash_profile
#patchelf
COPY patchelf-0.14.5.tar.gz /tmp/
#rpcsvc-proto
COPY rpcsvc-proto-1.4.tar.gz /tmp/rpcsvc-proto-1.4.tar.gz
COPY boost_1_73_0.tar.gz /opt/
RUN rm -fr /tmp/*
接下来就是制作docker镜像包,然后再启动一个docker容器,详细过程方法可参考这篇文章:自制GreatSQL Docker镜像。
启动容器后,将之前下载的几个安装包都拷贝到容器中的 /opt/ 目录下并解压缩:
drwxr-xr-x 8 root root 65536 Apr 22 2020 boost_1_73_0
-rw------- 1 root root 128699082 Dec 7 08:34 boost_1_73_0.tar.gz
drwxr-xr-x 31 7161 31415 65536 Mar 15 06:30 mysql-8.0.25
-rw-r--r-- 1 root root 285550905 Mar 15 06:12 mysql-8.0.25.tar.gz
drwxr-xr-x 17 root root 65536 Mar 15 07:12 mysql-shell-8.0.25-src
-rw-r--r-- 1 root root 81013338 Mar 15 06:12 mysql-shell-8.0.25-src.tar.gz
-rw-r--r-- 1 root root 1372 Mar 15 06:12 mysqlsh-for-greatsql-8.0.25-16.patch
-rw-r--r-- 1 root root 313132 Mar 16 09:26 patchelf-0.14.5-x86_64.tar.gz
drwxr-xr-x 18 411487 89939 65536 Mar 15 07:00 protobuf-3.11.4
-rw-r--r-- 1 root root 7408292 Mar 15 06:12 protobuf-all-3.11.4.tar.gz
drwxr-xr-x 3 root root 28 Mar 15 06:01 rh
drwxr-xr-x 4 1000 users 65536 Mar 15 06:29 rpcsvc-proto-1.4
-rw-r--r-- 1 root root 149354 Dec 7 08:23 rpcsvc-proto-1.4.tar.gz
接下来先编译安装几个依赖包:
$ cd /opt/protobuf-3.11.4
./autogen.sh && ./configure && make && make install
$ cd /opt/rpcsvc-proto-1.4
./configure && make && make install
2.2 开始编译mysql & mysql-shell
复制执行下面的命令,一次性完成mysql & mysql-shell编译安装:
cd /opt/mysql-8.0.25 && \
rm -fr bld && \
mkdir bld && \
cd bld && \
cmake .. -DBOOST_INCLUDE_DIR=/opt/boost_1_73_0/ \
-DLOCAL_BOOST_DIR=/opt/boost_1_73_0/ \
-DWITH_SSL=system && \
cmake --build . --target mysqlclient; \
cmake --build . --target mysqlxclient && \
cd /opt/mysql-shell-8.0.25-src && \
rm -fr bld && \
mkdir bld && \
cd bld && \
cmake .. \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-shell-8.0.25-16-Linux-glibc2.28-x86_64 \
-DMYSQL_SOURCE_DIR=/opt/mysql-8.0.25 \
-DMYSQL_BUILD_DIR=/opt/mysql-8.0.25/bld/ \
-DHAVE_PYTHON=1 -DWITH_PROTOBUF=bundled && \
make -j64 && make install
编译完成后,会把二进制文件安装到 /usr/local/mysql-shell-8.0.25-16-Linux-glibc2.28-x86_64 目录下。
2.3 利用patchelf修改mysqlsh二进制文件
编译安装完毕后,执行下面的命令,查看 mysqlsh 二进制文件的依赖关系是否都正常,会发现其中有个so依赖文件找不到:
$ cd /usr/local/mysql-shell-8.0.25-16-Linux-glibc2.28-x86_64
$ ldd bin/mysqlsh
linux-vdso.so.1 (0x00007ffd3adce000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f96f73a8000)
...
libprotobuf.so.22 => not found
...
这是因为 libprotobuf.so.22 这个动态库文件在 /usr/local/lib/ 目录下,所以找不到。
这时候,我们可以利用 patchelf
修改 mysqlsh 文件,将依赖路径从绝对路径方式改成相对路径方式:
- 首先,新建一个目录
$ pwd
/usr/local/mysql-shell-8.0.25-16-Linux-glibc2.28-x86_64
$ mkdir -p lib/private
- 将 libprotobuf 动态库文件拷贝过来
$ cp /usr/local/lib/libprotobuf.so.22.0.4 lib/private
- 做一个软链接
$ cd lib/private/
$ ln -s libprotobuf.so.22.0.4 libprotobuf.so.22
- 再利用patchelf修改mysqlsh
cd /usr/local/mysql-shell-8.0.25-16-Linux-glibc2.28-x86_64
$ patchelf --set-rpath '$ORIGIN/../lib/private' ./bin/mysqlsh
$ ldd bin/mysqlsh
cd ../..
[root@mysqlsh-c8 mysql-shell-8.0.25-16-Linux-glibc2.28-x86_64]# ldd bin/mysqlsh
linux-vdso.so.1 (0x00007ffec77e1000)
...
libprotobuf.so.22 => /usr/local/mysql-shell-8.0.25-16-Linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf.so.22 (0x00007f2b8a5db000)
可以看到,mysqlsh 能找到 libprotobuf 这个动态库文件了,也就可以正常使用了。
3. 运行mysql-shell for GreatSQL
如果想要让mysql-shell支持JavaScript语法,需要再编译时加上 libv8,但其难度太大了,我表示直接放弃。。。
虽然不支持js语法,但还是支持Python语法的呀,略有不同而已,下面举几个常举几个常见的例子作为参照对比:
js语法 | py语法 |
---|---|
var c=dba.getCluster() | c=dba.get_cluster() |
c.status() | c.statsu() |
c.setPrimaryInstance() | c.set_primary_instance() |
c.switchToMultiPrimaryMode() | c.switch_to_multi_primary_mode() |
c.rebootClusterFromCompleteOutage() | c.reboot_cluster_from_complete_outage() |
看到了吧,其实也就是语法风格略有不同而已,没什么本质区别。
接下来可以玩转GreatSQL 8.0.25-16的仲裁节点新功能了:
MySQL 172.16.16.12:33060+ ssl Py > c.status()
{
"clusterName": "GreatSQL8027",
"defaultReplicaSet": {
"name": "default",
"primary": "172.16.16.12:3306",
"ssl": "REQUIRED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"172.16.16.10:3306": {
"address": "172.16.16.10:3306",
"memberRole": "ARBITRATOR", <-- 仲裁节点
"mode": "R/O",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.25"
},
"172.16.16.11:3306": {
"address": "172.16.16.11:3306",
"memberRole": "SECONDARY", <-- Secondary节点
"mode": "R/O",
"readReplicas": {},
"replicationLag": "00:00:00.001422",
"role": "HA",
"status": "ONLINE",
"version": "8.0.25"
},
"172.16.16.12:3306": {
"address": "172.16.16.12:3306",
"memberRole": "PRIMARY", <-- Primary节点
"mode": "R/W",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.25"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "172.16.16.12:3306"
}
对了,第一次启动mysqlsh时,可能会有类似下面的提示:
WARNING: Found errors loading plugins, for more details look at the log at: /root/.mysqlsh/mysqlsh.log
只要执行下面的指令安装certifi这个Python模块即可:
$ pip3.6 install --user certifi
好了,开始感受MGR仲裁节点的魅力吧 O(∩_∩)O哈哈~
本文完。
Enjoy GreatSQL :)
《深入浅出MGR》视频课程
戳此小程序即可直达B站
https://www.bilibili.com/medialist/play/1363850082?business=space_collection&business_id=343928&desc=0
文章推荐:
想看更多技术好文,点个“在看”吧!