docker 安装sharding proxy 并做 mysql 数据分片、主从服务

文章目录

      • 2、docker 安装 sharding proxy
      • 2、配置

前言:此文承接上文的mysql 主从服务配置
https://blog.csdn.net/YL3126/article/details/122170589
docker镜像地址:
https://hub.docker.com/r/apache/sharding-proxy

2、docker 安装 sharding proxy

下载镜像【这里重点说明一下,一定要使用我这里试验过的 4.1.0 版本 和 相关的配置项 ,随便从网上去摘抄 配置文件 你会起不来服务的!!!】

docker pull apache/sharding-proxy:4.1.0

安装容器并运行

docker run --name=sharding_proxy_4_1_0 -d -v /mydata/sharding_proxy/conf:/opt/sharding-proxy/conf -v /mydata/sharding_proxy/ext-lib:/opt/sharding-proxy/ext-lib --env PORT=3408 -p13408:3408 apache/sharding-proxy:4.1.0

运行后我们查看一下宿主机的映射目录:嗯,没啥问题
docker 安装sharding proxy 并做 mysql 数据分片、主从服务_第1张图片
自行下载MYSQ 的驱动包,放到 映射目录中,如我的:
下载地址:https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.47.tar.gz
docker 安装sharding proxy 并做 mysql 数据分片、主从服务_第2张图片

下面开始改配置文件(不要随便去从网上摘抄,根本起不来):
(可以直接参考官网手册):https://shardingsphere.apache.org/document/4.1.0/en/manual/sharding-proxy/configuration/

2、配置

配置数据分片+读写分离

config-sharding.yaml

schemaName: sharding_db
dataSources:
  ds0:
    username: root
    password: root
    url: jdbc:mysql://192.168.56.10:3307/ds0?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
  ds0_slave0:
    username: root
    password: root
    url: jdbc:mysql://192.168.56.10:3317/ds0?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
  ds1:
    username: root
    password: root
    url: jdbc:mysql://192.168.56.10:3307/ds1?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
  ds1_slave0:
    username: root
    password: root
    url: jdbc:mysql://192.168.56.10:3317/ds1?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
#分库分表********************************************
shardingRule:
  tables:
    t_order:
      # 这里的 ms_ds  是跟 下面读写分离 对应的:(官方文档写的很清楚:)https://shardingsphere.apache.org/document/4.1.0/en/manual/sharding-proxy/configuration/
      actualDataNodes: ms_ds${0..1}.t_order${0..1}
      databaseStrategy:
        inline:
          shardingColumn: user_id
          algorithmExpression: ms_ds${user_id % 2}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_id

    t_order_item:
      actualDataNodes: ms_ds${0..1}.t_order_item${0..1}
      databaseStrategy:
        inline:
          shardingColumn: user_id
          algorithmExpression: ms_ds${user_id % 2}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_item${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_item_id
  bindingTables:
    - t_order,t_order_item
  broadcastTables:
    - t_config
  defaultDataSourceName: ds0
  defaultTableStrategy:
    none:
#  defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator 这货加上就报错!!
  #读写分离********************************************
  masterSlaveRules:
    ms_ds0:
      masterDataSourceName: ds0
      slaveDataSourceNames:
        - ds0_slave0
      loadBalanceAlgorithmType: ROUND_ROBIN
    ms_ds1:
      masterDataSourceName: ds1
      slaveDataSourceNames:
        - ds1_slave0
      loadBalanceAlgorithmType: ROUND_ROBIN

由于此文承接我们前言里提到的 mysql 主从复制,所以这里我们直接在master 中创建库和表(提示:必须使用语句创建):

先创建数据库:gulimall_ums、gulimall_oms

CREATE DATABASE `ds0` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE `ds1` CHARACTER SET utf8 COLLATE utf8_general_ci;

再分表创建表:t_order0、t_order1、t_order_item0、t_order_item1

CREATE TABLE `t_order0` (
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `t_order_item0` (
`order_item_id` bigint(20) NOT NULL,
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`content` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`status` varchar(50) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`order_item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE `t_order1` (
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `t_order_item1` (
`order_item_id` bigint(20) NOT NULL,
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`content` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`status` varchar(50) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`order_item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

好了之后再配置 server.yaml 文件

authentication:
  users:
    root:
      password: root
    sharding:
      password: sharding
      authorizedSchemas: sharding_db

重启容器:

docker restart sharding_proxy_4_1_0

查看容器日志:

docker logs sharding_proxy_4_1_0

连接测试:
docker 安装sharding proxy 并做 mysql 数据分片、主从服务_第3张图片
不过使用navicat 工具无法打开数据库!!!
docker 安装sharding proxy 并做 mysql 数据分片、主从服务_第4张图片

你可能感兴趣的:(docker,mysql,docker)