Mycat读写分离

最近搭建了 MySQL 主从 并使用MyCat作为数据库中间件

版本:

Mysql  5.5.48

Linux :CentOS 6.8

MyCat : 1.4

节点:

192.168.152.11Cluster1

192.168.152.12Cluster2

192.168.152.13Cluster3

首先 在三台节点上都安装mysql (如何安装 自行百度)

授权登陆:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'root' WITH GRANT OPTION;

三台节点都授权远程登陆

三个节点都创建数据库 database1 、database2

----------------------------------------

配置MySQL主服务器的my.cnf文件

vim /etc/my.cnf

在[mysqld]下面加入如下内容

[sql] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. binlog-do-db=database1  
  2. binlog-do-db=database2  
  3. binlog-ignore-db=mysql  

并且 开启binlog日志

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. log-bin=mysql-bin  

以及Server-id 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. server-id       = 1  

如图:

主服务器配置OK

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. service mysql restart  

重启服务

进入Mysql命令行

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. show master statusG  

得到:

记住这里的:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. File: mysql-bin.000028  
  2.    Position: 22285  

一会要用到

配置MySQL从服务器的my.cnf文件

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. replicate-do-db=database1  
  2. replicate-do-db=database2  
  3. replicate-ignore-db=mysql  




以及修改serverid

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. server-id       = 2  


另一台从服务器同理 ServerId 为3 

重启Mysql服务

注意:MySQL 5.1.7版本之后,已经不支持把master配置属性写入my.cnf配置文件中了,只需要把同步的数据库和要忽略的数据库写入即可。

Mysql命令行 进入Mysql 在 Cluster2、Cluster3上执行

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. slave stop;   #停止slave同步进程  

然后执行:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. change master to master_host='192.168.152.11',master_user='root',master_password='root',master_log_file='mysql-bin.000028' ,master_log_pos=22285;  

注意这里的 

master_log_file='mysql-bin.000028' ,master_log_pos=22285;

一定要对应

配置Ok后执行:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. slave start;    #开启slave同步进程  

开启Slave

在执行:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. SHOW SLAVE STATUSG   #查看slave同步信息,出现以下内容  

注意查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上这两个参数的值为Yes,即说明配置成功!

OK主从复制搭建成功

接下来 安装Mycat1.4 下载地址:http://www.mycat.org.cn/  点击这里直接下载

解压Mycat

得到:

接下来进行配置读写分离

首先 :

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. vim wrapper.conf   

配置Java路径

然后 编辑

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. vim schema.xml  

修改内容如下:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">  
  3. <mycat:schema xmlns:mycat="http://org.opencloudb/">  
  4.   
  5.         <schema name="database1" checkSQLschema="false" sqlMaxLimit="100" dataNode="database1"></schema>  
  6.   
  7.         <dataNode name="database1" dataHost="localhost1" database="database1" />  
  8.   
  9.         <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">  
  10.                 <heartbeat>select user()</heartbeat>  
  11.                 <writeHost host="cluster1" url="192.168.152.11:3306" user="root" password="root">  
  12.                         <readHost host="cluster2" url="192.168.152.12:3306" user="root" password="root" />  
  13.                         <readHost host="cluster3" url="192.168.152.13:3306" user="root" password="root" />  
  14.                 </writeHost>  
  15.         </dataHost>  
  16. </mycat:schema>  

简单说明一下:

编辑账户
vim server.xml

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mycat:server SYSTEM "server.dtd">  
  3. <mycat:server xmlns:mycat="http://org.opencloudb/">  
  4.         <system>  
  5.                 <!--   
  6.                         <property name="mutiNodeLimitType">1</property> 0:开启小数量级(默认) ;1:开启亿级数据排序  
  7.                         <property name="mutiNodePatchSize">100</property> 亿级数量排序批量  
  8.                         <property name="processors">32</property<property name="processorExecutor">32</property>   
  9.                         <property name="serverPort">8066</property<property name="managerPort">9066</property>   
  10.                         <property name="idleTimeout">300000</property<property name="bindIp">0.0.0.0</property>   
  11.                         <property name="frontWriteQueueSize">4096</property<property name="processors">32</property> -->  
  12.         </system>  
  13.   
  14.         <user name="admin">  
  15.                 <property name="password">admin</property>  
  16.                 <property name="schemas">database1</property>  
  17.         </user>  
  18. </mycat:server>  

这里的 的schemas 一定要和前面的 对应

配置OK

bin/mycat start

启动Mycat

连接MyCat

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. mysql -h127.0.0.1 -uadmin -padmin -P8066  


Ok 配置结束

Mysql 主从复制 + MyCat读写分离 配置就OK了

原文地址:https://www.cnblogs.com/yueminghai/p/6479099.html