跳过正文
  1. Posts/

MySQL分布式架构

·1499 字·8 分钟·
Kubehan
作者
Kubehan
云原生知识栈:深度解析容器技术、Kubernetes、Istio、DevOps 实践、Prometheus 监控、Envoy 代理、Golang 开发及云原生架构与微服务趋势的专业博客

1. 结构图
#

<div class="image-view" data-width="753" data-height="414">
  <img decoding="async" class="aligncenter" src="https://www.kubehan.cn/wp-content/uploads/2020/03/frc-cdcd166f2eb2eb2040ab6b06b5913c7a.png" alt="13-MySQL分布式架构13-MySQL分布式架构" />
</div>
image.png

2. MyCAT基础架构准备
#

1.环境准备:
#

两台虚拟机 db01 db02
每台创建四个mysql实例:3307 3308 3309 3310

2.节点主从规划
#

箭头指向谁是主库
#
以3307为主库,3309为从库,搭建主从复制关系
10.0.0.51:3307    <----->  10.0.0.52:3307
10.0.0.51:3309    ------>  10.0.0.51:3307
10.0.0.52:3309    ------>  10.0.0.52:3307

以3308为主库,3310为从库,搭建主从复制关系
10.0.0.52:3308  <----->    10.0.0.51:3308
10.0.0.52:3310  ----->     10.0.0.52:3308
10.0.0.51:3310  ----->     10.0.0.51:3308

3.分片规划
#

shard1:
    Master:10.0.0.51:3307
    slave1:10.0.0.51:3309
    Standby Master:10.0.0.52:3307
    slave2:10.0.0.52:3309
shard2:
    Master:10.0.0.52:3308
    slave1:10.0.0.52:3310
    Standby Master:10.0.0.2. MyCAT基础架构准备

4.开始配置
#

shard1
#
  • 1.配置3307库为双主模式

    10.0.0.51:3307 <—–> 10.0.0.52:3307 db02 mysql -S /data/3307/mysql.sock -e “grant replication slave on . to repl@‘10.0.0.%’ identified by ‘123’;” mysql -S /data/3307/mysql.sock -e “grant all on . to root@‘10.0.0.%’ identified by ‘123’ with grant option;” db01 mysql -S /data/3307/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.52’, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3307/mysql.sock -e “start slave;” mysql -S /data/3307/mysql.sock -e “show slave statusG” db02 mysql -S /data/3307/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.51’, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3307/mysql.sock -e “start slave;” mysql -S /data/3307/mysql.sock -e “show slave statusG”

  • 2.配置db01的3309为3307的从库

    10.0.0.51:3309 ——> 10.0.0.51:3307 db01 mysql -S /data/3309/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.51’, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3309/mysql.sock -e “start slave;” mysql -S /data/3309/mysql.sock -e “show slave statusG”

  • 3.配置db02的3309为3307的从库

    10.0.0.52:3309 ——> 10.0.0.52:3307 db02 mysql -S /data/3309/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.52’, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3309/mysql.sock -e “start slave;” mysql -S /data/3309/mysql.sock -e “show slave statusG”

shard2
#
  • 1.配置3308数据库为双主模式

    10.0.0.52:3308 <—–> 10.0.0.51:3308 db01 mysql -S /data/3308/mysql.sock -e “grant replication slave on . to repl@‘10.0.0.%’ identified by ‘123’;” mysql -S /data/3308/mysql.sock -e “grant all on . to root@‘10.0.0.%’ identified by ‘123’ with grant option;” db02 mysql -S /data/3308/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.51’, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3308/mysql.sock -e “start slave;” mysql -S /data/3308/mysql.sock -e “show slave statusG” db01 mysql -S /data/3308/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.52’, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3308/mysql.sock -e “start slave;” mysql -S /data/3308/mysql.sock -e “show slave statusG”

  • 2.配置db02的3310为3308的从库

    10.0.0.52:3310 —–> 10.0.0.52:3308 db02 mysql -S /data/3310/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.52’, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3310/mysql.sock -e “start slave;” mysql -S /data/3310/mysql.sock -e “show slave statusG”

  • 3.配置db01的3310为3308的从库

    10.0.0.51:3310 —–> 10.0.0.51:3308 db01 mysql -S /data/3310/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.51’, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘123’;” mysql -S /data/3310/mysql.sock -e “start slave;” mysql -S /data/3310/mysql.sock -e “show slave statusG”

