容器化安装


本文详细介绍如何在Docker中部署GreatSQL,并且构建一个MGR集群。

1. 安装Docker

直接用yum/dnf安装docker,非常省事

  1. $ yum install -y docker

之后启动 docker 服务,并设置开机自启动

  1. $ systemctl enable docker
  2. $ systemctl start docker

2. 拉取GreatSQL镜像,并创建容器

2.1 拉取镜像

拉取GreatSQL官方镜像

  1. $ docker pull greatsql/greatsql
  2. docker pull greatsql/greatsql
  3. Using default tag: latest
  4. Trying to pull repository docker.io/greatsql/greatsql ...
  5. latest: Pulling from docker.io/greatsql/greatsql
  6. ...
  7. Digest: sha256:03969daaaaaeb0f51dde0c9e92ef327302607cdde3afbe5c2b071098000c52c1
  8. Status: Downloaded newer image for greatsql/greatsql:latest
  9. docker.io/greatsql/greatsql:latest

检查是否成功

  1. $ docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. docker.io/greatsql/greatsql latest a930afc72d88 8 weeks ago 923 MB

2.2 创建新容器

之后,就可以直接创建一个新的容器了,先用常规方式

  1. $ docker run -d --name greatsql --hostname=greatsql -e MYSQL_ALLOW_EMPTY_PASSWORD=1 greatsql/greatsql

容器的命名和容器内主机名均为greatsql。

确认容器状态:

  1. $ docker ps -a | grep greatsql
  2. ...
  3. 4f351e22cea9 greatsql/greatsql "/docker-entrypoint.…" About a minute ago Up About a minute 3306/tcp, 33060-33061/tcp greatsql
  4. ...

看到容器状态是Up的,表示已正常启动了。

2.3 容器管理

进入容器查看

  1. $ docker exec -it greatsql /bin/bash
  2. [root@greatsql /]# mysql
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 12
  5. Server version: 8.0.25-16 GreatSQL (GPL), Release 16, Revision 8bb0e5af297
  6. ...
  7. Server version: 8.0.25-16 GreatSQL (GPL), Release 16, Revision 8bb0e5af297
  8. ...
  9. Threads: 2 Questions: 18 Slow queries: 0 Opens: 119 Flush tables: 3 Open tables: 36 Queries per second avg: 0.243

看到容器已经完成初始化,并且可以直接无密码登入。

3. 利用Docker-compose创建Docker容器并构建MGR集群

手工管理Docker比较麻烦,建议采用 docker-compose ,它可以更方便的管理docker容器。

先用yum安装docker-compose,并确认版本号

  1. $ yum install -y docker-compose
  2. $ docker-compose --version
  3. docker-compose version 1.29.2, build 5becea4c

编辑一个yaml文件,准备部署包含仲裁节点的三节点MGR集群:

  1. $ mkdir -p /data/docker-compose
  2. $ cat /data/docker-compose/mgr-3nodes.yml
  3. version: '2'
  4. services:
  5. mgr2:
  6. image: greatsql/greatsql #指定镜像
  7. container_name: mgr2 #设定容器名字
  8. hostname: mgr2 #设定容器中的主机名
  9. networks: #指定容器使用哪个专用网络
  10. mgr_net:
  11. ipv4_address: 172.18.0.2 #设置容器使用固定IP地址,避免重启后IP变化
  12. restart: unless-stopped #设定重启策略
  13. environment: #设置多个环境变量
  14. TZ: Asia/Shanghai #时区
  15. MYSQL_ALLOW_EMPTY_PASSWORD: 1 #允许root账户空密码
  16. MYSQL_INIT_MGR: 1 #初始化MGR集群
  17. MYSQL_MGR_LOCAL: '172.18.0.2:33061' #当前MGR节点的local_address
  18. MYSQL_MGR_SEEDS: '172.18.0.2:33061,172.18.0.3:33061,172.18.0.4:33061' #MGR集群seeds
  19. MYSQL_MGR_START_AS_PRIMARY: 1 #指定当前MGR节点为Primary角色
  20. MYSQL_MGR_ARBITRATOR: 0
  21. #MYSQL_MGR_VIEWID: "aaaaaaaa-bbbb-bbbb-aaaa-aaaaaaaaaaa1"
  22. mgr3:
  23. image: greatsql/greatsql
  24. container_name: mgr3
  25. hostname: mgr3
  26. networks:
  27. mgr_net:
  28. ipv4_address: 172.18.0.3
  29. restart: unless-stopped
  30. depends_on:
  31. - "mgr2"
  32. environment:
  33. TZ: Asia/Shanghai
  34. MYSQL_ALLOW_EMPTY_PASSWORD: 1
  35. MYSQL_INIT_MGR: 1
  36. MYSQL_MGR_LOCAL: '172.18.0.3:33061'
  37. MYSQL_MGR_SEEDS: '172.18.0.2:33061,172.18.0.3:33061,172.18.0.4:33061'
  38. MYSQL_MGR_START_AS_PRIMARY: 0
  39. MYSQL_MGR_ARBITRATOR: 0 #既非Primary,也非Arbitrator,那么就是Secondary角色了
  40. #MYSQL_MGR_VIEWID: "aaaaaaaa-bbbb-bbbb-aaaa-aaaaaaaaaaa1"
  41. mgr4:
  42. image: greatsql/greatsql
  43. container_name: mgr4
  44. hostname: mgr4
  45. networks:
  46. mgr_net:
  47. ipv4_address: 172.18.0.4
  48. restart: unless-stopped
  49. depends_on:
  50. - "mgr3"
  51. environment:
  52. TZ: Asia/Shanghai
  53. MYSQL_ALLOW_EMPTY_PASSWORD: 1
  54. MYSQL_INIT_MGR: 1
  55. MYSQL_MGR_LOCAL: '172.18.0.4:33061'
  56. MYSQL_MGR_SEEDS: '172.18.0.2:33061,172.18.0.3:33061,172.18.0.4:33061'
  57. MYSQL_MGR_START_AS_PRIMARY: 0
  58. MYSQL_MGR_ARBITRATOR: 1 #指定当前MGR节点为Arbitrator角色,此时不能同时指定其为Primary/Secondary角色
  59. #MYSQL_MGR_VIEWID: "aaaaaaaa-bbbb-bbbb-aaaa-aaaaaaaaaaa1"
  60. networks:
  61. mgr_net: #创建独立MGR专属网络
  62. ipam:
  63. config:
  64. - subnet: 172.18.0.0/24

