MyCAT基础架构准备

环境准备

  • 两台虚拟机db01,db02,每台创建四个实例3307,3308,3309,3310
  • 使用以下命令,清空之前的数据库内容和日志
#db01,db02
pkill mysqld
rm -rf /data/330*
mv /etc/my.cnf /etc/my.cnf.bak

准备文件和目录

创建相关目录初始化数据

#db01,db02
mkdir -p /data/33{07..10}/data
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3310/data --basedir=/application/mysql

准备db01配置文件和启动脚本

#db01

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

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

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

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

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

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

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

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

准备db02配置文件和启动脚本

#db02

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=17
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF

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=18
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF

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=19
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF

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=20
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF

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

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

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

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

修改权限,启动多实例

#db01,db02

chown -R mysql.mysql /data/*
systemctl start mysqld33{07,08,09,10}

#查看启动情况
systemctl status mysqld33{07,08,09,10}
mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"

节点主从规划

16956686-7e5ff50e4071c7eb.png

箭头指向所指代表主库,或互为主从,正常情况下为1主3从

  • 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.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.51:3308
  • slave2:10.0.0.51:3310

配置主从

第一组节点配置主从

# 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 '123456';"
mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123456' 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='123456';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"

#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='123456';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"

# 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='123456';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"

# 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='123456';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"

第二组节点配置主从

# 10.0.0.51:3308 <-----> 10.0.0.52:3308

#db01
mysql -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123456';"
mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123456' 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='123456';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"

#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='123456';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"

# 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='123456';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"

# 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='123456';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"

配置完成后检查主从状态

mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
mysql -S /data/3310/mysql.sock -e "show slave status\G"|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;"

MyCAT安装

预先安装Java运行环境

yum install -y java

下载

镜像地址 Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz

#解压到指定目录下
cd /application
tar -xf Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz

软件目录结构

[root@db01 mycat]# ll
总用量 12
drwxr-xr-x 2 root root  190 4月  21 15:00 bin
drwxrwxrwx 2 root root    6 6月  24 2019 catlet
drwxrwxrwx 4 root root 4096 4月  21 15:00 conf
drwxr-xr-x 2 root root 4096 4月  21 15:00 lib
drwxrwxrwx 2 root root    6 6月  26 2019 logs
-rwxrwxrwx 1 root root  227 6月  27 2019 version.txt

启动和连接

配置环境变量

#添加环境变量
echo 'export PATH=/application/mycat/bin:$PATH' >> /etc/profile
source /etc/profile

启动mycat

#启动mycat
mycat start
#登录到mycat
mysql -uroot -p123456 -h 127.0.0.1 -P8066

数据库分布式架构方式

垂直拆分 水平拆分(range,取模,枚举,hash,时间)

16956686-7c753fb3640bc0ec.png

主要配置文件介绍

  • mycat/conf/rule.xml : 分片策略定义
  • mycat/conf/schema.xml : 主配置文件
  • mycat/conf/server.xml : mycat服务有关
  • mycat/conf/log4j2.xml : 记录日志有关
  • mycat/conf/*.txt : 分片策略使用的规则

用户创建及数据库导入

world.sql

#db01
mysql -S /data/3307/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123456';
source /root/world.sql

#db02
mysql -S /data/3308/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123456';
source /root/world.sql

配置文件结构介绍

<!-- 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="mycat1" database= "world" />
<!-- 后端主机定义 -->
    <dataHost name="mycat1" 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="123456">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>
  • mycat配置读写分离,备份原文件,创建新的配置文件,执行mycat restart命令重启mycat

mycat实现1主1从读写分离配置,写操作落到主库,读操作落到从库.如果主库宕机,从库不能在继续提供服务了

#db01

cd /application/mycat/conf
mv schema.xml schema.xml.bak
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="mycat1" database= "world" />
    <dataHost name="mycat1" 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="123456">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>
  • mycat配置读写分离和高可用,备份原文件,创建新的配置文件,执行mycat restart命令重启mycat

mycat实现1主3从读写分离以及高可用配置,当写节点宕机后,后面跟的readhost也不提供服务,这时候standby的writehost就提供写服务变为1主1从读写分离

  1. 10.0.0.51:3307真正的写节点,负责写操作
  2. 10.0.0.52:3307准备写节点,负责读,当10.0.0.51:3307宕掉,会切换为真正的写节点
#db01

cd /application/mycat/conf
mv schema.xml schema.xml.bak
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="mycat1" database= "world" />    
    <dataHost name="mycat1" 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="123456">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>

配置参数属性介绍

  • balance : 负载均衡类型
  1. balance="0",不开启读写分离机制,所有读操作都发送到当前可用的writeHost
  2. balance="1",全部的readHoststandby writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡
  3. balance="2",所有读操作都随机的在writeHost,readhost上分发
  • writeType : 负载均衡类型
  1. writeType="0",所有写操作发送到配置的第一个writeHost, 第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为主,切换记录在配置文件中:dnindex.properties
  2. writeType="1",所有写操作都随机的发送到配置的writeHost,但不推荐使用
  • switchType : 自动切换类型
  1. switchType="-1",表示不自动切换
  2. switchType="1",默认值自动切换
  3. switchType="2",基于MySQL主从同步的状态决定是否切换,心跳语句为show slave status;
  • maxCon : 最大的并发连接数
  • minCon : mycat在启动之后,会在后端节点上自动开启的连接线程
  • tempReadHostAvailable="1" : 改参数在一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时<heartbeat>select user()</heartbeat>监测心跳

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

垂直拆分

  • 垂直拆分,将压力大的表拆分到另一组实例上,分担数据库压力,备份原文件,创建新的配置文件,并创建测试库和表,执行mycat restart命令重启mycat
#db01

cd /application/mycat/conf
mv schema.xml schema.xml.bak
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="user" dataNode="sh1"/>
        <table name="order_t" dataNode="sh2"/>
    </schema>
    <dataNode name="sh1" dataHost="mycat1" database= "taobao" />
    <dataNode name="sh2" dataHost="mycat2" database= "taobao" />
    <dataHost name="mycat1" 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="123456">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123456" />
        </writeHost>
    </dataHost>
    <dataHost name="mycat2" 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="123456">
            <readHost host="db2" url="10.0.0.51:3310" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3308" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3310" user="root" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>
  • 创建测试库和表
#db01

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))"
  • 重启后查看分库分表效果
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "show tables from taobao;"
+------------------+
| Tables_in_taobao |
+------------------+
| user             |
+------------------+
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "show tables from taobao;"
+------------------+
| Tables_in_taobao |
+------------------+
| order_t          |
+------------------+
[root@db01 conf]# mysql -uroot -p123456 -h 127.0.0.1 -P 8066 -e "use TESTDB;show tables;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+
| Tables_in_taobao |
+------------------+
| order_t          |
| user             |
+------------------+

水平拆分

  • 案例说明

对一个有800w行数据的表进行水平拆分,有以下几种方案

  1. 范围range,1-400w,400w-800w
  2. 取模mod,取余数
  3. 枚举
  4. 哈希hash
  5. 时间,流水

Mycat分布式-范围分片

  • 水平拆分,范围分片,将一张表根据设定的范围拆分到两组实例上,分担数据库压力,备份原文件,创建新的配置文件,并创建测试库和表,执行mycat restart命令重启mycat
#db01

cd /application/mycat/conf
mv schema.xml schema.xml.bak
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="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
    </schema>
    <dataNode name="sh1" dataHost="mycat1" database= "taobao" />
    <dataNode name="sh2" dataHost="mycat2" database= "taobao" />
    <dataHost name="mycat1" 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="123456">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123456" />
        </writeHost>
    </dataHost>
    <dataHost name="mycat2" 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="123456">
            <readHost host="db2" url="10.0.0.51:3310" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3308" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3310" user="root" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>
  • 根据rule="auto-sharding-long",可以去rule.xml中查看分片策略,找到auto-sharding-long相关信息
        <tableRule name="auto-sharding-long">
                <rule>
                        <columns>id</columns><!-- 将id列作为分片列 -->
                        <algorithm>rang-long</algorithm><!-- 分片策略函数名 -->
                </rule>
        </tableRule>

<!-- 往下翻,找到rang-long函数,可以在autopartition-long.txt文件中修改分片范围 -->

        <function name="rang-long"
                class="io.mycat.route.function.AutoPartitionByLong">
                <property name="mapFile">autopartition-long.txt</property>
        </function>
  • 查看autopartition-long.txt文件
# range start-end ,data node index
# K=1000,M=10000.

#节点根据写入顺序从上往下,从0开始计数
#5-5000000 数据分配到0节点
0-500M=0
#5000000-10000000 数据分配到1节点
500M-1000M=1
  • 修改autopartition-long.txt中的分片范围
#1-10 数据分配到0节点
1-10=0
#10-20 数据分配到1节点
10-20=1
  • 创建测试库和表
#db01

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);"
  • 重启后插入数据
#登录mycat
mysql -uroot -p123456 -h 127.0.0.1 -P 8066

use TESTDB
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(10,'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(20,'dd');
  • 查看效果
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t3;"
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
| 10 | d    |
+----+------+
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t3;"
+----+------+
| id | name |
+----+------+
| 11 | aa   |
| 12 | bb   |
| 13 | cc   |
| 14 | dd   |
| 20 | dd   |
+----+------+

Mycat分布式-取模分片

  • 水平拆分,取模分片,将一张表根据取模规则拆分到两组实例上,分担数据库压力,备份原文件,创建新的配置文件,并创建测试库和表,执行mycat restart命令重启mycat
#db01

cd /application/mycat/conf
mv schema.xml schema.xml.bak
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="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
        <table name="t4" dataNode="sh1,sh2" rule="mod-long" />
    </schema>
    <dataNode name="sh1" dataHost="mycat1" database= "taobao" />
    <dataNode name="sh2" dataHost="mycat2" database= "taobao" />
    <dataHost name="mycat1" 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="123456">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123456" />
        </writeHost>
    </dataHost>
    <dataHost name="mycat2" 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="123456">
            <readHost host="db2" url="10.0.0.51:3310" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3308" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3310" user="root" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>
  • 根据rule="mod-long",可以去rule.xml中查看分片策略,找到mod-long相关信息
        <tableRule name="mod-long">
                <rule>
                        <columns>id</columns>
                        <algorithm>mod-long</algorithm>
                </rule>
        </tableRule>

<!-- 往下翻,找到mod-long函数,根据实际节点数,修改count值 -->
        <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
                <!-- how many data nodes -->
                <!-- 根据数据节点数设置为2,取模的数是2 -->
                <property name="count">2</property>
        </function>
  • 创建测试库和表
#db01

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
mysql -uroot -p123456 -h 127.0.0.1 -P 8066

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');
  • 查看效果
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t4;"
+----+------+
| id | name |
+----+------+
|  2 | b    |
|  4 | d    |
+----+------+
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t4;"
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  3 | c    |
+----+------+

Mycat分布式-枚举分片

  • 水平拆分,枚举分片,将一张表根据枚举规则拆分到两组实例上,分担数据库压力,备份原文件,创建新的配置文件,并创建测试库和表,执行mycat restart命令重启mycat
#db01

cd /application/mycat/conf
mv schema.xml schema.xml.bak
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="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
        <table name="t4" dataNode="sh1,sh2" rule="mod-long" />
        <table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />
    </schema>
    <dataNode name="sh1" dataHost="mycat1" database= "taobao" />
    <dataNode name="sh2" dataHost="mycat2" database= "taobao" />
    <dataHost name="mycat1" 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="123456">
            <readHost host="db2" url="10.0.0.51:3309" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3309" user="root" password="123456" />
        </writeHost>
    </dataHost>
    <dataHost name="mycat2" 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="123456">
            <readHost host="db2" url="10.0.0.51:3310" user="root" password="123456" />
        </writeHost>
        <writeHost host="db3" url="10.0.0.52:3308" user="root" password="123456">
            <readHost host="db4" url="10.0.0.52:3310" user="root" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>
  • 根据rule="sharding-by-intfile",可以去rule.xml中查看分片策略,找到mod-long相关信息
        <tableRule name="sharding-by-intfile">
                <rule>
                        <columns>name</columns><!-- 将name列作为分片列 -->
                        <algorithm>hash-int</algorithm>><!-- 分片策略函数名 -->
                </rule>
        </tableRule>

<!-- 往下翻,找到hash-int函数,可以在partition-hash-int.txt文件中修改分片范围 -->

        <function name="hash-int"
                class="io.mycat.route.function.PartitionByFileMap">
                <property name="mapFile">partition-hash-int.txt</property>
                <!-- 默认情况下mycat分片列的值不能为字符串,加上该参数则支持字符串 -->
                <property name="type">1</property>
        </function>
  • 查看partition-hash-int.txt文件
10000=0
10010=1
  • 修改partition-hash-int.txt中的枚举规则
#name列为bj,sh的数据会被添加到对应节点
bj=0
sh=1
#其余的默认为1节点
DEFAULT_NODE=1
  • 创建测试库和表
#db01

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
mysql -uroot -p123456 -h 127.0.0.1 -P 8066

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');
  • 查看效果
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t5;"
+----+------+
| id | name |
+----+------+
|  1 | bj   |
|  3 | bj   |
+----+------+
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t5;"
+----+------+
| id | name |
+----+------+
|  2 | sh   |
|  4 | sh   |
|  5 | tj   |
+----+------+