docker-compose之mysql-router(MGR单主模式)

  一、mysql-router:简单理解就是集群的路由,通过路由来访问具体的数据库集群。

  

  二、本编介绍如何搭建docker版的MGR集群

  

   1、dockerfile

  Dockerfile-cluster

FROM mysql:8.0.19
MAINTAINER xbd
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  Dockerfile-router

FROM mysql/mysql-router:8.0.19
MAINTAINER xbd
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ADD ./config/mysql-shell/mysql-shell-8.0.19.tar.gz /mysql-shell/

  说明:这里添加mysql-shell主要是初始化集群。这个包我是修改过名字的,根据实际执行

  Mysql-shell下载地址:https://dev.mysql.com/downloads/shell/

   2、docker-compose.yaml

  注意:mysql-shell的官方版本只支持单主模式(可能是没有搞清楚咋弄)

  具体的参数意义可以参考

  https://www.cnblogs.com/ll409546297/p/16919642.html

  https://hub.docker.com/r/mysql/mysql-router

version: "2"
services:
  xbd-cluster-1:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-cluster
    image: xbd-cluster-1
    restart: always
    container_name: xbd-cluster-1
    volumes:
      - /var/lib/mysql/xbd-cluster-1:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=root
    privileged: true
    command: ['--server-id=1',
              '--gtid_mode=ON',
              '--enforce_gtid_consistency=ON',
              '--master_info_repository=TABLE',
              '--relay_log_info_repository=TABLE',
              '--binlog_checksum=NONE',
              '--log_slave_updates=ON',
              '--log-bin=xbd-cluster-1-bin',
              '--binlog_format=ROW',
              '--transaction_write_set_extraction=XXHASH64',
              '--plugin_load_add=group_replication.so',
              '--loose-group_replication_group_name=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
              '--loose-group_replication_start_on_boot=off',
              '--loose-group_replication_local_address=xbd-cluster-1:33061',
              '--loose-group_replication_group_seeds=xbd-cluster-1:33061,xbd-cluster-2:33062,xbd-cluster-3:33063',
              '--loose-group_replication_bootstrap_group=off',
              '--loose-group_replication_single_primary_mode=on',
              '--loose-group_replication_enforce_update_everywhere_checks=off',
              '--lower_case_table_names=1',
              '--character-set-server=utf8',
              '--collation-server=utf8_general_ci',
              '--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION']

  xbd-cluster-2:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-cluster
    image: xbd-cluster-2
    restart: always
    container_name: xbd-cluster-2
    volumes:
      - /var/lib/mysql/xbd-cluster-2:/var/lib/mysql
    ports:
      - 3307:3306
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=root
    privileged: true
    command: ['--server-id=2',
              '--gtid_mode=ON',
              '--enforce_gtid_consistency=ON',
              '--master_info_repository=TABLE',
              '--relay_log_info_repository=TABLE',
              '--binlog_checksum=NONE',
              '--log_slave_updates=ON',
              '--log-bin=xbd-cluster-2-bin',
              '--binlog_format=ROW',
              '--transaction_write_set_extraction=XXHASH64',
              '--plugin_load_add=group_replication.so',
              '--loose-group_replication_group_name=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
              '--loose-group_replication_start_on_boot=off',
              '--loose-group_replication_local_address=xbd-cluster-2:33062',
              '--loose-group_replication_group_seeds=xbd-cluster-1:33061,xbd-cluster-2:33062,xbd-cluster-3:33063',
              '--loose-group_replication_bootstrap_group=off',
              '--loose-group_replication_single_primary_mode=on',
              '--loose-group_replication_enforce_update_everywhere_checks=off',
              '--lower_case_table_names=1',
              '--character-set-server=utf8',
              '--collation-server=utf8_general_ci',
              '--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION']

  xbd-cluster-3:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-cluster
    image: xbd-cluster-3
    restart: always
    container_name: xbd-cluster-3
    volumes:
      - /var/lib/mysql/xbd-cluster-3:/var/lib/mysql
    ports:
      - 3308:3306
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=root
    privileged: true
    command: ['--server-id=3',
              '--gtid_mode=ON',
              '--enforce_gtid_consistency=ON',
              '--master_info_repository=TABLE',
              '--relay_log_info_repository=TABLE',
              '--binlog_checksum=NONE',
              '--log_slave_updates=ON',
              '--log-bin=xbd-cluster-3-bin',
              '--binlog_format=ROW',
              '--transaction_write_set_extraction=XXHASH64',
              '--plugin_load_add=group_replication.so',
              '--loose-group_replication_group_name=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
              '--loose-group_replication_start_on_boot=off',
              '--loose-group_replication_local_address=xbd-cluster-3:33063',
              '--loose-group_replication_group_seeds=xbd-cluster-1:33061,xbd-cluster-2:33062,xbd-cluster-3:33063',
              '--loose-group_replication_bootstrap_group=off',
              '--loose-group_replication_single_primary_mode=on',
              '--loose-group_replication_enforce_update_everywhere_checks=off',
              '--lower_case_table_names=1',
              '--character-set-server=utf8',
              '--collation-server=utf8_general_ci',
              '--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION']

  xbd-mysql-router:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-router
    image: xbd-mysql-router
    restart: always
    container_name: xbd-mysql-router
    ports:
      - 6446:6446
      - 6447:6447
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_HOST=xbd-cluster-1
      - MYSQL_PORT=3306
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
      - MYSQL_INNODB_CLUSTER_MEMBERS=3
    privileged: true
    depends_on:
      - xbd-cluster-1
      - xbd-cluster-2
      - xbd-cluster-3

  三、部署

  1、build

  

   2、运行mysql集群

docker-compose up xbd-cluster-1 xbd-cluster-2 xbd-cluster-3 &

  

   3、运行一个临时的mysql-router,目的通过mysql-shell建立集群

docker-compose run xbd-mysql-router /bin/bash

  

./mysql-shell/mysql-shell-8.0.19/bin/mysqlsh

  

  执行以下脚本mysql-shell.script

shell.connect('root@xbd-cluster-1:3306');
var cluster = dba.createCluster('cluster');
cluster.addInstance('xbd-cluster-2:3306');
cluster.addInstance('xbd-cluster-3:3306');
cluster.status();

  

   查看状态

  

   4、启动mysql-router

docker-compose up xbd-mysql-router &

  

   四、数据库测试

SELECT * FROM `performance_schema`.replication_group_members;

  

   到此搭建完成,并且集群具备自动修复能力。

  五、说一下,多主的配置,通过物理代理的形式也是可以达到代理的目的。

  1、搭建MGR多主集群

  https://www.cnblogs.com/ll409546297/p/16919642.html

  2、下载mysql-router

  https://dev.mysql.com/downloads/router/

  3、修改和配置

  

   复制配置文件并修改

cp share/doc/mysqlrouter/sample_mysqlrouter.conf conf/mysqlrouter.conf
[DEFAULT]
logging_folder = /usr/local/mysqlrouter/logs
plugin_folder = /usr/local/mysqlrouter/lib/mysqlrouter
config_folder = /usr/local/mysqlrouter/conf
runtime_folder = /usr/local/mysqlrouter/run
data_folder = /usr/local/mysqlrouter/lib/data
keyring_path = /usr/local/mysqlrouter/lib/keyring-data
master_key_path = /usr/local/mysqlrouterlib/keyring-key

[logger]
level = INFO[routing:cluster]
bind_address = 0.0.0.0
bind_port = 3305
routing_strategy = first-available
mode = read-write
destinations = 192.168.5.14:3306,192.168.5.14:3307,192.168.5.14:3308

  4、启动

/usr/local/mysqlrouter/bin/mysqlrouter -c /usr/local/mysqlrouter/conf/mysqlrouter.conf

  5、说明:这种方式可以代理多主的模式,但是不建议这种方式,还是推荐单主和docker的方式吧,可以配合K8S做容灾处理。

posted @ 2022-12-29 12:39  小不点丶  阅读(1037)  评论(1编辑  收藏  举报