5.检测主从状态
#

mysql -S /data/3307/mysql.sock -e "show slave statusG"|grep Yes
mysql -S /data/3308/mysql.sock -e "show slave statusG"|grep Yes
mysql -S /data/3309/mysql.sock -e "show slave statusG"|grep Yes
mysql -S /data/3310/mysql.sock -e "show slave statusG"|grep Yes

注:如果中间出现错误,执行以下命令,重新配置主从关系
mysql -S /data/3307/mysql.sock -e “stop slave; reset slave all;”
mysql -S /data/3308/mysql.sock -e “stop slave; reset slave all;”
mysql -S /data/3309/mysql.sock -e “stop slave; reset slave all;”
mysql -S /data/3310/mysql.sock -e “stop slave; reset slave all;”

3. MyCAT安装
#

1.预先安装Java运行环境
#
yum install -y java
2.下载
#

软件包名称: Mycat-server-xxxxx.linux.tar.gz
下载地址: http://mycat.io/

3.解压文件
#
tar xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
mv mycat/ /usr/local/
4.软件目录结构
#
[root@db01 mycat]# ls
bin  catlet  conf  lib  logs  version.txt
5.启动和连接
#
  • 配置环境变量

    vim /etc/profile export PATH=/usr/local/mycat/bin:$PATH source /etc/profile

  • 启动

    mycat start

  • 连接mycat

    mysql -uroot -p123456 -h 127.0.0.1 -P8066

4. 配置文件介绍
#

1.logs目录
#
wrapper.log       ---->mycat启动日志
mycat.log         ---->mycat详细工作日志
2.conf目录
#
schema.xml      
主配置文件(读写分离、高可用、分布式策略定制、节点控制)

server.xml
mycat软件本身相关的配置

rule.xml 
分片规则配置文件,记录分片规则列表、使用方法等

5.应用前环境准备
#

1.用户创建及数据库导入
#
db01:
mysql -S /data/3307/mysql.sock 
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql

mysql -S /data/3308/mysql.sock 
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
2.配置文件处理
#
[root@db01 conf]# vim schema.xml 
<schema name="TESTDB"  dataNode="sh1"> 
</schema>  
        <dataNode name="sh1" dataHost="oldguo1" database= "world" />    
        <dataHost name="oldguo1"
        <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123"> 
                        <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" /> 
        </writeHost> 
        </dataHost>         
</mycat:schema>

6. 读写分离结构配置
#

  • 编写读写分离配置文件

    vim schema.xml

    <mycat:schema xmlns:mycat=“http://io.mycat/">


    select user()

    </mycat:schema>

7.测试一主一从的读写分离
#

1.重启mycat
#
mycat restart
2.读写分离测试
#
mysql -uroot -p -h 127.0.0.1 -P8066
select @@server_id;

begin;
select @@server_id;
commit;

总结:
以上案例实现了1主1从的读写分离功能,写操作落到主库,读操作落到从库.如果主库宕机,从库不能在继续提供服务了

8. 配置读写分离及高可用
#

1.编写读写分离及高可用配置文件
#
[root@db01 conf]# vim schema.xml
<?xml version="1.0"?>  
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">  
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> 
</schema>  
    <dataNode name="sh1" dataHost="oldguo1" database= "world" />  
    <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1"> 
        <heartbeat>select user()</heartbeat>  
    <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123"> 
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" /> 
    </writeHost> 
    <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123"> 
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123" /> 
    </writeHost>        
    </dataHost>  
</mycat:schema>
2.配置文件介绍
#

真正的 writehost:负责写操作的writehost
standby writeHost :和readhost一样,只提供读服务
当写节点宕机后,后面跟的readhost也不提供服务,这时候standby的writehost就提供写服务,
后面跟的readhost提供读服务

3.测试:
#
mycat restart
mysql -uroot -p123456 -h 127.0.0.1 -P 8066

读写分离测试
mysql -uroot -p -h 127.0.0.1 -P8066
select @@server_id;

begin;
select @@server_id;
commit;

对db01 3307节点进行关闭和启动,测试读写操作

9. 配置中的属性介绍:
#

