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

来源:互联网 发布:海澜之家淘宝 编辑:程序博客网 时间:2024/05/20 19:45

mycat是最近很火的一款国人发明的分布式数据库中间件,它是基于阿里的cobar的基础上进行开发的

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

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


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


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

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


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

设置mycat的环境变量

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

使配置文件立即生效

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


进入mycat的配置文件目录

[html] view plain copy
print?
  1. cd /usr/local/mycat/conf/  
cd /usr/local/mycat/conf/
[html] view plain copy
print?
  1. vi schema.xml  
vi schema.xml
这个配置文件主要是用来配置数据库节点,逻辑表等等东西的

[html] view plain copy
print?
  1. <?xml version=“1.0”?>  
  2. <!DOCTYPE mycat:schema SYSTEM “schema.dtd”>  
  3. <mycat:schema xmlns:mycat=“http://org.opencloudb/”>  
  4.       
  5.     <!– 定义MyCat的逻辑库 –>  
  6.     <schema name=“pcx_schema” checkSQLschema=“false” sqlMaxLimit=“100” dataNode=“pcxNode”></schema>  
  7.   
  8.     <!– 定义MyCat的数据节点 –>  
  9.     <dataNode name=“pcxNode” dataHost=“dtHost” database=“pcx” />  
  10.   
  11.       
  12.     <!– 定义数据主机dtHost,连接到MySQL读写分离集群 ,schema中的每一个dataHost中的host属性值必须唯一–>  
  13.     <!– dataHost实际上配置就是后台的数据库集群,一个datahost代表一个数据库集群 –>  
  14.     <!– balance=”1”,全部的readHost与stand by writeHost参与select语句的负载均衡–>  
  15.     <!– writeType=”0”,所有写操作发送到配置的第一个writeHost,这里就是我们的hostmaster,第一个挂了切到还生存的第二个writeHost–>  
  16.     <dataHost name=“dtHost” maxCon=“500” minCon=“20” balance=“1”  
  17.         writeType=“0” dbType=“mysql” dbDriver=“native” switchType=“2” slaveThreshold=“100”>  
  18.         <!–心跳检测 –>  
  19.         <heartbeat>show slave status</heartbeat>  
  20.           
  21.         <!–配置后台数据库的IP地址和端口号,还有账号密码 –>  
  22.         <writeHost host=“hostMaster” url=“192.168.1.6:3306” user=“root” password=“root” />  
  23.         <writeHost host=“hostSlave” url=“192.168.1.7:3306” user=“root” password=“root” />  
  24.     </dataHost>  
  25.       
  26.   
  27. </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?
  1. vi server.xml  
 vi server.xml
[html] view plain copy
print?
  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <!– - - Licensed under the Apache License, Version 2.0 (the “License”);   
  3.     - you may not use this file except in compliance with the License. - You   
  4.     may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0   
  5.     - - Unless required by applicable law or agreed to in writing, software -   
  6.     distributed under the License is distributed on an “AS IS” BASIS, - WITHOUT   
  7.     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the   
  8.     License for the specific language governing permissions and - limitations   
  9.     under the License. –>  
  10. <!DOCTYPE mycat:server SYSTEM “server.dtd”>  
  11. <mycat:server xmlns:mycat=“http://org.opencloudb/”>  
  12.     <system>  
  13.     <!– 这里配置的都是一些系统属性,可以自己查看mycat文档 –>  
  14.     <property name=“defaultSqlParser”>druidparser</property>  
  15.     <property name=“charset”>utf8mb4</property>  
  16.     </system>  
  17.   
  18.       
  19.                               
  20.     <!– 用户1,对应的MyCat逻辑库连接到的数据节点对应的主机为主从复制集群 –>  
  21.     <user name=“user1”>  
  22.         <property name=“password”>root</property>  
  23.         <property name=“schemas”>pcx_schema</property>  
  24.     </user>  
  25.                               
  26.     <!– 用户2,只读权限–>  
  27.     <user name=“user2”>  
  28.         <property name=“password”>root</property>  
  29.         <property name=“schemas”>pcx_schema</property>  
  30.         <property name=“readOnly”>true</property>  
  31.     </user>  
  32.   
  33. </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?
  1. vi /etc/sysconfig/iptables  
 vi /etc/sysconfig/iptables
[html] view plain copy
print?
  1. -A INPUT -m state –state NEW -m tcp -p tcp –dport 8066 -j ACCEPT  
  2. -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?
  1. service iptables restart  
service iptables restart
[html] view plain copy
print?
  1. cd /usr/local/mycat/bin/  
cd /usr/local/mycat/bin/

进入mycat的脚本目录


运行启动命令

[html] view plain copy
print?
  1. ./mycat start  
 ./mycat start
我们可以使用mysql客户端连接或者navicat来连接mycat




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

进入mycat的日志目录

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



先测试一下读操作

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


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

那么我们执行多次看看

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


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



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


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


记录成功的同步过来了,可见读写分离搭建成功。

原创粉丝点击