1.下载mycat:
https://raw.githubusercontent.com/MyCATApache/Mycat-download/master/1.5-RELEASE/Mycat-server-1.5-RELEASE-20160301083012-linux.tar.gz
2.安装mycat:
tar -zxvf Mycat-server-1.5-GA-20160225120029-linux.tar.gz
mv mycat /srv/mycat-1.5
ln -s mycat-1.5 mycat
3.配置java环境变量:
which java
在bin/startup_nowrap.sh文件中找到noJavaHome=false行下并增加如下内容:
JAVA_HOME=Java_home的实际路径
4.配置conf/schema.xml文件:
该文件主要配置读写分离数据库的连接地址信息,具体如下:
root@ebj-rd-02:/srv/mycat/conf# cat schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="mydb" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"/>
<dataNode name="dn1" dataHost="localhost1" database="test" />
<dataHost name="localhost1" maxCon="1000" minCon="50" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="-1" >
<heartbeat>select user()</heartbeat>
<writeHost host="host_3320" url="127.0.0.1:3320" user="root" password="password">
<readHost host="host_3321" url="127.0.0.1:3321" user="root" password="password"/>
<readHost host="host_3322" url="127.0.0.1:3322" user="root" password="password"/>
</writeHost>
</dataHost>
</mycat:schema>
文件终于参数说明:
这里mydb 就是我们对外声称的我们有数据库的名称 必须和server.xml中的用户指定的数据库名称一致
添加一个dataNode="dn1" 是指定了我们这个库只在dn1上.没有进行分库
sqlMaxLimit表示sql语句最大返回的结果集的大小;
writeHost和readHost配置的是实际的读数据库和写数据库的IP地址端口,用户名及密码;
balance 属性负载均衡类型,目前的取值有 4 种:
1. balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。
2. balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,
简单的说,当双主双从模式(M1 ->S1 , M2->S2,并且 M1 与 M2 互为主备),正常情况下, M2,S1,S2 都参与 select 语句的负载均衡。
3. balance="2",所有读操作都随机的在 writeHost、 readhost 上分发。
4. balance="3", 所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 没有。
writeType 属性,目前的取值有 3 种:
1. writeType="0", 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个
writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
2. writeType="1",所有写操作都随机的发送到配置的 writeHost。
3. writeType="2",没实现。
switchType 属性
- -1 表示不自动切换
- 1 默认值,自动切换
- 2 基于MySQL 主从同步的状态决定是否切换
5配置 server.xml
该文件主要配置的是mycat的用户名和密码及mycat的端口号信息配置,mycat的用户名和密码和mysql的用户名密码是分开的,应用连接mycat就用这个用户名和密码。
root@ebj-rd-02:/srv/mycat/conf# cat server.xml
<?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>
<property name="defaultSqlParser">druidparser</property>
<property name="serverPort">18066</property>
<property name="managerPort">19066</property>
<property name="charset">utf8</property>
<property name="idleTimeout">30000000</property>
<!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
<!-- <property name="processorBufferChunk">40960</property> -->
<!--
<property name="processors">1</property>
<property name="processorExecutor">32</property>
-->
<!--默认是65535 64K 用于sql解析时最大文本长度 -->
<!--<property name="maxStringLiteralLength">65535</property>-->
<!--<property name="sequnceHandlerType">0</property>-->
<!--<property name="backSocketNoDelay">1</property>-->
<!--<property name="frontSocketNoDelay">1</property>-->
<!--<property name="processorExecutor">16</property>-->
<!--
<property name="mutiNodeLimitType">1</property> 0:开启小数量级(默认) ;1:开启亿级数据排序
<property name="mutiNodePatchSize">100</property> 亿级数量排序批量
<property name="processors">32</property> <property name="processorExecutor">32</property>
<property name="serverPort">8066</property> <property name="managerPort">9066</property>
<property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
</system>
<user name="test">
<property name="password">test_123</property>
<property name="schemas">mydb</property>
</user>
<user name="user">
<property name="password">user_123</property>
<property name="schemas">mydb</property>
<property name="readOnly">true</property>
</user>
<!--
<quarantine>
<whitehost>
<host host="127.0.0.1" user="mycat"/>
<host host="127.0.0.2" user="mycat"/>
</whitehost>
<blacklist check="false"></blacklist>
</quarantine>
-->
</mycat:server>
文件终于参数说明:
serverPort设置mycat的服务端口,默认为8066;
managerPort设置mycat的管理端口,默认为9066;
idleTimeout设置mycat的空闲超时时间;
user设置mycat的访问相关schema的用户名及密码;
6.配置日志文件的路径及日志级别:
root@ebj-rd-02:/srv/mycat/conf# cat log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2012 Alibaba Group.
-
- 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 log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MM-dd HH:mm:ss.SSS} %5p [%t] (%F:%L) -%m%n" />
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="/data/log/mycat/mycat.log" />
<param name="Append" value="false"/>
<param name="MaxFileSize" value="1000KB"/>
<param name="MaxBackupIndex" value="10"/>
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MM/dd HH:mm:ss.SSS} %5p [%t] (%F:%L) -%m%n" />
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="FILE" />
<!--<appender-ref ref="FILE" />-->
</root>
</log4j:configuration>
文件终于参数说明:
<level value="debug" />表示设置mycat的日志级别为调试模式,默认为info;
<param name="file" value="/data/log/mycat/mycat.log" />表示设置mycat的日志文件路径为/data/log/mycat/mycat.log
7.mycat日常管理
root@ebj-rd-02:/srv/mycat# ./bin/mycat --help
Usage: ./bin/mycat { console | start | stop | restart | status | dump }
登录mycat读写分离服务:
mysql -utest -ptest_123 -P18066 -h127.0.0.1
登录mycat管理端:
mysql -utest -ptest_123 -P19066 -h127.0.0.1
在事务中,通过/*balance*/强制读取主数据库数据:
MySQL [mydb]> begin;
MySQL [mydb]> /*balance*/select * from mytest1;
MySQL [mydb]> commit;
查看mycat管理端的命令帮助:
MySQL [(none)]> show @@help;
8.部署中的异常处理:
MySQL [(none)]> use test;
Database changed
MySQL [test]> show tables;
ERROR 1184 (HY000): Unknown charsetIndex:192
MySQL [test]> select * from mytest;
ERROR 1184 (HY000): Unknown charsetIndex:192
MySQL [test]> exit
该异常是字符集问题引起的数据库不能连接成功
在conf/index_to_charset.properties文件中增加与自己mysql服务器匹配的字符集即可:
root@ebj-rd-02:/srv/mycat/conf# tail -1 index_to_charset.properties
192=utf8
root@ebj-rd-02:/srv/mycat/conf#