MySQL 部署分布式架构 MyCAT (二)

安装 MyCAT

安装 java 环境(db1)

yum install -y java

下载 Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

官网地址在:http://dl.mycat.io/
把 软件上传至 /software 上

cd /data/
tar zxf /software/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

vi /etc/profile
export PATH=/data/mycat/bin:$PATH

source /etc/profile

mycat 配置文件目录介绍

logs目录:
wrapper.log       ---->mycat启动日志
mycat.log         ---->mycat详细工作日志
conf目录:
schema.xml      
主配置文件(读写分离、高可用、分布式策略定制、节点控制)
server.xml
mycat软件本身相关的配置
rule.xml 
分片规则配置文件,记录分片规则列表、使用方法等

测试数据准备(db1)

mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'192.168.31.%' identified by '123';"
mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'192.168.31.%' identified by '123';"

mysql -S /data/3307/mysql.sock
create database klvchen;
CREATE TABLE klvchen.t1
(
PersonID int,
Name varchar(255)
);
insert into klvchen.t1 values(1,'lucy'),(2,'lily'),(3,'james');

mysql -S /data/3308/mysql.sock
create database klvchen;
CREATE TABLE klvchen.t1
(
PersonID int,
Name varchar(255)
);
insert into klvchen.t1 values(1,'lucy'),(2,'lily'),(3,'james');

配置 MyCAT 读写分离

配置 schema.xml (db1)

cd /data/mycat/conf
mv schema.xml schema.xml.ori
vi 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= "klvchen" />  
    <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.31.205:3307" user="root" password="123"> 
            <readHost host="db2" url="192.168.31.205:3309" user="root" password="123" /> 
    </writeHost> 
    </dataHost>  
</mycat:schema>

测试(db1)

# 启动
mycat start

# 连接mycat:
mysql -uroot -p123456 -h 127.0.0.1 -P8066

select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           9 |
+-------------+

begin; select @@server_id;commit;
+-------------+
| @@server_id |
+-------------+
|           7 |
+-------------+
1 row in set (0.00 sec)

配置讲解

# 逻辑库:schema
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> 
</schema>  

# 数据节点:datanode
<dataNode name="dn1" dataHost="localhost1" database= "klvchen" />

# 数据主机: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.31.205:3307" user="root" password="123"> 
            <readHost host="db2" url="192.168.31.205:3309" user="root" password="123" /> 
    </writeHost> 
    </dataHost>  

# 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": 如果配置了这个属性 writeHost 下面的 readHost 仍旧可用,默认 0 可配置(0、1)
<heartbeat>select user()</heartbeat> : 监测心跳

配置 MyCAT 读写分离高可用

配置 schema.xml (db1)

cd /data/mycat/conf
cp schema.xml schema.xml.rw

vi 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= "klvchen" />  
    <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.31.205:3307" user="root" password="123"> 
            <readHost host="db2" url="192.168.31.205:3309" user="root" password="123" /> 
    </writeHost> 
    <writeHost host="db3" url="192.168.31.206:3307" user="root" password="123"> 
            <readHost host="db4" url="192.168.31.206:3309" user="root" password="123" /> 
    </writeHost>   
    </dataHost>  
</mycat:schema>

mycat restart

测试(db1)

mysql -uroot -p123456 -h 127.0.0.1 -P8066

select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           9 |
+-------------+

select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          19 |
+-------------+

select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          17 |
+-------------+

begin; select @@server_id;commit;
+-------------+
| @@server_id |
+-------------+
|           7 |
+-------------+

# 关闭真正的 writehost (db1)
systemctl stop mysqld3307

# 测试
mysql -uroot -p123456 -h 127.0.0.1 -P8066
select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          19 |
+-------------+

select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          19 |
+-------------+

begin; select @@server_id;commit;
+-------------+
| @@server_id |
+-------------+
|          17 |
+-------------+

begin; select @@server_id;commit;
+-------------+
| @@server_id |
+-------------+
|          17 |
+-------------+

结论

真正的 writehost:负责写操作的writehost  
standby  writeHost  :和readhost一样,只提供读服务

当写节点宕机后,后面跟的readhost也不提供服务,这时候standby的writehost就提供写服务,
后面跟的readhost提供读服务

posted @ 2019-12-12 20:47  klvchen  阅读(445)  评论(0编辑  收藏  举报