shardingsphere配置读写分离集群(1主2从结构)

发布时间 2023-07-18 22:47:42作者: zhengzai7

第一章、shardingsphere的安装

1、使用ftp将离线安装包放到/opt

2、进入opt目录,解压,移动到/usr/local,重命名

cd /opt
tar -xzvf mysql-5.7,29-linux-glibc2.12-x86_64.tar.gz
mv apache-shardingsphere-4.1.1-sharding-proxy-bin /usr/local
cd /usr/local
mv apache-shardingsphere-4.1.1-sharding-proxy-bin shardingsphere

3、安装mysql连接器

tar -xzvf mysql-connector-java-5.1.48.tar.gz
mv /usr/local/shardingphere/lib

4、启动和停止

cd shrdingsphere/bin
./start.sh
./stop.sh

第二章、配置读写分离多用户集群

1、结构

2、config-master_slave.yaml

cd ..
cd shardingsphere/conf
vi config-master_slave.yaml

配置如下内容:

schemaName:demo #数据库名 demo
dataSources:
    master_ds: # 主库配置
        url:jdbc:mysql://主库ip:3306/demo?serverTimezone=UTC&useSSL=false
        username:demo # 数据库用户,
        password:demo # 数据库密码
        connectionTimeoutMilliseconds:30000
        idleTimeoutMillisecond:60000
        maxLifetimeMillliseconds:1800000
        maxPoolSize:50
    slave_ds_0: # 从库1配置
        url:jdbc:mysql://从库ip1:3306/demo?serverTimezone=UTC&useSSL=false
        username:demo # 数据库用户,
        password:demo # 数据库密码
        connectionTimeoutMilliseconds:30000
        idleTimeoutMillisecond:60000
        maxLifetimeMillliseconds:1800000
        maxPoolSize:50
     slave_ds_1: # 从库2配置
        url:jdbc:mysql://从库ip2:3306/demo?serverTimezone=UTC&useSSL=false
        username:demo # 数据库用户,
        password:demo # 数据库密码
        connectionTimeoutMilliseconds:30000
        idleTimeoutMillisecond:60000
        maxLifetimeMillliseconds:1800000
        maxPoolSize:50

masterSlaveRule:
    name:ms_ds
    masterDataSourceName:master_ds
    slaveDataSourceNames:
        - slave_ds_0
        - slave_ds_1

3、配置数据库2

在另外复制一个config-master_slave.yaml配置文件,改个名称即可。

cp config-master_slave.yaml config-master_slave-2.yaml
vi config-master_slave-2.yaml

按如下配置

schemaName:demo #数据库名 demo
dataSources:
    master_ds: # 主库配置
        url:jdbc:mysql://主库ip:3306/demo1?serverTimezone=UTC&useSSL=false
        username:demo # 数据库用户,
        password:demo # 数据库密码
        connectionTimeoutMilliseconds:30000
        idleTimeoutMillisecond:60000
        maxLifetimeMillliseconds:1800000
        maxPoolSize:50
    slave_ds_0: # 从库1配置
        url:jdbc:mysql://从库ip1:3306/demo1?serverTimezone=UTC&useSSL=false
        username:demo # 数据库用户,
        password:demo # 数据库密码
        connectionTimeoutMilliseconds:30000
        idleTimeoutMillisecond:60000
        maxLifetimeMillliseconds:1800000
        maxPoolSize:50
     slave_ds_1: # 从库2配置
        url:jdbc:mysql://从库ip2:3306/demo1?serverTimezone=UTC&useSSL=false
        username:demo # 数据库用户,
        password:demo # 数据库密码
        connectionTimeoutMilliseconds:30000
        idleTimeoutMillisecond:60000
        maxLifetimeMillliseconds:1800000
        maxPoolSize:50

masterSlaveRule:
    name:ms_ds
    masterDataSourceName:master_ds
    slaveDataSourceNames:
        - slave_ds_0
        - slave_ds_1

3、server.xml

在server.xml中配置如下内容

authentication:
    user:
        demo: # demo数据库
            password:demo
            authorizedSchemas:demo
        demo1: # demo1数据库
            password:demo1
            authorizedSchemas:demo1
    sharding:
        password:sharding
        authorizedSchemas:sharding_db

props:
    executor.size:16
    sql.show:false

4、重启

cd shrdingsphere/bin
./start.sh
./stop.sh