码迷,mamicode.com
首页 > 数据库 > 详细

MySQL-MyCAT分布式架构

时间:2020-05-30 18:24:44      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:箭头   service   db2   driver   介绍   val   null   模式   ecs   

MySQL-MyCAT分布式架构

MyCAT基础架构准备

主机名 ip地址
sql01 10.0.1.110
sql02 10.0.1.120
#环境准备:
两台虚拟机 sql01 sql02
每台创建四个mysql实例:3307 3308 3309 3310

#创建相关目录初始化数据
[root@sql01 ~]# mkdir /data/33{07..10}/data -p
[root@sql01 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3307/data --basedir=/application/mysql
[root@sql01 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
[root@sql01 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3309/data --basedir=/application/mysql
[root@sql01 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3310/data --basedir=/application/mysql

#准备配置文件
[root@sql01 ~]# cat >/data/3307/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
port=3307
log-error=/data/3307/mysql.log
log_bin=/data/3307/mysql-bin
binlog_format=row
skip-name-resolve
server-id=7
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
[root@sql01 ~]# cat >/data/3308/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
port=3308
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
log_bin=/data/3308/mysql-bin
binlog_format=row
skip-name-resolve
server-id=8
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
[root@sql01 ~]# cat >/data/3309/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
port=3309
log-error=/data/3309/mysql.log
log_bin=/data/3309/mysql-bin
binlog_format=row
skip-name-resolve
server-id=9
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
[root@sql01 ~]# cat >/data/3310/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3310/data
socket=/data/3310/mysql.sock
port=3310
log-error=/data/3310/mysql.log
log_bin=/data/3310/mysql-bin
binlog_format=row
skip-name-resolve
server-id=10
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF

#准备启动脚本
[root@sql01 ~]# cat >/etc/systemd/system/mysqld3307.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
EOF
[root@sql01 ~]# cat >/etc/systemd/system/mysqld3308.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000
EOF
[root@sql01 ~]# 
[root@sql01 ~]# cat >/etc/systemd/system/mysqld3309.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
LimitNOFILE = 5000
EOF
[root@sql01 ~]# cat >/etc/systemd/system/mysqld3310.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF
[root@sql01 ~]# 

#修改权限,启动多实例
[root@sql01 ~]# chown -R mysql.mysql /data/*
[root@sql01 ~]# systemctl start mysqld3307
[root@sql01 ~]# systemctl start mysqld3308
[root@sql01 ~]# systemctl start mysqld3309
[root@sql01 ~]# systemctl start mysqld3310
[root@sql01 ~]# mysql -S /data/3307/mysql.sock -e "show variables like ‘server_id‘"
ke ‘server_id‘"+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 7     |
+---------------+-------+
[root@sql01 ~]# mysql -S /data/3308/mysql.sock -e "show variables like ‘server_id‘"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 8     |
+---------------+-------+
[root@sql01 ~]# mysql -S /data/3309/mysql.sock -e "show variables like ‘server_id‘"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 9     |
+---------------+-------+
[root@sql01 ~]# mysql -S /data/3310/mysql.sock -e "show variables like ‘server_id‘"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 10    |
+---------------+-------+
[root@sql01 ~]# 

节点主从规划

技术图片

箭头指向谁是主库
    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
	
	

    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
shard1:
    Master:10.0.1.110:3307
    slave1:10.0.1.110:3309
    Standby Master:10.0.1.120:3307
    slave2:10.0.1.120:3309
shard2:
    Master:10.0.1.120:3308
    slave1:10.0.1.120:3310
    Standby Master:10.0.1.110:3308
    slave2:10.0.1.110:3310

#开始配置
#第一组四节点结构
# 10.0.1.110:3307 <-----> 10.0.1.120:3307
#sql02
[root@sql02 ~]# mysql  -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@‘10.0.1.%‘ identified by ‘123‘;"
[root@sql02 ~]# mysql  -S /data/3307/mysql.sock -e "grant all  on *.* to root@‘10.0.1.%‘ identified by ‘123‘  with grant option;"

#sql01
[root@sql01 ~]# mysql  -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.120‘, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql01 ~]# mysql  -S /data/3307/mysql.sock -e "start slave;"
[root@sql01 ~]# mysql  -S /data/3307/mysql.sock -e "show slave status\G"

#sql02
[root@sql02 ~]# mysql  -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.110‘, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql02 ~]# mysql  -S /data/3307/mysql.sock -e "start slave;"
[root@sql02 ~]# mysql  -S /data/3307/mysql.sock -e "show slave status\G"

# 10.0.1.110:3309 ------> 10.0.1.110:3307
#sql01
[root@sql01 ~]# mysql  -S /data/3309/mysql.sock  -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.110‘, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql01 ~]# mysql  -S /data/3309/mysql.sock  -e "start slave;"
[root@sql01 ~]# mysql  -S /data/3309/mysql.sock  -e "show slave status\G"

# 10.0.1.120:3309 ------> 10.0.1.120:3307
[root@sql02 ~]# mysql  -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.120‘, MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql02 ~]# mysql  -S /data/3309/mysql.sock -e "start slave;"
[root@sql02 ~]# mysql  -S /data/3309/mysql.sock -e "show slave status\G"

#第二组四节点结构
#10.0.1.120:3308 <-----> 10.0.1.110:3308
#sql01
[root@sql01 ~]# mysql  -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@‘10.0.1.%‘ identified by ‘123‘;"
[root@sql01 ~]# mysql  -S /data/3308/mysql.sock -e "grant all  on *.* to root@‘10.0.1.%‘ identified by ‘123‘  with grant option;"

#sql02
[root@sql02 ~]# mysql  -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.110‘, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql02 ~]# mysql  -S /data/3308/mysql.sock -e "start slave;"
[root@sql02 ~]# mysql  -S /data/3308/mysql.sock -e "show slave status\G"

#sql01
[root@sql01 ~]# mysql  -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.120‘, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql01 ~]# mysql  -S /data/3308/mysql.sock -e "start slave;"
[root@sql01 ~]# mysql  -S /data/3308/mysql.sock -e "show slave status\G"

# 10.0.1.120:3310 -----> 10.0.1.120:3308
[root@sql02 ~]# mysql  -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.120‘, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql02 ~]# mysql  -S /data/3310/mysql.sock -e "start slave;"
[root@sql02 ~]# mysql  -S /data/3310/mysql.sock -e "show slave status\G"

# 10.0.1.110:3310 -----> 10.0.1.110:3308
[root@sql01 ~]# mysql  -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST=‘10.0.1.110‘, MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘;"
[root@sql01 ~]# mysql  -S /data/3310/mysql.sock -e "start slave;"
[root@sql01 ~]# mysql  -S /data/3310/mysql.sock -e "show slave status\G"

#检测主从状态
[root@sql01 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
[root@sql01 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
[root@sql01 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
[root@sql01 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes

MyCAT安装

技术图片

#安装Java运行环境
[root@sql01 ~]# yum install -y java

#下载
http://dl.mycat.io/

#解压文件
[root@sql01 ~]# tar xf Mycat-server-1.6.7.3-release-20190809210613-linux.tar.gz 

#配置环境变量
[root@sql01 ~]# mv mycat /application/mycat
[root@sql01 ~]# echo ‘export PATH=/application/mycat/bin:$PATH‘ >> /etc/profile
[root@sql01 ~]# source /etc/profile

#启动
[root@sql01 ~]# mycat start

#连接mycat
[root@sql01 ~]# mysql -uroot -p123456 -h127.0.0.1 -P8066

#应用前环境准备
[root@sql01 ~]# mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@‘10.0.0.%‘ identified by ‘123‘;"
[root@sql01 ~]# mysql -S /data/3307/mysql.sock -e "source /root/world.sql"

[root@sql01 ~]# mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@‘10.0.0.%‘ identified by ‘123‘;"
[root@sql01 ~]# mysql -S /data/3308/mysql.sock -e "source /root/world.sql"

配置文件介绍

#logs目录:
wrapper.log       #mycat启动日志
mycat.log         #mycat详细工作日志

#conf目录:
schema.xml      #主配置文件(读写分离、高可用、分布式策略定制、节点控制)
server.xml      #mycat软件本身相关的配置
rule.xml        #分片规则配置文件,记录分片规则列表、使用方法等

配置文件结构介绍

[root@sql01 ~]# cd /application/mycat/conf/
[root@sql01 conf]# mv schema.xml schema.xml.bak
[root@sql01 conf]# vim schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

#mycat 逻辑库定义:
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
</schema>

#数据节点定义:
        <dataNode name="sh1" dataHost="opesn01" database= "world" />

#后端主机定义:
        <dataHost name="opesn01" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
                <heartbeat>select user()</heartbeat>
        <writeHost host="db1" url="10.0.1.110:3307" user="root" password="123">
                        <readHost host="db2" url="10.0.1.110:3309" user="root" password="123" />
        </writeHost>
        </dataHost>
</mycat:schema>

#配置中的属性介绍:
#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上分发。

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

#switchType属性:
-1 表示不自动切换 
1 默认值,自动切换 
2 基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status 

#datahost其他配置:
maxCon="1000":最大的并发连接数
minCon="10" :mycat在启动之后,会在后端节点上自动开启的连接线程
tempReadHostAvailable="1"
这个一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时
<heartbeat>select user()</heartbeat>  监测心跳

mycat实现1主1从读写分离

[root@sql01 conf]# cat 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="opesn01" database= "world" />         
        <dataHost name="opesn01" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">    
                <heartbeat>select user()</heartbeat>  
        <writeHost host="db1" url="10.0.1.110:3307" user="root" password="123">
                        <readHost host="db2" url="10.0.1.110:3309" user="root" password="123" /> 
        </writeHost> 
        </dataHost>  
</mycat:schema>
[root@sql01 conf]# 
[root@sql01 conf]# mycat restart

Mycat高可用+读写分离

[root@sql01 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="opesn01" database= "world" />
        <dataHost name="opesn01" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
                <heartbeat>select user()</heartbeat>
        <writeHost host="db1" url="10.0.1.110:3307" user="root" password="123">
                        <readHost host="db2" url="10.0.1.110:3309" user="root" password="123" />
        </writeHost>
        <writeHost host="db3" url="10.0.1.120:3307" user="root" password="123">
                        <readHost host="db4" url="10.0.1.120:3309" user="root" password="123" />
        </writeHost>
        </dataHost>
</mycat:schema>
[root@sql01 conf]# mycat restart

#第一个 writehost: 10.0.1.110:3307   真正的写节点,负责写操作
#第二个 writehost: 10.0.1.120:3307   准备写节点,负责读,当10.0.1.110:3307宕掉,会切换为真正的写节点

Mycat高级应用-分布式解决方案

垂直分表

[root@sql01 conf]# cat 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">
        <table name="user" dataNode="sh1"/>
        <table name="order_t" dataNode="sh2"/>
</schema>  

        <dataNode name="sh1" dataHost="opesn01" database= "taobao" />
        <dataNode name="sh2" dataHost="opesn02" database= "taobao" />

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

    <dataHost name="opesn02" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.1.110:3308" user="root" password="123">
            <readHost host="db2" url="10.0.1.110:3310" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.1.120:3308" user="root" password="123">
            <readHost host="db4" url="10.0.1.120:3310" user="root" password="123" />
    </writeHost>
    </dataHost>	
</mycat:schema>
[root@sql01 conf]# 
[root@sql01 conf]# mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;"
[root@sql01 conf]# mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;"
[root@sql01 conf]# mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))";
[root@sql01 conf]# mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"
[root@sql01 conf]# mycat restart
[root@sql01 conf]# mysql -uroot -p123456 -h 10.0.1.110 -P 8066
[root@db01 conf]# mysql -uroot -p123456 -h127.0.0.1 -P8066
mysql> use TESTDB
mysql> show tables;
+------------------+
| Tables_in_taobao |
+------------------+
| order_t          |
| user             |
+------------------+
2 rows in set (0.00 sec)
mysql> 

Mycat分布式-水平拆分(分片)

#分片:对一个"bigtable",比如说t3表
(1)行数非常多,800w
(2)访问非常频繁

#分片的目的:
(1)将大数据量进行分布存储
(2)提供均衡的访问路由

#分片策略:
范围 range  800w  1-400w 400w01-800w
取模 mod    取余数
枚举 
哈希 hash 
时间 流水

Mycat分布式-范围分片

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

[root@sql01 conf]# cat 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">
        <table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
</schema>  

        <dataNode name="sh1" dataHost="opesn01" database= "taobao" />
        <dataNode name="sh2" dataHost="opesn02" database= "taobao" />

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

    <dataHost name="opesn02" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.1.110:3308" user="root" password="123">
            <readHost host="db2" url="10.0.1.110:3310" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.1.120:3308" user="root" password="123">
            <readHost host="db4" url="10.0.1.120:3310" user="root" password="123" />
    </writeHost>
    </dataHost>	
</mycat:schema>

[root@sql01 conf]# vim rule.xml 
	......
        <tableRule name="auto-sharding-long">
                <rule>
                        <columns>id</columns>
                        <algorithm>rang-long</algorithm>
                </rule>
        </tableRule>
	......
        <function name="rang-long"
                class="io.mycat.route.function.AutoPartitionByLong">
                <property name="mapFile">autopartition-long.txt</property>
        </function>
[root@sql01 conf]# vim autopartition-long.txt 
0-10=0
10-20=1
[root@sql01 conf]# 
[root@sql01 conf]# mycat restart
[root@sql01 conf]# 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);"
[root@sql01 conf]# 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);"
[root@sql01 conf]# 
[root@sql01 conf]# mysql -uroot -p123456 -h 127.0.0.1 -P 8066
mysql> use TESTDB
mysql> insert into t3(id,name) values(1,‘a‘);
mysql> insert into t3(id,name) values(2,‘b‘);
mysql> insert into t3(id,name) values(3,‘c‘);
mysql> insert into t3(id,name) values(10,‘d‘);
mysql> insert into t3(id,name) values(11,‘aa‘);
mysql> insert into t3(id,name) values(12,‘bb‘);
mysql> insert into t3(id,name) values(13,‘cc‘);
mysql> insert into t3(id,name) values(14,‘dd‘);
mysql> insert into t3(id,name) values(20,‘dd‘);
mysql> commit;
[root@sql01 conf]# mysql -S /data/3307/mysql.sock -e "use taobao;select * from t3;"
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
| 10 | d    |
+----+------+
[root@sql01 conf]# mysql -S /data/3308/mysql.sock -e "use taobao;select * from t3;"
+----+------+
| id | name |
+----+------+
| 11 | aa   |
| 12 | bb   |
| 13 | cc   |
| 14 | dd   |
| 20 | dd   |
+----+------+
[root@sql01 conf]# 

取模分片(mod-long):

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

[root@sql01 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">
        <table name="t4" dataNode="sh1,sh2" rule="mod-long" />
</schema>

        <dataNode name="sh1" dataHost="opesn01" database= "taobao" />
        <dataNode name="sh2" dataHost="opesn02" database= "taobao" />

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

    <dataHost name="opesn02" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.1.110:3308" user="root" password="123">
            <readHost host="db2" url="10.0.1.110:3310" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.1.120:3308" user="root" password="123">
            <readHost host="db4" url="10.0.1.120:3310" user="root" password="123" />
    </writeHost>
    </dataHost>
</mycat:schema>

[root@sql01 conf]# vim rule.xml 

        <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
                <!-- how many data nodes -->
                <property name="count">2</property>
        </function>
[root@sql01 conf]# 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);"
[root@sql01 conf]# 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);"
[root@sql01 conf]# mycat restart
[root@sql01 conf]# mysql -uroot -p123456 -h10.0.1.110 -P8066
mysql> use TESTDB
mysql> insert into t4(id,name) values(1,‘a‘);
mysql> insert into t4(id,name) values(2,‘b‘);
mysql> insert into t4(id,name) values(3,‘c‘);
mysql> insert into t4(id,name) values(4,‘d‘);
mysql> commit;
mysql> 
[root@sql01 conf]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t4;"
+----+------+
| id | name |
+----+------+
|  2 | b    |
|  4 | d    |
+----+------+
[root@sql01 conf]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t4;"
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  3 | c    |
+----+------+
[root@sql01 conf]# 

枚举分片

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

[root@sql01 conf]# cat 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">
	<table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />
</schema>  

        <dataNode name="sh1" dataHost="opesn01" database= "taobao" />
        <dataNode name="sh2" dataHost="opesn02" database= "taobao" />

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

    <dataHost name="opesn02" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
        <heartbeat>select user()</heartbeat>
    <writeHost host="db1" url="10.0.1.110:3308" user="root" password="123">
            <readHost host="db2" url="10.0.1.110:3310" user="root" password="123" />
    </writeHost>
    <writeHost host="db3" url="10.0.1.120:3308" user="root" password="123">
            <readHost host="db4" url="10.0.1.120:3310" user="root" password="123" />
    </writeHost>
    </dataHost>	
</mycat:schema>
[root@sql01 conf]# 
[root@sql01 conf]# vim rule.xml 
......
        <tableRule name="sharding-by-intfile">
                <rule>
                        <columns>name</columns>
                        <algorithm>hash-int</algorithm>
                </rule>
        </tableRule>
......
        <function name="hash-int"
                class="io.mycat.route.function.PartitionByFileMap">
                <property name="mapFile">partition-hash-int.txt</property>
                  <property name="type">1</property>
                        <property name="defaultNode">0</property>
        </function>

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

[root@sql01 conf]# vim partition-hash-int.txt 
10000=0
bj=0
sh=1
DEFAULT_NODE=1 
[root@sql01 conf]# 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);"
[root@sql01 conf]# 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);"
[root@sql01 conf]# mycat restart
[root@sql01 conf]# mysql -uroot -p123456 -h10.0.1.110 -P8066
mysql> use TESTDB
mysql> insert into t5(id,name) values(1,‘bj‘);
mysql> insert into t5(id,name) values(2,‘sh‘);
mysql> insert into t5(id,name) values(3,‘bj‘);
mysql> insert into t5(id,name) values(4,‘sh‘);
mysql> insert into t5(id,name) values(5,‘tj‘);
mysql> commit;
mysql> 
[root@sql01 conf]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t5;"
+----+------+
| id | name |
+----+------+
|  1 | bj   |
|  3 | bj   |
|  5 | tj   |
+----+------+
[root@sql01 conf]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t5;"
+----+------+
| id | name |
+----+------+
|  2 | sh   |
|  4 | sh   |
+----+------+
[root@sql01 conf]# 

MySQL-MyCAT分布式架构

标签:箭头   service   db2   driver   介绍   val   null   模式   ecs   

原文地址:https://www.cnblogs.com/opesn/p/12994088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!