2018-04-04 mysql+mycat分片环境部署

2018-04-04  本文已影响0人  张大志的博客

1、环境准备

1、操作系统:64位CentOS 6.9

2、jdk版本:1.8.0_121

3、mysql版本: 5.7.20

2、准备压缩文件和解压缩

wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
tar -xvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 

下载网址:http://www.mycat.io/

3、设置环境变量

 vim /etc/profile.d/env.sh
export PATH=/app/mycat/bin/:$PATH
source /etc/profile.d/env.sh
echo $PATH

4、配置主机名

vim /etc/hosts
192.168.100.173 server_1
192.168.101.200 server_2

5、忽略大小写

linux下部署安装mysql,表名大小写区分。需要我们手动配置/etc/my.cnf,添加lower_case_table_names=1,使 linux 环境下 mysql 忽略表名大小写,否则使用 mycat 的时候会提示找不到。

vim /etc/my.cnf
lower_case_table_names=1
service mysqld restart

6、配置schema.xml

将以下内容替换整个文件内容

vim 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"> #定义逻辑数据库的名字为TESTDB
        <table name="blog" primaryKey="id" dataNode="dn1,dn2" rule="mod-long" /> #定义要分片的表为blog规则为mod-long,mod-long在rule.xml文件中定义了分片的规则为按照id进行分片
    </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.100.173:3306" user="root" password="123456" /> #这里要注意对root用户要授权通过mycat主机可以连接到192.168.100.173:3306这个数据库
    </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.101.200:3306" user="root" password="123456" /> #这里要注意对root用户要授权通过mycat主机可以连接到192.168.100.173:3306这个数据库
    </dataHost>
</mycat:schema>

7、配置rule.xml

vim rule.xml 
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
                <!-- how many data nodes -->
                <property name="count">2</property> #将3改为2,因为只有两台mysql主机
        </function>

8、配置jvm

vim wrapper.conf
wrapper.java.additional.1=-DMYCAT_HOME=.
wrapper.java.additional.2=-server
wrapper.java.additional.3=-XX:MaxPermSize=64M
wrapper.java.additional.4=-XX:+AggressiveOpts
wrapper.java.additional.5=-XX:MaxDirectMemorySize=512m #此处要配置大一些,不然会无法启动mycat,显示内存溢出
wrapper.java.additional.6=-Dcom.sun.management.jmxremote
wrapper.java.additional.7=-Dcom.sun.management.jmxremote.port=1984
wrapper.java.additional.8=-Dcom.sun.management.jmxremote.authenticate=false
wrapper.java.additional.9=-Dcom.sun.management.jmxremote.ssl=false
wrapper.java.additional.10=-Xmx512m
wrapper.java.additional.11=-Xms512m

9、配置连接mycat的用户和密码

vim server.xml 
 <user name="root">
                <property name="password">123456</property>
                <property name="schemas">TESTDB</property>

10、设置日志级别

vim log4j2.xml 
<asyncRoot level="debug" includeLocation="true"> # 可以设置成info,debug级别是最详细的

11、创建数据库

192.168.100.173对应的mysql服务器创建数据库:db1
192.168.101.200对应的mysql服务器创建数据库:db2

12、启动mycat并连接

mycat start
mysql -uroot -p123456 -P8086 -h192.168.100.173 #连接端口为8086

13、测试

1、 建表

mysql> use TESTDB;
CREATE TABLE blog ( id int primary key,title char(20) NOT NULL) 
执行完上面的sql语句,会看到
192.168.100.173 db1下有了blog表
192.168.101.200 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逻辑数据库的话,出现的结果是:

技术分享
参考:http://www.bubuko.com/infodetail-2269198.html
https://github.com/MyCATApache/Mycat-Server
上一篇下一篇

猜你喜欢

热点阅读