MySQL分布式架构——MyCAT
一、MyCAT准备环境
两台虚拟机 db01 db02
每台创建四个mysql实例:3307 3308 3309 3310
节点主从规划
箭头指向谁是主库
192.168.1.1:3307 <-----> 192.168.1.2:3307
192.168.1.1:3309 ------> 192.168.1.1:3307
192.168.1.2:3309 ------> 192.168.1.2:3307
192.168.1.2:3308 <-----> 192.168.1.1:3308
192.168.1.2:3310 -----> 192.168.1.2:3308
192.168.1.1:3310 -----> 192.168.1.1:3308
分片规划
shard1:
Master:192.168.1.1:3307
slave1:192.168.1.1:3309
Standby Master:192.168.1.2:3307
slave2:192.168.1.2:3309
shard2:
Master:192.168.1.2:3308
slave1:192.168.1.2:3310
Standby Master:192.168.1.1:3308
slave2:192.168.1.1:3310
二、MyCAT基础环境准备
2.1 删除历史环境
两台db数据库都需进行以下操作!
pkill mysqld
rm -rf /data/330*
mv /etc/my.cnf /etc/my.cnf.bak
2.2 创建相应目录、初始化数据
两台db数据库都需进行以下操作!
mkdir /data/33{07..10}/data -p
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/app/database/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/app/database/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/app/database/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3310/data --basedir=/app/database/mysql
2.3 准备MySQL的配置文件
=====================db01=====================
cat >/data/3307/my.cnf<<EOF
[mysqld]
basedir=/app/database/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/database/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/database/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/database/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
=====================db02=====================
cat >/data/3307/my.cnf<<EOF
[mysqld]
basedir=/app/database/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=/app/database/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=/app/database/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=/app/database/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
2.4 准备MySQL的启动脚本
=====================db01=====================
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/database/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/database/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/database/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/database/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF
=====================db02=====================
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/database/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/database/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/database/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/database/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF
2.5 修改权限,启动多实例
两台db服务器都需进行以下操作:
chown -R mysql.mysql /data/*
systemctl daemon-reload
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
systemctl start mysqld3310
确认两台db服务器MySQL实例对应的端口已经启动!
2.6 配置主从关系
① shard1
192.168.1.1:3307 <-----> 192.168.1.2: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.2', 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.1', 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.1:3309 ------> 192.168.1.1:3307
db01
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.1', 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.2:3309 ------> 192.168.1.2:3307
db02
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.2', 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.2:3308 <-----> 192.168.1.1: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.1', 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.2', 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.2:3310 -----> 192.168.1.2:3308
db02
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.2', 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.1:3310 -----> 192.168.1.1:3308
db01
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.1', 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"
③ 确认主从关系
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
保证两条db服务器可以看到8个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
本次选择使用一台单独的服务器安装配置MyCAT!
3.1 安装MyCAT
[root@mycat ~]# wget http://dl.mycat.io/1.6.5/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
[root@mycat ~]# yum -y install java mysql
[root@mycat ~]# tar zxf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
[root@mycat ~]# echo -e "export PATH=/root/mycat/bin:$PATH" >> /etc/profile
[root@mycat ~]# source /etc/profile
[root@mycat ~]# mycat start
[root@mycat ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]>
#可以正常访问即可!
3.2 MyCAT配置文件介绍
logs目录:
wrapper.log ---->mycat启动日志
mycat.log ---->mycat详细工作日志
conf目录:
schema.xml
主配置文件(读写分离、高可用、分布式策略定制、节点控制)
server.xml
mycat软件本身相关的配置
rule.xml
分片规则配置文件,记录分片规则列表、使用方法等
四、MyCAT实现读写分离
[root@mycat ~]# cd mycat/conf/
[root@mycat conf]# mv schema.xml schema.xml.bak
[root@mycat 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= "test" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.1:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.1:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
配置文件简单介绍
逻辑库:schema
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
数据节点(逻辑):datanode
<dataNode name="dn1" dataHost="localhost1" database= "test" />
#DN配置垂直和水平拆分
数据主机(逻辑):datahost(w和r)
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.1:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.1:3309" user="root" password="123" />
</writeHost>
</dataHost>
#DH主要配置高可用和读写分离
测试读写分离
[root@mycat conf]# mycat restart
[root@mycat conf]# mysql -uroot -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+
MySQL [(none)]> begin ; select @@server_id; commit;
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
如果出现错误,MyCAT使用用户连接数据进行测试:
[root@mycat ~]# mysql -uroot -p123 -h 192.168.1.1 -P 3307
[root@mycat ~]# mysql -uroot -p123 -h 192.168.1.1 -P 3309
五、MyCAT配置读写分离及高可用
[root@mycat conf]# mv schema.xml schema.xml.rw
[root@mycat 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="lzj" database= "test" />
<dataHost name="lzj" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.1:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.1:3309" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.1.2:3307" user="root" password="123">
<readHost host="db4" url="192.168.1.2:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
配置文件介绍
<writeHost host="db1" url="192.168.1.1:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.1:3309" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.1.2:3307" user="root" password="123">
<readHost host="db4" url="192.168.1.2:3309" user="root" password="123" />
</writeHost>
#真正的 writehost:负责写操作的writehost;standby writeHost :和readhost一样,只提供读服务(上述配置文件是一写三读)
#当写节点宕机后,后面跟的readhost也不提供服务,这时候standby的writehost就提供写服务,后面跟的readhost提供读服务(一写一读)
#故障修复之后,原本的写库也就变成了读库,依然是一写三读的架构
测试
[root@mycat conf]# mycat restart
[root@mycat conf]# mysql -uroot -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 19 |
+-------------+
1 row in set (0.01 sec)
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+
1 row in set (0.00 sec)
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 17 |
+-------------+
MySQL [(none)]> begin ; select @@server_id; commit;
Query OK, 0 rows affected (0.00 sec)
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
此时将配置文件中第一个写节点宕机,模拟故障,进行测试:
[root@db01 ~]# systemctl stop mysqld3307
MySQL [(none)]> begin ; select @@server_id; commit;
+-------------+
| @@server_id |
+-------------+
| 17 |
+-------------+
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 19 |
+-------------+
将故障节点恢复再次进行测试:
[root@db01 ~]# systemctl start mysqld3307
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 19 |
+-------------+
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+
MySQL [(none)]> begin ; select @@server_id; commit;
+-------------+
| @@server_id |
+-------------+
| 17 |
+-------------+
六、配置文件重要属性介绍
6.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上分发。
6.2 writeType属性
负载均衡类型,目前的取值有2种:
1. writeType="0", 所有写操作发送到配置的第一个writeHost, 第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为主,切换记录在配置文件中:dnindex.properties;
2. writeType=“1”,所有写操作都随机的发送到配置的writeHost,但不推荐使用;
6.3 switchType属性
取值范围有3种:
-1 表示不自动切换
1 默认值,自动切换
2 基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status
#双主双从建议使用手动切换
6.4 datahost其他配置
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
maxCon="1000":最大的并发连接数
minCon="10" :mycat在启动之后,会在后端节点上自动开启的连接线程
tempReadHostAvailable="1"
这个一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时,如果开启,故障库发生故障时,就变成了一写两读
<heartbeat>select user()</heartbeat> 监测心跳
七、MyCAT实现垂直拆分
垂直拆分说白了就是将库中的表进行拆分!
[root@mycat conf]# mv schema.xml schema.xml.ha
[root@mycat 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="user" dataNode="sh1"/>
<table name="order_t" dataNode="sh2"/>
</schema>
<dataNode name="sh1" dataHost="lzj1" database= "taobao" />
<dataNode name="sh2" dataHost="lzj2" database= "taobao" />
<dataHost name="lzj1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.1:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.1:3309" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.1.2:3307" user="root" password="123">
<readHost host="db4" url="192.168.1.2:3309" user="root" password="123" />
</writeHost>
</dataHost>
<dataHost name="lzj2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.1:3308" user="root" password="123">
<readHost host="db2" url="192.168.1.1:3310" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.1.2:3308" user="root" password="123">
<readHost host="db4" url="192.168.1.2: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))"
生产中需要在配置文件中添加以下内容:
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh3">
dataNode选项表示:如果不是访问的以下两个表,所有的表都走sh3
<table name="user" dataNode="sh1"/>
<table name="order_t" dataNode="sh2"/>
<dataNode name="sh3" dataHost="lzj3" database= "taobao" />
测试
[root@mycat conf]# mycat restart
[root@mycat conf]# mysql -u root -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]> use TESTDB;
MySQL [TESTDB]> show tables;
+------------------+
| Tables_in_taobao |
+------------------+
| order_t |
| user |
+------------------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3307 -e 'show tables from taobao;'
+------------------+
| Tables_in_taobao |
+------------------+
| user |
+------------------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3308 -e 'show tables from taobao;'
+------------------+
| Tables_in_taobao |
+------------------+
| order_t |
+------------------+
八、MyCAT实现水平拆分
8.1 范围分片
[root@mycat conf]# cp schema.xml schema.xml.1
[root@mycat conf]# vim schema.xml
更改为以下内容
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
<table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
</schema>
<dataNode name="sh1" dataHost="oldguo1" database= "taobao" />
<dataNode name="sh2" dataHost="oldguo2" database= "taobao" />
如图:
查看rule文件定义的auto-sharding-long:
[root@mycat conf]# vim rule.xml
如果生产中没有id列,可自行更改rule定义的条件!
这个rule文件无需修改,主要就是查看规则是如何定义的!
编辑规则中定义的autopartition-long.txt文件:
[root@mycat conf]# vim autopartition-long.txt
0-10=0
11-20=1
#这个文件默认有一些规则,直接将其删除,编写自己所需要的即可!
创建测试表:
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);"
测试:
[root@mycat conf]# mycat restart
[root@mycat conf]# mysql -u root -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]> use TESTDB
MySQL [TESTDB]> insert into t3(id,name) values(1,'a');
MySQL [TESTDB]> insert into t3(id,name) values(2,'b');
MySQL [TESTDB]> insert into t3(id,name) values(3,'c');
MySQL [TESTDB]> insert into t3(id,name) values(4,'d');
MySQL [TESTDB]> insert into t3(id,name) values(11,'aa');
MySQL [TESTDB]> insert into t3(id,name) values(12,'bb');
MySQL [TESTDB]> insert into t3(id,name) values(13,'cc');
MySQL [TESTDB]> insert into t3(id,name) values(14,'dd');
MySQL [TESTDB]> select * from t3;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 11 | aa |
| 12 | bb |
| 13 | cc |
| 14 | dd |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3307 -e 'select * from taobao.t3;'
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3308 -e 'select * from taobao.t3;'
+----+------+
| id | name |
+----+------+
| 11 | aa |
| 12 | bb |
| 13 | cc |
| 14 | dd |
+----+------+
8.2 取模分片
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点!
[root@mycat conf]# vim schema.xml
<table name="t4" dataNode="sh1,sh2" rule="mod-long" />
查看rule文件定义的mod-long:
创建测试表:
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);"
测试:
[root@mycat conf]# mycat restart
[root@mycat conf]# mysql -u root -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]> use TESTDB
MySQL [TESTDB]> insert into t4(id,name) values(1,'a');
MySQL [TESTDB]> insert into t4(id,name) values(2,'b');
MySQL [TESTDB]> insert into t4(id,name) values(3,'c');
MySQL [TESTDB]> insert into t4(id,name) values(4,'d');
MySQL [TESTDB]> select * from t4;
+----+------+
| id | name |
+----+------+
| 2 | b |
| 4 | d |
| 1 | a |
| 3 | c |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3307 -e 'select * from taobao.t4;'
+----+------+
| id | name |
+----+------+
| 2 | b |
| 4 | d |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3308 -e 'select * from taobao.t4;'
+----+------+
| id | name |
+----+------+
| 1 | a |
| 3 | c |
+----+------+
8.3 枚举分片
[root@mycat conf]# vim schema.xml
<table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />
查看rule文件定义的sharding-by-intfile:
[root@mycat conf]# vim rule.xml
<tableRule name="sharding-by-intfile">
<rule> <columns>name</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<function name="hash-int" class="org.opencloudb.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
<property name="type">1</property>
</function>
编辑partition-hash-int.txt文件:
[root@mycat conf]# vim partition-hash-int.txt
bj=0
sh=1
DEFAULT_NODE=1
#bj存在在0号分片,sh存放在1号分片,剩余都存放在1号分片
创建测试表:
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);"
测试:
[root@mycat conf]# mycat restart
[root@mycat conf]# mysql -u root -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]> use TESTDB
MySQL [TESTDB]> insert into t5(id,name) values(1,'bj');
MySQL [TESTDB]> insert into t5(id,name) values(2,'sh');
MySQL [TESTDB]> insert into t5(id,name) values(3,'bj');
MySQL [TESTDB]> insert into t5(id,name) values(4,'sh');
MySQL [TESTDB]> insert into t5(id,name) values(5,'tj');
MySQL [TESTDB]> select * from t5;
+----+------+
| id | name |
+----+------+
| 1 | bj |
| 3 | bj |
| 2 | sh |
| 4 | sh |
| 5 | tj |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3307 -e 'select * from taobao.t5;'
+----+------+
| id | name |
+----+------+
| 1 | bj |
| 3 | bj |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3308 -e 'select * from taobao.t5;'
+----+------+
| id | name |
+----+------+
| 2 | sh |
| 4 | sh |
| 5 | tj |
+----+------+
MyCAT还有很多的分片策略,可参考rule文件自行测试,也可以查看MyCAT官方文档!
九、MyCAT全局表
使用场景:
如果你的业务中有些数据类似于数据字典,比如配置文件的配置,
常用业务的配置或者数据量不大很少变动的表,这些表往往不是特别大,而且大部分的业务场景都会用到,那么这种表适合于Mycat全局表,无须对数据进行切分,要在所有的分片上保存一份数据即可,Mycat 在Join操作中,业务表与全局表进行Join聚合会优先选择相同分片内的全局表join,避免跨库Join,在进行数据插入操作时,mycat将把数据分发到全局表对应的所有分片执行,在进行数据读取时候将会随机获取一个节点读取数据。
[root@mycat conf]# vim schema.xml
<table name="t_area" primaryKey="id" type="global" dataNode="sh1,sh2" />
创建测试数据:
mysql -S /data/3307/mysql.sock -e '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 -e 'use taobao;create table t_area (id int not null primary key auto_increment,name varchar(20) not null);'
测试:
[root@mycat conf]# mycat restart
[root@mycat conf]# mysql -u root -p123456 -h 127.0.0.1 -P8066
MySQL [(none)]> use TESTDB
MySQL [TESTDB]> insert into t_area(id,name) values(1,'a');
MySQL [TESTDB]> insert into t_area(id,name) values(2,'b');
MySQL [TESTDB]> insert into t_area(id,name) values(3,'c');
MySQL [TESTDB]> insert into t_area(id,name) values(4,'d');
MySQL [TESTDB]> select * from t_area;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3307 -e 'select * from taobao.t_area;'
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+----+------+
[root@mycat conf]# mysql -uroot -p123 -h 192.168.1.1 -P 3308 -e 'select * from taobao.t_area;'
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+----+------+
如果需要更改MyCAT默认的逻辑库,账号密码等信息,可修改server.xml这个文件!