您的位置:首页 > 数据库 > MySQL

用mycat做读写分离:基于 MySQL主从复制

2017-09-07 10:42 911 查看
mycat是最近很火的一款国人发明的分布式数据库中间件,它是基于阿里的cobar的基础上进行开发的

搭建之前我们先要配置mysql的主从复制,这个过程很长,我这里就不写了,有兴趣的可以看看我写的这篇文章。

linux centos下mysql数据库的主从复制环境搭建

mycat在应用当中的作用可以看下图



mycat可以让程序员只需要关心业务代码的编写,而不用担心后端数据库集群的负载均衡,读写分离,分库分表的数据分片逻辑的编写,只要直接连接mycat就可以了

首先我们准备一台干净的centos机器,安装好jdk



解压mycat的安装包到/user/local/下



设置mycat的环境变量

[html] view plain copy print?vi /etc/profile
vi /etc/profile




使配置文件立即生效

[html] view plain copy print?source /etc/profile
source /etc/profile


进入mycat的配置文件目录

[html] view plain copy print?cd /usr/local/mycat/conf/
cd /usr/local/mycat/conf/
[html] view plain copy print?vi schema.xml
vi schema.xml

这个配置文件主要是用来配置数据库节点,逻辑表等等东西的

[html] view plain copy print?<?xml version=“1.0”?>
<!DOCTYPE mycat:schema SYSTEM “schema.dtd”>
<mycat:schema xmlns:mycat=“http://org.opencloudb/”>

<!– 定义MyCat的逻辑库 –>
<schema name=“pcx_schema” checkSQLschema=“false” sqlMaxLimit=“100” dataNode=“pcxNode”></schema>

<!– 定义MyCat的数据节点 –>
<dataNode name=“pcxNode” dataHost=“dtHost” database=“pcx” />

<!– 定义数据主机dtHost,连接到MySQL读写分离集群 ,schema中的每一个dataHost中的host属性值必须唯一–>
<!– dataHost实际上配置就是后台的数据库集群,一个datahost代表一个数据库集群 –>
<!– balance=”1”,全部的readHost与stand by writeHost参与select语句的负载均衡–>
<!– writeType=”0”,所有写操作发送到配置的第一个writeHost,这里就是我们的hostmaster,第一个挂了切到还生存的第二个writeHost–>
<dataHost name=“dtHost” maxCon=“500” minCon=“20” balance=“1”
writeType=“0” dbType=“mysql” dbDriver=“native” switchType=“2” slaveThreshold=“100”>
<!–心跳检测 –>
<heartbeat>show slave status</heartbeat>

<!–配置后台数据库的IP地址和端口号,还有账号密码 –>
<writeHost host=“hostMaster” url=“192.168.1.6:3306” user=“root” password=“root” />
<writeHost host=“hostSlave” url=“192.168.1.7:3306” user=“root” password=“root” />
</dataHost>

</mycat:schema>
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">

<!-- 定义MyCat的逻辑库 -->
<schema name="pcx_schema" checkSQLschema="false" sqlMaxLimit="100" dataNode="pcxNode"></schema>

<!-- 定义MyCat的数据节点 -->
<dataNode name="pcxNode" dataHost="dtHost" database="pcx" />

<!-- 定义数据主机dtHost,连接到MySQL读写分离集群 ,schema中的每一个dataHost中的host属性值必须唯一-->
<!-- dataHost实际上配置就是后台的数据库集群,一个datahost代表一个数据库集群 -->
<!-- balance="1",全部的readHost与stand by writeHost参与select语句的负载均衡-->
<!-- writeType="0",所有写操作发送到配置的第一个writeHost,这里就是我们的hostmaster,第一个挂了切到还生存的第二个writeHost-->
<dataHost name="dtHost" maxCon="500" minCon="20" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
<!--心跳检测 -->
<heartbeat>show slave status</heartbeat>

<!--配置后台数据库的IP地址和端口号,还有账号密码 -->
<writeHost host="hostMaster" url="192.168.1.6:3306" user="root" password="root" />
<writeHost host="hostSlave" url="192.168.1.7:3306" user="root" password="root" />
</dataHost>

</mycat:schema>
接下来配置用户权限,系统变量

[html] view plain copy print?vi server.xml
vi server.xml
[html] view plain copy print?<?xml version=“1.0” encoding=“UTF-8”?>
<!– - - Licensed under the Apache License, Version 2.0 (the “License”);
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an “AS IS” BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. –>
<!DOCTYPE mycat:server SYSTEM “server.dtd”>
<mycat:server xmlns:mycat=“http://org.opencloudb/”>
<system>
<!– 这里配置的都是一些系统属性,可以自己查看mycat文档 –>
<property name=“defaultSqlParser”>druidparser</property>
<property name=“charset”>utf8mb4</property>
</system>

<!– 用户1,对应的MyCat逻辑库连接到的数据节点对应的主机为主从复制集群 –>
<user name=“user1”>
<property name=“password”>root</property>
<property name=“schemas”>pcx_schema</property>
</user>

<!– 用户2,只读权限–>
<user name=“user2”>
<property name=“password”>root</property>
<property name=“schemas”>pcx_schema</property>
<property name=“readOnly”>true</property>
</user>

</mycat:server>
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://org.opencloudb/">
<system>
<!-- 这里配置的都是一些系统属性,可以自己查看mycat文档 -->
<property name="defaultSqlParser">druidparser</property>
<property name="charset">utf8mb4</property>
</system>

<!-- 用户1,对应的MyCat逻辑库连接到的数据节点对应的主机为主从复制集群 -->
<user name="user1">
<property name="password">root</property>
<property name="schemas">pcx_schema</property>
</user>

<!-- 用户2,只读权限-->
<user name="user2">
<property name="password">root</property>
<property name="schemas">pcx_schema</property>
<property name="readOnly">true</property>
</user>

</mycat:server>
修改防火墙,允许mycat的端口被外界访问

[html] view plain copy print?vi /etc/sysconfig/iptables
vi /etc/sysconfig/iptables
[html] view plain copy print?-A INPUT -m state –state NEW -m tcp -p tcp –dport 8066 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 9066 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8066 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9066 -j ACCEPT


然后重启防火墙

[html] view plain copy print?service iptables restart
service iptables restart

[html] view plain copy print?cd /usr/local/mycat/bin/
cd /usr/local/mycat/bin/


进入mycat的脚本目录



运行启动命令

[html] view plain copy print?./mycat start
./mycat start

我们可以使用mysql客户端连接或者navicat来连接mycat





接下来我们测试一下读写分离

进入mycat的日志目录

[html] view plain copy print?cd /usr/local/mycat/logs/
cd /usr/local/mycat/logs/




先测试一下读操作

我们连接到mycat发送一句select *命令试试





可以看到select 操作被路由到了192.168.1.7也就是我们的slave节点

那么我们执行多次看看



结果还是被路由到了读节点

接下来我们测试一下写操作





可见插入被路由到了master节点

最后我们看看master的数据是否被同步到slave



记录成功的同步过来了,可见读写分离搭建成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: