说明:
1、操作系统:64位CentOS Linux release 7.2.1511 (Core)
2、jdk版本:1.8.0_121
3、mysql版本: 5.7.17
本文假设以上或者类似环境已经配置成功。
准备压缩文件
1、下载网址:http://www.mycat.io/
选择1.6版本,然后点击进入下载页面,如下图:
选择第一个,适合linux系统的
2、创建/soft目录:
[[email protected] soft]# mkdir /soft
利用WinSCP将下载好的tar包放入/soft目录
解压缩
解压
[[email protected] ~]# cd /soft
[[email protected] soft]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
移动到/soft目录
[[email protected] soft]# mv mycat /usr/local/
配置环境变量
1、打开配置文件
[[email protected] bin]# vi /etc/profile
2、在文件末尾添加:
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
保存并退出
3、使修改生效
[[email protected] bin]# source /etc/profile
4、测试是否生效
[[email protected] bin]# echo $MYCAT_HOME
配置主机名
[[email protected] bin]# vi /etc/hosts
拷贝以下代码到文件末尾:
192.168.1.70 server_1
192.168.1.71 server_2
保存并退出
忽略大小写
linux下部署安装mysql,表名大小写区分。
需要我们手动配置/etc/my.cnf,添加lower_case_table_names=1,使 linux 环境下 mysql 忽略表名大小写,否则使用 mycat 的时候会提示找不到。
因为之前配置了两台mysql服务器
192.168.1.70
192.168.1.71
所以两台都需要修改
[[email protected] soft]# vi /etc/my.cnf
将“lower_case_table_names=1”加入文件末尾,保存并退出
重启mysql服务
[[email protected] soft]# /etc/init.d/mysqld restart
配置schema.xml
[[email protected] conf]# vi schema.xml
将以下内容替换整个文件内容
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <table name="blog" primaryKey="id" dataNode="dn1,dn2" rule="mod-long" /> </schema> <dataNode name="dn1" dataHost="server1" database="db1" /> <dataNode name="dn2" dataHost="server2" database="db2" /> <dataHost name="server1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="server_1" url="192.168.1.70:3306" user="root" password="123456" /> </dataHost> <dataHost name="server2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="server_2" url="192.168.1.71:3306" user="root" password="123456" /> </dataHost> </mycat:schema>
保存并退出
配置rule.xml
因为我们就两个mysql节点,且分片规则使用的是:rule="mod-long",所以需要修改此文件内容,将mod-long默认的3个节点修改成2个,不然mycat无法启动。
既然启动失败,那么在后面的连接mycat的步骤会报以下错误:
ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘192.168.1.70‘ (111)
我曾在这里卡了一段时间,最终通过查看mycat的wrapper.log日志文件才找到原因的。
[[email protected] conf]# vi rule.xml
如下图:
----------------------------------------------------------------------------------
下面这段是日志文件里的错误信息,可以不看,写出来的原因是方便自己以后理解
STATUS | wrapper | 2017/08/19 20:13:36 | TERM trapped. Shutting down. STATUS | wrapper | 2017/08/19 20:13:37 | <-- Wrapper Stopped STATUS | wrapper | 2017/08/19 20:13:41 | --> Wrapper Started as Daemon STATUS | wrapper | 2017/08/19 20:13:41 | Launching a JVM... INFO | jvm 1 | 2017/08/19 20:13:41 | Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0 INFO | jvm 1 | 2017/08/19 20:13:41 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org INFO | jvm 1 | 2017/08/19 20:13:41 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. INFO | jvm 1 | 2017/08/19 20:13:41 | INFO | jvm 1 | 2017/08/19 20:13:42 | INFO | jvm 1 | 2017/08/19 20:13:42 | WrapperSimpleApp: Encountered an error running main: java.lang.ExceptionInInitializerError INFO | jvm 1 | 2017/08/19 20:13:42 | java.lang.ExceptionInInitializerError INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.MycatStartup.main(MycatStartup.java:53) INFO | jvm 1 | 2017/08/19 20:13:42 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) INFO | jvm 1 | 2017/08/19 20:13:42 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) INFO | jvm 1 | 2017/08/19 20:13:42 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) INFO | jvm 1 | 2017/08/19 20:13:42 | at java.lang.reflect.Method.invoke(Method.java:498) INFO | jvm 1 | 2017/08/19 20:13:42 | at org.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:240) INFO | jvm 1 | 2017/08/19 20:13:42 | at java.lang.Thread.run(Thread.java:745) INFO | jvm 1 | 2017/08/19 20:13:42 | Caused by: io.mycat.config.util.ConfigException: Illegal table conf : table [ BLOG ] rule function [ mod-long ] partition size : 3 > table datanode size : 2, please make sure table datanode size = function partition size INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.loader.xml.XMLSchemaLoader.checkRuleSuitTable(XMLSchemaLoader.java:563) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.loader.xml.XMLSchemaLoader.loadTables(XMLSchemaLoader.java:396) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.loader.xml.XMLSchemaLoader.loadSchemas(XMLSchemaLoader.java:173) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.loader.xml.XMLSchemaLoader.load(XMLSchemaLoader.java:122) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.loader.xml.XMLSchemaLoader.<init>(XMLSchemaLoader.java:83) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.loader.xml.XMLSchemaLoader.<init>(XMLSchemaLoader.java:87) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.ConfigInitializer.<init>(ConfigInitializer.java:74) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.config.MycatConfig.<init>(MycatConfig.java:72) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.MycatServer.<init>(MycatServer.java:144) INFO | jvm 1 | 2017/08/19 20:13:42 | at io.mycat.MycatServer.<clinit>(MycatServer.java:96) INFO | jvm 1 | 2017/08/19 20:13:42 | ... 7 more STATUS | wrapper | 2017/08/19 20:13:44 | <-- Wrapper Stopped
创建数据库
192.168.1.70对应的mysql服务器创建数据库:db1
[[email protected] soft]# /usr/local/mysql/bin/mysql -uroot -p123456
mysql> create database db1;
----------------------------------------------------------------------------------
192.168.1.71对应的mysql服务器创建数据库:db2
[[email protected] soft]# /usr/local/mysql/bin/mysql -uroot -p123456
mysql> create database db2;
启动mycat
[[email protected] ~]# mycat start
连接mycat
[[email protected] mycat]# /usr/local/mysql/bin/mysql -u root -p -P 8066 -h 192.168.1.70
如上图,表示逻辑数据库连接成功
说明:这里连接的就是逻辑数据库了
测试
1、 建表
选择逻辑数据库
mysql> use TESTDB;
CREATE TABLE blog ( id int(11) NOT NULL, title varchar(20) NOT NULL, author varchar(30), PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
执行完上面的sql语句,会看到
192.168.1.70 db1下有了blog表
192.168.1.71 db2下也有了blog表
2、插入数据
insert into blog(id,title) values(1,‘test‘); insert into blog(id,title) values(2,‘test‘); insert into blog(id,title) values(3,‘test‘); insert into blog(id,title) values(4,‘test‘); insert into blog(id,title) values(5,‘test‘); insert into blog(id,title) values(6,‘test‘); insert into blog(id,title) values(7,‘test‘); insert into blog(id,title) values(8,‘test‘); insert into blog(id,title) values(9,‘test‘); insert into blog(id,title) values(10,‘test‘); insert into blog(id,title) values(11,‘test‘); insert into blog(id,title) values(12,‘test‘); insert into blog(id,title) values(13,‘test‘); insert into blog(id,title) values(14,‘test‘);
3、查询
Select * from blog;
分别在server_1和server_2上执行查询语句
server_1返回的结果是:
server_2返回的结果是:
如果查询TESTDB逻辑数据库的话,出现的结果是:
好,到此大功告成,作为入门篇非常简单。
远程连接mycat
开启端口8066
[[email protected] ~]# firewall-cmd --zone=public --add-port=8066/tcp --permanent
重启防火墙
[[email protected] ~]# firewall-cmd –reload
Navicat for MySQL连接
参考文献:
http://blog.csdn.net/testcs_dn/article/details/51438216 这篇主要参考环境配置
http://maosheng.iteye.com/blog/2306937 这篇文章主要参考它的简单配置schema.xml
http://www.jb51.net/article/114290.htm 这篇文章主要参考它的简单配置schema.xml