一、前言
通过mycat中间件在centos6.7的linux系统下实现mysql主从集群、读写分离、分库分表等,有关详细实践配置步骤整理如下。
二、操作步骤
1. 安装jdk,如安装jdk-8u192-linux-x64.tar.gzlinux(安装操作步骤参见详细配置文章)
[root@centos-cluster-s19423 soft]# cd /home/app/@b@[root@centos-cluster-s19423 soft]# wget http://dl.mycat.io/1.6-RELEASE/
[root@centos-cluster-s19423 soft]# tar -zxvf Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz @b@mycat/bin/mycat@b@mycat/bin/wrapper-linux-ppc-64@b@mycat/bin/wrapper-linux-x86-32@b@mycat/bin/wrapper-linux-x86-64@b@mycat/lib/asm-4.0.jar@b@mycat/lib/commons-collections-3.2.1.jar@b@mycat/lib/commons-lang-2.6.jar@b@mycat/lib/curator-client-2.11.0.jar@b@mycat/lib/curator-framework-2.11.0.jar@b@mycat/lib/curator-recipes-2.11.0.jar@b@mycat/lib/disruptor-3.3.4.jar@b@mycat/lib/dom4j-1.6.1.jar@b@mycat/lib/druid-1.0.26.jar@b@mycat/lib/ehcache-core-2.6.11.jar@b@mycat/lib/fastjson-1.2.12.jar@b@mycat/lib/guava-19.0.jar@b@mycat/lib/hamcrest-core-1.3.jar@b@mycat/lib/hamcrest-library-1.3.jar@b@mycat/lib/jline-0.9.94.jar@b@mycat/lib/joda-time-2.9.3.jar@b@mycat/lib/jsr305-2.0.3.jar@b@mycat/lib/kryo-2.10.jar@b@mycat/lib/leveldb-0.7.jar@b@mycat/lib/leveldb-api-0.7.jar@b@mycat/lib/libwrapper-linux-ppc-64.so@b@mycat/lib/libwrapper-linux-x86-32.so@b@mycat/lib/libwrapper-linux-x86-64.so@b@mycat/lib/log4j-1.2-api-2.5.jar@b@mycat/lib/log4j-1.2.17.jar@b@mycat/lib/log4j-api-2.5.jar@b@mycat/lib/log4j-core-2.5.jar@b@mycat/lib/log4j-slf4j-impl-2.5.jar@b@mycat/lib/mapdb-1.0.7.jar@b@mycat/lib/minlog-1.2.jar@b@mycat/lib/mongo-java-driver-2.11.4.jar@b@mycat/lib/Mycat-server-1.6.6.1-release.jar@b@mycat/lib/mysql-binlog-connector-java-0.16.1.jar@b@mycat/lib/mysql-connector-java-5.1.35.jar@b@mycat/lib/netty-3.7.0.Final.jar@b@mycat/lib/netty-buffer-4.1.9.Final.jar@b@mycat/lib/netty-common-4.1.9.Final.jar@b@mycat/lib/objenesis-1.2.jar@b@mycat/lib/reflectasm-1.03.jar@b@mycat/lib/sequoiadb-driver-1.12.jar@b@mycat/lib/slf4j-api-1.6.1.jar@b@mycat/lib/univocity-parsers-2.2.1.jar@b@mycat/lib/velocity-1.7.jar@b@mycat/lib/wrapper.jar@b@mycat/lib/zookeeper-3.4.6.jar@b@mycat/conf/wrapper.conf@b@mycat/conf/@b@mycat/conf/zkconf/@b@mycat/conf/zkdownload/@b@mycat/conf/auto-sharding-long.txt@b@mycat/conf/auto-sharding-rang-mod.txt@b@mycat/conf/autopartition-long.txt@b@mycat/conf/cacheservice.properties@b@mycat/conf/dbseq.sql@b@mycat/conf/ehcache.xml@b@mycat/conf/index_to_charset.properties@b@mycat/conf/migrateTables.properties@b@mycat/conf/myid.properties@b@mycat/conf/partition-hash-int.txt@b@mycat/conf/partition-range-mod.txt@b@mycat/conf/rule.xml@b@mycat/conf/schema.xml@b@mycat/conf/sequence_conf.properties@b@mycat/conf/sequence_db_conf.properties@b@mycat/conf/sequence_distributed_conf.properties@b@mycat/conf/sequence_time_conf.properties@b@mycat/conf/server.xml@b@mycat/conf/sharding-by-enum.txt@b@mycat/conf/zkconf/auto-sharding-long.txt@b@mycat/conf/zkconf/auto-sharding-rang-mod.txt@b@mycat/conf/zkconf/autopartition-long.txt@b@mycat/conf/zkconf/cacheservice.properties@b@mycat/conf/zkconf/ehcache.xml@b@mycat/conf/zkconf/index_to_charset.properties@b@mycat/conf/zkconf/partition-hash-int.txt@b@mycat/conf/zkconf/partition-range-mod.txt@b@mycat/conf/zkconf/rule.xml@b@mycat/conf/zkconf/schema.xml@b@mycat/conf/zkconf/sequence_conf.properties@b@mycat/conf/zkconf/sequence_db_conf.properties@b@mycat/conf/zkconf/sequence_distributed_conf-mycat_fz_01.properties@b@mycat/conf/zkconf/sequence_distributed_conf.properties@b@mycat/conf/zkconf/sequence_time_conf-mycat_fz_01.properties@b@mycat/conf/zkconf/sequence_time_conf.properties@b@mycat/conf/zkconf/server-mycat_fz_01.xml@b@mycat/conf/zkconf/server.xml@b@mycat/conf/zkconf/sharding-by-enum.txt@b@mycat/conf/zkdownload/auto-sharding-long.txt@b@mycat/version.txt@b@mycat/conf/log4j2.xml@b@mycat/bin/dataMigrate.sh@b@mycat/bin/init_zk_data.sh@b@mycat/bin/rehash.sh@b@mycat/bin/startup_nowrap.sh@b@mycat/logs/@b@mycat/catlet/
4. 环境配置&QA
root@centos-cluster-s19423 soft]# vi /etc/profile@b@export MYCAT_HOME=/home/app/mycat@b@export JAVA_HOME=/home/app/jdk1.8@b@export JRE_HOME=${JAVA_HOME}/jre@b@export CLASSPATh=.:${JAVA_HOME}/lib:${JRE_HOME}/lib@b@export PATH=${MYCAT_HOME}/bin:${JAVA_HOME}/bin:$PATH@b@root@centos-cluster-s19423 soft]# source /etc/profile@b@@b@#启动mycat@b@root@centos-cluster-s19423 soft]# mycat start@b@[root@centos-cluster-s19423 log]# tail -f /var/log/mysqld.log @b@2019-05-14T12:19:53.553304Z 0 [Warning] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a slave and has his hostname changed!! Please use '--relay-log=centos-cluster-s19423-relay-bin' to avoid this problem.@b@20
5.配置防火墙并重启mycat
[root@centos-cluster-s19423 conf]# vi/etc/sysconfig/iptables@b@# Firewall configuration written by system-config-firewall@b@# Manual customization of this file is not recommended.@b@*filter@b@:INPUT ACCEPT [0:0]@b@:FORWARD ACCEPT [0:0]@b@:OUTPUT ACCEPT [0:0]@b@-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT@b@-A INPUT -p icmp -j ACCEPT@b@-A INPUT -i lo -j ACCEPT@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT @b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 8066 -j ACCEPT@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT@b@-A INPUT -j REJECT --reject-with icmp-host-prohibited@b@-A FORWARD -j REJECT --reject-with icmp-host-prohibited@b@COMMIT@b@[root@centos-cluster-s19423 conf]# service iptables restart
6. 配置mycat的conf目录下的schema.xml、server.xml、rule.xml 配置文件 并重启mycat
schema.xml - mysql分片1(dataNode数据节点1,单机,192.168.1.204,mytest1库)+mysql分片2(dataNode数据节点2,单机,192.168.1.205,mytest1库)
<?xml version="1.0"?>@b@<!DOCTYPE mycat:schema SYSTEM "schema.dtd">@b@<mycat:schema xmlns:mycat="http://io.mycat/">@b@@b@ <schema name="mytest1" checkSQLschema="false" sqlMaxLimit="100"> @b@ <table name="tb_test" dataNode="dn1,dn2" rule="mod-long"/> @b@ </schema> @b@@b@ <dataNode name="dn1" dataHost="dhost1" database="mytest1" /> @b@ <dataNode name="dn2" dataHost="dhost2" database="mytest1" />@b@@b@ <dataHost name="dhost1" maxCon="1000" minCon="10" balance="0"@b@ writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">@b@ <heartbeat>select user()</heartbeat> @b@@b@ <writeHost host="hostM1" url="192.168.1.204:3306" user="root" @b@ password="123456">@b@ </writeHost> @b@ @b@@b@ </dataHost> @b@@b@ <dataHost name="dhost2" maxCon="1000" minCon="10" balance="0"@b@ writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">@b@ <heartbeat>select user()</heartbeat>@b@@b@ <writeHost host="hostM2" url="192.168.1.205:3306" user="root"@b@ password="123456">@b@ </writeHost>@b@@b@@b@ </dataHost>@b@ @b@</mycat:schema>
sqlMaxLimit配置默认查询数量@b@database为真实数据库名@b@balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost 上。@b@balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1 ->S1 , M2->S2,并且 M1 与 M2 互为主备),正常情况下, M2,S1,S2 都参与 select 语句的负载均衡。@b@balance="2",所有读操作都随机的在 writeHost、 readhost 上分发。@b@balance="3", 所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 没有。@b@writeType="0", 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .@b@writeType="1",所有写操作都随机的发送到配置的 writeHost。@b@writeType="2",没实现。@b@-1 表示不自动切换@b@1 默认值,自动切换@b@2 基于MySQL 主从同步的状态决定是否切换
server.xml
<?xml version="1.0" encoding="UTF-8"?>@b@<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); @b@ - you may not use this file except in compliance with the License. - You @b@ may obtain a copy of the License at - - @b@<!DOCTYPE mycat:server SYSTEM "server.dtd">@b@<mycat:server xmlns:mycat="http://io.mycat/">@b@ <system>@b@ <property name="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户-->@b@ <property name="useHandshakeV10">1</property>@b@ <property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->@b@ <property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->@b@@b@ <property name="sequnceHandlerType">2</property>@b@ <property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的情况下,检查关联字段中是否有分片字段 .默认 false -->@b@ <!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->@b@ <!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->@b@ <!-- <property name="processorBufferChunk">40960</property> -->@b@ <!-- @b@ <property name="processors">1</property> @b@ <property name="processorExecutor">32</property> @b@ -->@b@ <!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->@b@ <property name="processorBufferPoolType">0</property>@b@ <!--默认是65535 64K 用于sql解析时最大文本长度 -->@b@ <!--<property name="maxStringLiteralLength">65535</property>-->@b@ <!--<property name="sequnceHandlerType">0</property>-->@b@ <!--<property name="backSocketNoDelay">1</property>-->@b@ <!--<property name="frontSocketNoDelay">1</property>-->@b@ <!--<property name="processorExecutor">16</property>-->@b@ <!--@b@ <property name="serverPort">8066</property> <property name="managerPort">9066</property> @b@ <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> @b@ <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->@b@ <!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->@b@ <property name="handleDistributedTransactions">0</property>@b@@b@ <!--@b@ off heap for merge/order/group/limit 1开启 0关闭@b@ -->@b@ <property name="useOffHeapForMerge">1</property>@b@@b@ <!--@b@ 单位为m@b@ -->@b@ <property name="memoryPageSize">64k</property>@b@@b@ <!--@b@ 单位为k@b@ -->@b@ <property name="spillsFileBufferSize">1k</property>@b@@b@ <property name="useStreamOutput">0</property>@b@@b@ <!--@b@ 单位为m@b@ -->@b@ <property name="systemReserveMemorySize">384m</property>@b@@b@@b@ <!--是否采用zookeeper协调切换 -->@b@ <property name="useZKSwitch">false</property>@b@@b@ <!-- XA Recovery Log日志路径 -->@b@ <!--<property name="XARecoveryLogBaseDir">./</property>-->@b@@b@ <!-- XA Recovery Log日志名称 -->@b@ <!--<property name="XARecoveryLogBaseName">tmlog</property>-->@b@ <!--如果为 true的话 严格遵守隔离级别,不会在仅仅只有select语句的时候在事务中切换连接-->@b@ <property name="strictTxIsolation">false</property>@b@@b@ <property name="useZKSwitch">true</property>@b@@b@ </system>@b@@b@ <!-- 全局SQL防火墙设置 -->@b@ <!--白名单可以使用通配符%或着*-->@b@ <!--例如<host host="127.0.0.*" user="root"/>-->@b@ <!--例如<host host="127.0.*" user="root"/>-->@b@ <!--例如<host host="127.*" user="root"/>-->@b@ <!--例如<host host="1*7.*" user="root"/>-->@b@ <!--这些配置情况下对于127.0.0.1都能以root账户登录-->@b@ <!--@b@ <firewall>@b@ <whitehost>@b@ <host host="1*7.0.0.*" user="root"/>@b@ </whitehost>@b@ <blacklist check="false">@b@ </blacklist>@b@ </firewall>@b@ -->@b@@b@ <user name="root" defaultAccount="true">@b@ <property name="password">123456</property>@b@ <property name="schemas">mytest1</property>@b@@b@ <!-- 表级 DML 权限设置 -->@b@ <!-- @b@ <privileges check="false">@b@ <schema name="mytest1" dml="0110" >@b@ <table name="tb_test" dml="0000"></table>@b@ <table name="tb_test" dml="1111"></table>@b@ </schema>@b@ </privileges>@b@ -->@b@ </user>@b@@b@<!--@b@ <user name="user">@b@ <property name="password">user</property>@b@ <property name="schemas">mytest1</property>@b@ <property name="readOnly">true</property>@b@ </user>@b@@b@-->@b@@b@</mycat:server>
rule.xml(mod-long的count根据schema.xml设置dataNode个数进行设置,按照个数取模后决定存储置对应数据节点所在dataHost数据库)
<?xml version="1.0" encoding="UTF-8"?>@b@@b@<!DOCTYPE mycat:rule SYSTEM "rule.dtd">@b@<mycat:rule xmlns:mycat="http://io.mycat/">@b@ ...@b@ <function name="mod-long" class="io.mycat.route.function.PartitionByMod">@b@ <!-- how many data nodes -->@b@ <property name="count">2</property>@b@ </function>@b@@b@ ...@b@</mycat:rule>
7.通过navicat连接mycat端口服务(对于mycat的conf目录下server.xml配置的默认端口及root账号密码) ,如下图所示
8.连接成功204mycat后,打开mytest1数据库并插入三条数据后,分别在打开204及205mysql数据库进行验证(如下图所示,可以看到192.168.1.204的mysql存两条数据,192.168.1.205的mysql数据库存了一条)
另外:配置mysql分片1(dataNode数据节点1,双机热备读写分离,192.168.1.204,192.168.1.205,mytest库)+mysql分片2(dataNode数据节点2,单机,192.168.1.205,mytest1库)- mysql分片1主从双机配置参考其他文章页面
schema.xml
<?xml version="1.0"?>@b@<!DOCTYPE mycat:schema SYSTEM "schema.dtd">@b@<mycat:schema xmlns:mycat="http://io.mycat/">@b@@b@ <schema name="mytest1" checkSQLschema="false" sqlMaxLimit="100"> @b@ <table name="tb_test" dataNode="dn1,dn2" rule="mod-long"/> @b@ </schema> @b@@b@ <dataNode name="dn1" dataHost="dhost1" database="mytest" /> @b@ <dataNode name="dn2" dataHost="dhost2" database="mytest1" />@b@@b@ <dataHost name="dhost1" maxCon="1000" minCon="10" balance="0"@b@ writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">@b@ <heartbeat>select user()</heartbeat> @b@@b@ <writeHost host="hostM1" url="192.168.1.204:3306" user="root" @b@ password="123456">@b@ <readHost host="hostS1" url="192.168.1.205:3306" user="root" password="123456"/>@b@ </writeHost> @b@ @b@@b@ </dataHost> @b@@b@ <dataHost name="dhost2" maxCon="1000" minCon="10" balance="0"@b@ writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">@b@ <heartbeat>select user()</heartbeat>@b@@b@ <writeHost host="hostM2" url="192.168.1.205:3306" user="root"@b@ password="123456">@b@ </writeHost>@b@@b@@b@ </dataHost>@b@@b@</mycat:schema>
插入7条数据如下图所示
最后,将mycat设置为开机自动启动
[root@centos-cluster-s19423 bin]# sudo chmod +x /etc/rc.d/rc.local @b@[root@centos-cluster-s19423 bin]# sudo vim /etc/rc.d/rc.local@b@#!/bin/sh@b@#@b@# This script will be executed *after* all the other init scripts.@b@# You can put your own initialization stuff in here if you don't@b@# want to do the full Sys V style init stuff.@b@@b@touch /var/lock/subsys/local@b@@b@/home/app/mycat/bin/mycat start &@b@@b@ @b@~@b@"/etc/rc.d/rc.local" 10L, 258C 已写入 @b@@b@#重启系统后,测试mycat已经自动启动了 @b@[root@centos-cluster-s19423 ~]# reboot@b@[root@centos-cluster-s19423 ~]# @b@Broadcast message from root@centos-cluster-s19423@b@ (/dev/pts/1) at 23:35 ...@b@@b@The system is going down for reboot NOW!@b@@b@Last login: Thu May 23 23:26:51 2019 from 192.168.1.176@b@[root@centos-cluster-s19423 ~]# mycat status@b@Mycat-server is running (2411).