1.balance属性
#

  • 负载均衡类型,目前的取值有3种:
    1.balance=“0”, 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上
    2.balance=“1”,全部的readHost与standby writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡
    3.balance=“2”,所有读操作都随机的在writeHost、readhost上分发

2.writeType属性
#

  • 负载均衡类型,目前的取值有2种:
    1.writeType=“0”, 所有写操作发送到配置的第一个writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为主,切换记录在配置文件中:dnindex.properties
    2.writeType=“1”,所有写操作都随机的发送到配置的writeHost,但不推荐使用

3.switchType属性
#

-1   表示不自动切换 
 1   默认值,自动切换 
 2   基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status
  • datahost其他配置

    maxCon=“1000”:最大的并发连接数 minCon=“10” :mycat在启动之后,会在后端节点上自动开启的连接线程

    tempReadHostAvailable=“1” 这个一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时 select user() 监测心跳

10.垂直分表
#

  • 编写配置文件

    vim schema.xml

    <mycat:schema xmlns:mycat=“http://io.mycat/">

    <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123">
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123" />
    </writeHost>
    </dataHost>
    
    <dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.0.51:3308" user="root" password="123">
            <readHost host="db2" url="10.0.0.51:3310" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.0.52:3308" user="root" password="123">
            <readHost host="db4" url="10.0.0.52:3310" user="root" password="123" />
    </writeHost>
    </dataHost>
    

    </mycat:schema>

  • 创建测试库和表:

    mysql -S /data/3307/mysql.sock -e “create database taobao charset utf8;” mysql -S /data/3308/mysql.sock -e “create database taobao charset utf8;” mysql -S /data/3307/mysql.sock -e “use taobao;create table user(id int,name varchar(20))”; mysql -S /data/3308/mysql.sock -e “use taobao;create table order_t(id int,name varchar(20))”

  • 重启测试 :

    mycat restart mysql -uroot -p -h 127.0.0.1 -P8066

    use TESTDB ; mysql> insert into order_t(id,name) values(1,‘c’); mysql> insert into order_t(id,name) values(2,’d’); mysql> insert into user(id,name) values(1,‘a’); mysql> insert into user(id,name) values(2,‘b’);

  • 分别登陆后端主节点查看数据

    mysql -S /data/3307/mysql.sock -e “select * from taobao.user” mysql -S /data/3308/mysql.sock -e “select * from taobao.order_t”

  • 11. MyCAT核心特性——分片(水平拆分)
    #

    • 分片:对一个"bigtable”,比如说t3表
      (1)行数非常多,800w
      (2)访问非常频繁
    • 分片的目的:
      (1)将大数据量进行分布存储
      (2)提供均衡的访问路由
    • 分片策略:
      范围 range
      取模 mod
      枚举
      哈希 hash
      时间 流水

    优化关联查询
    全局表
    ER分片

    12 .范围分片
    #

    比如说t3表
    (1)行数非常多,2000w(1-1000w:sh1 1000w-2000w:sh2)
    (2)访问非常频繁,用户访问较离散

    1.修改配置文件
    #

    <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123">
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123" />
    </writeHost>
    </dataHost>
    
    <dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.0.51:3308" user="root" password="123">
            <readHost host="db2" url="10.0.0.51:3310" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.0.52:3308" user="root" password="123">
            <readHost host="db4" url="10.0.0.52:3310" user="root" password="123" />
    </writeHost>
    </dataHost>
    

    </mycat:schema>

  • rule文件

    vim rule.xml id rang-long

    autopartition-long.txt

  • autopartition-long.txt文件

    vim autopartition-long.txt 0-10=0 10-20=1

  • 2.创建测试
    #

    mysql -S /data/3307/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);"
    
    mysql -S /data/3308/mysql.sock  -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);"
    

    3.测试
    #

    重启mycat
    mycat restart
    mysql -uroot -p123456 -h 127.0.0.1 -P 8066
    insert into t3(id,name) values(1,'a');
    insert into t3(id,name) values(2,'b');
    insert into t3(id,name) values(3,'c');
    insert into t3(id,name) values(4,'d');
    insert into t3(id,name) values(11,'aa');
    insert into t3(id,name) values(12,'bb');
    insert into t3(id,name) values(13,'cc');
    insert into t3(id,name) values(14,'dd');
    insert into t3(id,name) values(10,'ddd');
    

    分别登陆后端主节点查看数据

    mysql -S /data/3307/mysql.sock -e "select * from taobao.t3"
    mysql -S /data/3308/mysql.sock -e "select * from taobao.t3"
    

    13. 取模分片(mod-long):
    #

    取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点

    修改配置文件
    #

    • schema配置

      vim schema.xml

    rule配置

    vim rule.xml
    <property name="count">2</property>
    

    准备测试环境
    #

    • 创建测试表

      mysql -S /data/3307/mysql.sock -e “use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);” mysql -S /data/3308/mysql.sock -e “use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);”

    • 重启mycat

      mycat restart

    • 测试

      mysql -uroot -p123456 -h10.0.0.52 -P8066

      use TESTDB insert into t4(id,name) values(1,‘a’); insert into t4(id,name) values(2,‘b’); insert into t4(id,name) values(3,‘c’); insert into t4(id,name) values(4,’d’);

    • 分别登录后端节点查询数据

      mysql -S /data/3307/mysql.sock use taobao select * from t4;

      mysql -S /data/3308/mysql.sock use taobao select * from t4;

    14. 枚举分片
    #

    t5 表
    id name telnum
    1 bj 1212
    2 sh 22222
    3 bj 3333
    4 sh 44444
    5 bj 5555

    sharding-by-intfile

    修改配置文件
    #

    • schema配置

      vim schema.xml

  • rule配置

    vim rule.xml name hash-int

    partition-hash-int.txt 1 0
  • partition-hash-int.txt 配置

    bj=0 sh=1 DEFAULT_NODE=1

  • columns 标识将要分片的表字段,algorithm 分片函数, 其中分片函数配置中,mapFile标识配置文件名称

    2.准备测试环境
    #

    • 准备数据

      mysql -S /data/3307/mysql.sock -e “use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);”

      mysql -S /data/3308/mysql.sock -e “use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);”

    • 重启mycat

      mycat restart mysql -uroot -p123456 -h10.0.0.51 -P8066 use TESTDB insert into t5(id,name) values(1,‘bj’); insert into t5(id,name) values(2,‘sh’); insert into t5(id,name) values(3,‘bj’); insert into t5(id,name) values(4,‘sh’); insert into t5(id,name) values(5,’tj’);

    15 . Mycat全局表
    #

    a b c d
    join
    t

    select t1.name ,t.x from t1
    join t
    select t2.name ,t.x from t2
    join t
    select t3.name ,t.x from t3
    join t

    使用场景:
    如果你的业务中有些数据类似于数据字典,比如配置文件的配置,
    常用业务的配置或者数据量不大很少变动的表,这些表往往不是特别大,
    而且大部分的业务场景都会用到,那么这种表适合于Mycat全局表,无须对数据进行切分,
    要在所有的分片上保存一份数据即可,Mycat 在Join操作中,业务表与全局表进行Join聚合会优先选择相同分片内的全局表join,
    避免跨库Join,在进行数据插入操作时,mycat将把数据分发到全局表对应的所有分片执行,在进行数据读取时候将会随机获取一个节点读取数据。

    1.修改配置
    #

    vim schema.xml 
    <table name="t_area" primaryKey="id"  type="global" dataNode="sh1,sh2" /> 
    

    2.后端数据准备
    #

    mysql -S /data/3307/mysql.sock 
    use taobao
    create table t_area (id int not null primary key auto_increment,name varchar(20) not null);
    
    mysql -S /data/3308/mysql.sock 
    use taobao
    create table t_area  (id int not null primary key auto_increment,name varchar(20) not null);
    

    3.重启mycat
    #

    mycat restart 
    

    4.测试
    #

    mysql -uroot -p123456 -h10.0.0.52 -P8066
    
    use TESTDB
    insert into t_area(id,name) values(1,'a');
    insert into t_area(id,name) values(2,'b');
    insert into t_area(id,name) values(3,'c');
    insert into t_area(id,name) values(4,'d');
    

    16. E-R分片
    #

    A
    join
    B
    为了防止跨分片join,可以使用E-R模式
    A join B
    on a.xx=b.yy
    join C
    on A.id=C.id