MyCATMycat是一个数据库中间件,是一个实现了MySQL协议的服务器。前端用户可以把它看作是一个数据库代理,用 MySQL 客户端工具和命令行访问,而其后端可以用
MySQL 原生(Native) 协议与多个 MySQL 服务器通信,也可以用 JDBC 协议与大多数主流数据库服务器通信其。核心功能是分库分表,配合数据库的主从模式还可以实现读写分离。
MyCAT最重要的动词是"拦截",它拦截了用户发送过来的SQL语句并对其进行特定分析:如分片分析、路由分析、读写分离分析、缓存分析等,然后将此SQL发往后端的真实数据库,并将返回的结果做适当的处理,最终再返回给用户。
MyCAT拆分介绍
MyCAT应用场景
db01 3307实例与db02 3307实例互为主从关系;
每个3309实例是本地3307实例的从库;
192.168.1.5:3307 ⇋ 192.168.1.6:3307
192.168.1.5:3309 → 192.168.1.5:3307
192.168.1.6:3309 → 192.168.1.6:3307
db01 3308实例与db02 3308实例互为主从关系;
每个3310实例是本地3308实例的从库;
192.168.1.5:3308 ⇋ 192.168.1.6:3308
192.168.1.5:3310 → 192.168.1.5:3308
192.168.1.6:3310 → 192.168.1.6:3308
节点分片规划:
shard1:
Master:192.168.1.5:3307
slave1:192.168.1.5:3309
Standby Master:192.168.1.6:3307
slave2:192.168.1.6:3309
shard2:
Master:192.168.1.6:3308
slave1:192.168.1.6:3310
Standby Master:192.168.1.5:3308
slave2:192.168.1.5:3310
部署过程:
创建多实例
两台虚拟机 db01(192.168.1.5)和db02(192.168.1.6)
每台虚拟机创建四个MySQL实例:3307 3308 3309 3310
创建脚本:
[root@db01 ~]# cat /scripts/deploy_multiple_instances.sh
# delete historical environment
pkill mysqld
rm -rf /data
mv /etc/my.cnf /etc/my.cnf.bak
# make data directory
mkdir /data/33{07..10}/data -p
# grant promissions to the data directory
chown -R mysql.mysql /data
# Initialize the data directory
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/app/mysql &> /dev/null && echo "3307 initialized complete."
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/app/mysql &> /dev/null && echo "3308 initialized complete."
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/app/mysql &> /dev/null && echo "3309 initialized complete."
mysqld --initialize-insecure --user=mysql --datadir=/data/3310/data --basedir=/app/mysql &> /dev/null && echo "3310 initialized complete."
# edit profile
cat >/data/3307/my.cnf<<EOF
[mysqld]
basedir=/app/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=/app/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=/app/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=/app/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
# add to systemd manager
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=/app/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=/app/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=/app/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=/app/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF
# grant promissions to the data directory
chown -R mysql.mysql /data
# start mysqld instances
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
systemctl start mysqld3310
# check instances status
mysql -S /data/3307/mysql.sock -e "select @@server_id"
mysql -S /data/3308/mysql.sock -e "select @@server_id"
mysql -S /data/3309/mysql.sock -e "select @@server_id"
mysql -S /data/3310/mysql.sock -e "select @@server_id"
shard1部署主从关系
192.168.1.5:3307 ⇋ 192.168.1.6:3307
# db02
mysql -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'192.168.1.%' identified by '123';"
mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'192.168.1.%' identified by '123' with grant option;"
# db01
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.6', 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 status\G"
# db02
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.5', 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 status\G"
192.168.1.5:3309 → 192.168.1.5:3307
# db01
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.5', 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 status\G"
192.168.1.6:3309 → 192.168.1.6:3307
# db02
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.6', 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 status\G"
shard2部署主从关系
192.168.1.5:3308 ⇋ 192.168.1.6:3308
# db01
mysql -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'192.168.1.%' identified by '123';"
mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'192.168.1.%' identified by '123' with grant option;"
# db02
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.5', 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 status\G"
# db01
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.6', 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 status\G"
192.168.1.5:3310 → 192.168.1.5:3308
# db01
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.5', 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 status\G"
192.168.1.6:3310 → 192.168.1.6:3308
# db02
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.6', 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 status\G"
检查主从状态
# db01
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@db01 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@db01 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
# db02
[root@db02 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@db02 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@db02 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@db02 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
导入测试库
# db01
mysql> mysql -S /data/3307/mysql.sock
source /root/world.sql
mysql> mysql -S /data/3308/mysql.sock
source /root/world.sql
安装Java运行环境
[root@db01 ~]# yum install -y java
下载解压MyCAT软件
[root@db01 ~]# wget http://dl.mycat.org.cn/1.6.7.6/20201112144313/Mycat-server-1.6.7.6-release-20201112144313-linux.tar.gz
[root@db01 ~]# tar xf Mycat-server-1.6.7.6-release-20201112144313-linux.tar.gz
[root@db01 ~]# mkdir /application
[root@db01 ~]# mv mycat /application/
查看MyCAT软件目录结构
[root@db01 ~]# ls /application/mycat/
bin catlet conf lib logs version.txt
启动和连接MyCAT
# 配置环境变量
[root@db01 ~]# vim /etc/profile
export PATH=/application/mycat/bin:$PAT
[root@db01 ~]# source /etc/profile
# 启动MyCAT
[root@db01 ~]# mycat start
Starting Mycat-server...
[root@db01 ~]# netstat -lntup | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 6719/java
[root@db01 ~]# netstat -lntup | grep 9066
tcp6 0 0 :::9066 :::* LISTEN 6719/java
# 连接MyCAT(默认用户名和密码为root 123456,在server.xml中设置)
[root@db01 conf]# mysql -uroot -p123456 -h 192.168.1.5 -P8066
MyCAT相关文件介绍
[root@db01 ~]# ls /application/mycat/logs/
mycat.log # mycat详细工作日志
mycat.pid
switch.log
wrapper.log # mycat启动日志
[root@db01 ~]# ls /application/mycat/conf/
schema.xml # 主配置文件(读写分离、高可用、分布式策略定制、节点控制)
server.xml # mycat软件本身相关的配置
rule.xml # 分片规则配置文件,记录分片规则列表、使用方法等
编辑配置文件
[root@db01 ~]# cd /application/mycat/conf/
[root@db01 conf]# mv schema.xml schema.xml.bak
[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="dn1"> </schema> # TESTDB逻辑库去访问数据节点dn1
<dataNode name="dn1" dataHost="localhost1" database= "wordpress" /> # 数据节点dn1的定义:访问dn1就是访问wordpress数据库(库名确定)
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> # 数据主机,确定物理节点的主机IP等信息
<heartbeat>select user()</heartbeat>
<writeHost host="db01" url="192.168.1.5:3307" user="root" password="123">
<readHost host="db02" url="192.168.1.5:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
schema(TESTDB)→ dataNode(dn1) → dataHost(host、r、w)
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.00 sec)
编辑schema.xml配置文件(更改datanode的database= 以及writehost和readhost的内容)
[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="dn1">
</schema>
<dataNode name="dn1" dataHost="localhost1" database= "world" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db01" url="192.168.1.5:3307" user="root" password="123">
<readHost host="db02" url="192.168.1.5:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
~
重启mycat
[root@db01 conf]# mycat restart
读写分离测试
[root@db01 conf]# mysql -uroot -p123456 -h 192.168.1.5 -P8066
mysql> use TESTDB;
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+
1 row in set (0.02 sec)
mysql> begin ;select @@server_id; commit;
Query OK, 0 rows affected (0.01 sec)
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
注意:以上案例实现了1主1从的读写分离功能,写操作落到主库,读操作落到从库。如果主库宕机,从库也不能再继续提供读服务(配置文件readhost包含在writehost内)。
部署过程:
编辑schema.xml配置文件
[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="dn1">
</schema>
<dataNode name="dn1" dataHost="localhost1" database= "world" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db01" url="192.168.1.5:3307" user="root" password="123"> <readHost host="db02" url="192.168.1.5:3309" user="root" password="123" />
</writeHost>
<writeHost host="db03" url="192.168.1.6:3307" user="root" password="123">
<readHost host="db04" url="192.168.1.6:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
~
重启mycat
[root@db01 conf]# mycat restart
测试高可用
写节点宕机之前
# 读测试
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 17 |
+-------------+
1 row in set (0.11 sec)
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 19 |
+-------------+
1 row in set (0.10 sec)
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+
1 row in set (0.20 sec)
# 写测试
mysql> begin; select @@server_id; commit;
Query OK, 0 rows affected (0.01 sec)
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
写节点宕机之后
# 读测试
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 19 |
+-------------+
1 row in set (0.08 sec)
# 写测试
mysql> begin; select @@server_id; commit;
Query OK, 0 rows affected (0.01 sec)
+-------------+
| @@server_id |
+-------------+
| 17 |
+-------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
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. switchType="-1" 表示不自动切换
2. switchType="1" 默认值,自动切换
3. switchType="2" 基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status
maxCon属性:最大的并发连接数
minCon属性:MyCAT在启动之后,会在后端节点上自动开启的连接线程数(设置过大对内存资源压力较大)
tempReadHostAvailable属性:一主一从时(1个writehost,1个readhost时),可以开启这个参数,在写节点宕机时,可暂时开启读功能;如果2个writehost,2个readhost时,没有必要开启此属性。
select user() 监测心跳,通过该语句探测数据库是否宕机
需求:将world数据库中的city表和country表分别放在不同的节点上
<?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="dn1">
<table name="user" dataNode="dn1" />
<table name="order" dataNode="dn2" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database= "taobao" />
<dataNode name="dn2" dataHost="localhost2" database= "taobao" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db01" url="192.168.1.5:3307" user="root" password="123">
<readHost host="db02" url="192.168.1.5:3309" user="root" password="123" />
</writeHost>
<writeHost host="db03" url="192.168.1.6:3307" user="root" password="123">
<readHost host="db04" url="192.168.1.6:3309" user="root" password="123" />
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db01" url="192.168.1.5:3308" user="root" password="123">
<readHost host="db02" url="192.168.1.5:3310" user="root" password="123" />
</writeHost>
<writeHost host="db03" url="192.168.1.6:3308" user="root" password="123">
<readHost host="db04" url="192.168.1.6:3310" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
创建库表
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;"
[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;"
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))";
[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"
重启mycat
[root@db01 ~]# mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
查看mycat库表情况。此时对应用来说,order_t和user表都在TESTDB表中,但是order_t和user表实际上在不同的物理节点上,如此便实现了垂直拆分。
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.01 sec)
mysql> use TESTDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables ;
+------------------+
| Tables_in_taobao |
+------------------+
| order_t |
| user |
+------------------+
2 rows in set (0.14 sec)
分片需求
分片目的
分片策略
优化关联查询
编辑配置文件schema.xml
[root@db01 conf]# vim schema.xml
<table name="t1" dataNode="dn1,dn2" rule="auto-sharding-long" />
查看配置文件rule.xml,可以得知range分配规则由autopartition-long.txt指定
[root@db01 conf]# vim rule.xml
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
查看并编辑autopartition-long.txt
[root@db01 conf]# cat autopartition-long.txt
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2
[root@db01 conf]# vim autopartition-long.txt
0-10=0
11-20=1
创建测试表
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "use taobao;create table t1 (id int not null primary key auto_increment,name varchar(20) not null);"
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "use taobao;create table t1 (id int not null primary key auto_increment,name varchar(20) not null);"
重启mycat
[root@db01 conf]# mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
测试
# mycat录入数据
[root@db01 conf]# mysql -uroot -p123456 -h 127.0.0.1 -P 8066
mysql> insert into t1(id,name) values(1,'a'),(2,'b');
Query OK, 2 rows affected (2.22 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into t1(id,name) values(11,'aa'),(12,'bb');
Query OK, 2 rows affected (0.13 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 11 | aa |
| 12 | bb |
+----+------+
4 rows in set (0.20 sec)
分别查询mysqld3307和mysqld3308
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t1;"
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
+----+------+
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t1;"
+----+------+
| id | name |
+----+------+
| 11 | aa |
| 12 | bb |
+----+------+
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点。
编辑schema.xml
[root@db01 conf]# vim schema.xml
<table name="t2" dataNode="dn1,dn2" rule="mod-log" />
查看rule.xml
[root@db01 conf]# vim rule.xml
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
</function>
创建测试表
mysql -S /data/3307/mysql.sock -e "use taobao;create table t2 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t2 (id int not null primary key auto_increment,name varchar(20) not null);"
重启mycat
[root@db01 conf]# mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
测试
# mycat录入数据
[root@db01 conf]# mysql -uroot -p123456 -h 127.0.0.1 -P 8066
mysql> insert into t2(id,name) values(1,'a'),(2,'b');
Query OK, 2 rows affected (2.22 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into t2(id,name) values(3,'c'),(4,'d');
Query OK, 2 rows affected (0.13 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t2;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+----+------+
4 rows in set (0.20 sec)
分别查询mysqld3307和mysqld3308
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t2;"
+----+------+
| id | name |
+----+------+
| 2 | b |
| 4 | d |
+----+------+
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t2;"
+----+------+
| id | name |
+----+------+
| 1 | a |
| 3 | c |
+----+------+
编辑配置文件schema.xml
vim schema.xml
<table name="t3" dataNode="dn1,dn2" rule="sharding-by-intfile" />
查看配置文件rule.xml
cat rule.xml
<tableRule name="sharding-by-intfile">
<rule>
<columns>name</columns> # columns 标识将要分片的表字段
<algorithm>hash-int</algorithm> # algorithm 分片函数,其中分片函数配置中,mapFile标识配置文件名称
</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> # 若不加该行partition-hash-int.txt只支持数字
</function>
编辑partition-hash-int.txt
vim partition-hash-int.txt
beijing=0
shanghai=1
测试…
使用场景
为了防止跨分片join,可以使用E-R模式。
<table name="A" dataNode="dn1,dn2" rule="mod-long">
<childTable name="B" joinKey="yy" parentKey="xx" />
</table>