关于GreatSQL容器启动选项说明,详见GreatSQL For Docker文档容器化安装 - 图1 (opens new window)

如果不想要仲裁节点,则可以修改最后一个节点的属性 MYSQL_MGR_ARBITRATOR: 0 就行了。

另外,利用 docker-compose 方式暂时无法构建多主模式的MGR集群,需要手动部署。

启动三个实例:

  1. $ docker-compose -f /data/docker-compose/mgr-3nodes.yml up -d
  2. Creating network "docker-compose_mgr_net" with the default driver
  3. Creating mgr2 ... done
  4. Creating mgr3 ... done
  5. Creating mgr4 ... done

查看运行状态:

  1. $ docker-compose -f /data/docker-compose/mgr-3nodes.yml up -d
  2. Name Command State Ports
  3. ----------------------------------------------------------------------------
  4. mgr2 /docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp, 33061/tcp
  5. mgr3 /docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp, 33061/tcp
  6. mgr4 /docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp, 33061/tcp

容器刚创建完还需要过一小段时间才能完成GreatSQL的初始化以及MGR集群自动构建,视服务器性能不同而定,一般需要30秒至四分钟左右。

进入被选为PRIMARY节点的容器mgr2,查看MGR集群状态:

  1. $ docker exec -it mgr2 bash
  2. [root@mgr2 /]# mysql
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 30
  5. Server version: 8.0.25-16 GreatSQL (GPL), Release 16, Revision 8bb0e5af297
  6. ...
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. (Tue Jul 12 14:28:00 2022)[root@GreatSQL][(none)]>\s
  9. --------------
  10. mysql Ver 8.0.25-16 for Linux on x86_64 (GreatSQL (GPL), Release 16, Revision 8bb0e5af297)
  11. ...
  12. Uptime: 1 min 38 sec
  13. Threads: 11 Questions: 52 Slow queries: 0 Opens: 145 Flush tables: 3 Open tables: 62 Queries per second avg: 0.530
  14. --------------
  15. (Tue Jul 12 14:28:05 2022)[root@GreatSQL][(none)]>select * from performance_schema.replication_group_members;
  16. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+
  17. | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
  18. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+
  19. | group_replication_applier | 396465ad-01ab-11ed-9c1a-0242ac120002 | 172.18.0.2 | 3306 | ONLINE | PRIMARY | 8.0.25 |
  20. | group_replication_applier | 3a4eabbd-01ab-11ed-a7ea-0242ac120003 | 172.18.0.3 | 3306 | ONLINE | SECONDARY | 8.0.25 |
  21. | group_replication_applier | 3c707b56-01ab-11ed-969b-0242ac120004 | 172.18.0.4 | 3306 | ONLINE | ARBITRATOR | 8.0.25 |
  22. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+
  23. 3 rows in set (0.01 sec)

可以看到,包含仲裁节点的三节点MGR集群已自动构建完毕。

如果想在Docker环境下逐个节点手动构建MGR集群,可以参考这篇文档在Docker中部署GreatSQL并构建MGR集群

问题反馈

联系我们

扫码关注微信公众号

输入图片说明