mycat基于java运行,检查java版本
[root@node119 ~]# java -version
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-b10)
OpenJDK 64-Bit Server VM (build 25.171-b10, mixed mode)
配置java变量
[root@node119 ~]# vim /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
mycat下载地址
http://dl.mycat.io/
cd /opt
tar zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
大致架构图
分片节点与节点主机的概念稍微有点不好理解,举例来说,一张100W的表被分为两张表,前50W在A主机的数据库中,后50W在B主机的数据库中,表所在的数据库就称这张表的分片节点;一个主机中不可能只有一张表,会有许多的分片表放在该主机上,该主机就称为节点主机,即datanode=database@datahost
mycat原理的简单说明:
拦截--->分析--->转发--->结果处理--->返回给用户
mycat分析大致包括:分片分析,路由分析,读写分离分析,缓存分析等,在接下来的实验中会通过分析log来查看mycat如何工作。
有一个库3张表
MySQL [mycat01]> show tables;
+-------------------+
| Tables in mycat01 |
+-------------------+
| t1 |
| t2 |
| t3 |
+-------------------
t1在mysql集群中
t2在percona集群中
t3 id取模1024小于1024的在mysql集群中,大于1024则在percona集群中
t1 writehost 为node115
stand by writehost 为node116
readhost 为node117
t2 writehost 为node126
readhost 为node123 node125
#这里创建了两个mycat用户(注意:与后端mysql用户无关)root具有读写权限,user具有读权限
[root@node119 ~]# vim /opt/mycat/conf/server.xml
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">mycat01</property>
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">mycat01</property>
<property name="readOnly">true</property>
</user>
balance="0" 不开启读写分离,即所有读写操作都发送到writehost上
balance="1" 全部的readhost与stand by writehost都参与select语句的负载,也就是互为主从模式下
balance="2" 所有读写操作在readhost和writehost上随机分发
balance="3" 所有读请求都发送到与writehost对应的readhost上,减轻writehost压力
writeType="0" 所有的写操作发送到第一个writehost,当第一个writehots down后则切换到第二个,且不会回切
[root@node119 ~]# vim /opt/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="mycat01" checkSQLschema="false" sqlMaxLimit="100" > #schema name就是映射出去的逻辑库名称,也就是访问mycat用户看到的名称
<table name="t1" primaryKey="id" type="global" dataNode="MySQL" /> #t1所在的分片节点为mysql,也就是说mycat只会从mysql集群中去找t1
<table name="t2" primaryKey="id" type="global" dataNode="MySQL_galera" /> #通上
<table name="t3" primaryKey="id" dataNode="MySQL_galera,MySQL" rule="rule1" /> #mycat 会从两个分片节点去找t3
</schema>
<dataNode name="MySQL" dataHost="MySQL_HOST" database="yuyang" />
<dataNode name="MySQL_galera" dataHost="MySQL_galera" database="yuyang" />
<dataHost name="MySQL_HOST" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="node115" url="192.168.31.115:3306" user="mycat" password="Mirror-12345">
<readHost host="node117" url="192.168.31.117:3306" user="mycat" password="Mirror-12345" />
</writeHost>
<writeHost host="node116" url="192.168.31.116:3306" user="mycat" password="Mirror-12345">
</writeHost>
</dataHost>
<dataHost name="MySQL_galera" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="node126" url="192.168.31.126:3306" user="mycat" password="Mirror-12345">
<readHost host="node123" url="192.168.31.123:3306" user="mycat" password="Mirror-12345" />
<readHost host="node125" url="192.168.31.125:3306" user="mycat" password="Mirror-12345" />
</writeHost>
[root@node119 ~]# vim /opt/mycat/conf/rule.xml
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">2</property>
<property name="partitionLength">512</property>
Mycat读写分离以及拆库拆表综合实验2:部署配置mycat读写分离与拆库拆表
原文地址:http://blog.51cto.com/2143754/2119815