《童虎学习笔记》10分钟ShardingSphere搭建MySQL读写分离

本文章配套视频 https://www.ixigua.com/7093732988801778206
本专栏全部文章 https://blog.csdn.net/tonghu_note/category_11716546.html
总目录 https://blog.csdn.net/tonghu_note/article/details/124333034

来我的dou音 aa10246666, 看配套视频


一、场景

机器名 版本 IP地址
Master_node1 mysql  Ver 8.0.28 10.211.55.9
Slave_node2 mysql  Ver 8.0.28 10.211.55.4
ShardingSphere_Proxy_node3 5.1.0 10.211.55.6
client_node4用于连接ShardingSphere-Proxy进行测试 mysql  Ver 8.0.28 10.211.55.7

二、主、从点节的准备工作

1、搭建好主从同步

Master_node1是主,Slave_node2是从

2、创建数据库及帐号

在主库Master_node1上操作,创建数据库d1并创建帐号appuser使其具有对d1的全部权限

mysql> create database d1;
Query OK, 1 row affected (0.00 sec)
mysql> create user appuser@'%' identified with mysql_native_password by '111';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all on d1.* to appuser@'%';
Query OK, 0 rows affected (0.00 sec) 


三、配置ShardingSphere-Proxy

1、安装及配置jdk(jdk的版本要依据你cpu来选择,我用的是mac虚出来的虚机)

tar xzf jdk-8u202-linux-arm64-vfp-hflt.tar.gz

mv jdk1.8.0_202 /usr/local/

/etc/profile最后添加如下配置

PATH=/usr/local/jdk1.8.0_202/bin:$PATH

source /etc/profile

2、安装及配置ShardingSphere-Proxy

tar xzf apache-shardingsphere-5.1.0-shardingsphere-proxy-bin.tar.gz

mv apache-shardingsphere-5.1.0-shardingsphere-proxy-bin /usr/local/

编辑服务文件 /usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/conf/server.yaml红色部分, 用于配置虚拟帐号密码,将配置root用户及密码123且允许外网连接,同时权限指定为“所有权限无限制”,sql-show是用于在日志中记录下每条sql及执行的节点,其它默认

#      maxRetries: 3
#      operationTimeoutMilliseconds: 500
#  overwrite: false
#
rules:
  - !AUTHORITY
    users:
      - root@%:123
#      - sharding@:sharding
    provider:
      type: ALL_PRIVILEGES_PERMITTED
#  - !TRANSACTION
#    defaultType: XA
#    providerType: Atomikos

props:
#  max-connections-size-per-query: 1
#  kernel-executor-size: 16  # Infinite by default.
#  proxy-frontend-flush-threshold: 128  # The default value is 128.
#  proxy-opentracing-enabled: false
#  proxy-hint-enabled: false
#  sql-show: false
  sql-show: true

编辑读写分离文件 /usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/conf/config-readwrite-splitting.yaml,其中:

schemaName: readwrite_splitting_db,为虚拟映射的数据库名称

write_ds,为主库也就是写库的相关配置

read_ds_0,为从库也就是读库的相关配置

schemaName: readwrite_splitting_db

dataSources:
  write_ds:
    url: jdbc:mysql://10.211.55.9:3306/d1?serverTimezone=UTC&useSSL=false
    username: appuser
    password: 111
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  read_ds_0:
    url: jdbc:mysql://10.211.55.4:3306/d1?serverTimezone=UTC&useSSL=false
    username: appuser
    password: 111
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
#  read_ds_1:
#    url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_1?serverTimezone=UTC&useSSL=false
#    username: root
#    password:
#    connectionTimeoutMilliseconds: 30000
#    idleTimeoutMilliseconds: 60000
#    maxLifetimeMilliseconds: 1800000
#    maxPoolSize: 50
#    minPoolSize: 1
#
rules:
- !READWRITE_SPLITTING
  dataSources:
    readwrite_ds:
      type: Static
      props:
        write-data-source-name: write_ds
        read-data-source-names: read_ds_0

3、下载mysql jdbc驱动并配置

到官网下载适合你的驱动 https://dev.mysql.com/downloads/connector/j/

我是debian系统,以下是我的安装方式,大家可以根据自已的操作系统环境来安装

root@node3:~# dpkg -i mysql-connector-java_8.0.29-1debian11_all.deb

root@node3:~# cd /usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/lib

root@node3:/usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/lib# cp /usr/share/java/mysql-connector-java-8.0.29.jar . 

4、启动ShardingSphere-Proxy

日志文件的路径是 /usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/logs/stdout.log

root@node3:/usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/bin# ./start.sh 
we find java version: java8, full_version=1.8.0_202
Starting the ShardingSphere-Proxy ...
The classpath is /usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/conf:.:/usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/lib/*:/usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/ext-lib/*
Please check the STDOUT file: /usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/logs/stdout.log

服务默认监听端口是 3307,可以验证一下端口是否启用 ss -tnl

root@node3:/usr/local/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/bin# ss -tnl
State          Recv-Q         Send-Q                 Local Address:Port                 Peer Address:Port         Process         
LISTEN         0              128                          0.0.0.0:22                        0.0.0.0:*                            
LISTEN         0              224                        127.0.0.1:5432                      0.0.0.0:*                            
LISTEN         0              4096                               *:3307                            *:*                            
LISTEN         0              128                             [::]:22                           [::]:*    

四、测试ShardingSphere-Proxy

在client_node4上测试连接ShardingSphere-Proxy

root@node4:~# mysql -h 10.211.55.6 -P 3307 -u root -p123 readwrite_splitting_db
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 8.0.28-ShardingSphere-Proxy 5.1.0 Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

创建一个表t1

mysql> create table t1(id int);
Query OK, 0 rows affected (0.14 sec)

查看日志,确认请求走的是主节点,也就是写库

[INFO ] 2022-05-03 23:21:41.570 [ShardingSphere-Command-1] ShardingSphere-SQL - Logic SQL: create table t1(id int)
[INFO ] 2022-05-03 23:21:41.570 [ShardingSphere-Command-1] ShardingSphere-SQL - SQLStatement: MySQLCreateTableStatement(containsNotExistClause=false)
[INFO ] 2022-05-03 23:21:41.570 [ShardingSphere-Command-1] ShardingSphere-SQL - Actual SQL: write_ds ::: create table t1(id int)

发起一个查询

mysql> select * from t1;
Empty set (0.04 sec)

查看日志,,确认请求走的是从节点,也就是读节点

[INFO ] 2022-05-03 23:23:19.203 [ShardingSphere-Command-2] ShardingSphere-SQL - Logic SQL: select * from t1
[INFO ] 2022-05-03 23:23:19.203 [ShardingSphere-Command-2] ShardingSphere-SQL - SQLStatement: MySQLSelectStatement(table=Optional.empty, limit=Optional.empty, lock=Optional.empty, window=Optional.empty)
[INFO ] 2022-05-03 23:23:19.203 [ShardingSphere-Command-2] ShardingSphere-SQL - Actual SQL: read_ds_0 ::: select * from t1

 五、作业

1、搭建1主多从的架构,并应用ShardingSphere-Proxy

2、搭建双节点ShardingSphere-Proxy,使用高可用方案keepalived等,实现ShardingSphere-Proxy的高可用

你可能感兴趣的:(MySQL超简单新手入门教程,mysql,分布式,proxy模式,sql,数据库)