数据库中间件mycat测试之一

来源:互联网 发布:大唐电信数据所地址 编辑:程序博客网 时间:2024/06/05 15:17

一、Mycat

根据官网的定义:Mycat是一个可以用于MySQL读写分离和高可用的中间件,一个模拟为MySQL Server的超级数据库,一个能平滑扩展支持1000亿大表的分布式数据库系统,一个可管控多种关系数据库的数据库路由器,一个平滑从关系数据升级到大数据的应用中间件。其前身是阿里的Cobar项目。 
介绍就不多说了,可以看官网http://www.mycat.org.cn/ 
接下来我们实践吧。

二、mysql配置

mysql节点A机器 
操作系统 : centos6.5 x64 
数据库 : mysql-5.1.73 
mycat版本 :1.5.1 release 
数据库名 : db1 
hostname: localhost.localdomain 
ip:192.168.1.201

mysql节点B机器 
操作系统 : centos6.5 x64 
数据库 : mysql-5.1.73 
mycat版本 :1.5.1 release 
数据库名 : db2 
hostname: 202.localdomain 
ip:192.168.1.202

开始前,要先建立实体库,逻辑库要在实体库上做分表。分别在两台机器上执行创建对应的库 
A上:CREATE database db1; 
B上:CREATE database db2; 
另外注意linux下数据库的名称是大小写敏感的,设置my.cnf内追加一行,使得都转为小写,这样在分库的时候就统一起来了 
vim /etc/my.cnf 
lower_case_table_names = 1

三、安装mycat

1.安装

我们将mycat安装在A机器上 
由于官方的地址下载后无法解压,可能是他们编译出错了吧,所以我另外找了一个http://download.csdn.net/detail/socho/9571201,下载1.5.1版本,得到Mycat-server-1.5.1-RELEASE-20160509173344-linux.tar.gz 
然后一系列操作,你们都懂

tar -xzvf Mycat-server-1.5.1-RELEASE-20160509173344-linux.tar.gzmkdri /home/mycat/appmv mycat /usr/local/
  • 1
  • 2
  • 3

设置环境变量

vim /etc/profileexport MYCAT_HOME=/usr/local/mycatPATH=$PATH:$MYCAT_HOME/bin
  • 1
  • 2
  • 3

令修改生效

[mycat@c1 ~]$ source /etc/profile
  • 1

测试是否配置成功

[mycat@c1 ~]$ echo $MYCAT_HOME/usr/local/mycat
  • 1
  • 2

配置逻辑库(schema)和逻辑表(table),打开conf/schema.xml 每个属性的含义请参考权威指南,这里给出基本的

<?xml version="1.0"?><!DOCTYPE mycat:schema SYSTEM "schema.dtd"><mycat:schema xmlns:mycat="http://org.opencloudb/" >    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">        <!-- 需要分片的表,在节点dn1,dn2上分片,分片规则是auto-sharding-long -->        <table name="travelrecord" dataNode="dn1,dn2" rule="auto-sharding-long" />        <table name="company" primaryKey="ID" dataNode="dn2,dn1" rule="mod-long"/>        <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />        <table name="hotnews" primaryKey="ID" dataNode="dn1,dn2" rule="mod-long" />        <table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" />        <table name="customer" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile">            <childTable name="orders" primaryKey="ID" joinKey="customer_id" parentKey="id">                <childTable name="order_items" joinKey="order_id" parentKey="id" />            </childTable>            <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" parentKey="id" />        </table>    </schema>    <dataNode name="dn1" dataHost="201.liberalman.cn" database="db1" />    <dataNode name="dn2" dataHost="202.liberalman.cn" database="db2" />    <dataHost name="201.liberalman.cn" maxCon="1000" minCon="10" balance="0"        writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">        <!-- 心跳包 -->        <heartbeat>select user()</heartbeat>        <!-- 后端mysql配置 -->        <writeHost host="hostM1" url="localhost:3306" user="socho" password="Looks137"></writeHost>    </dataHost>    <dataHost name="202.liberalman.cn" maxCon="1000" minCon="10" balance="0"        writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">        <!-- 心跳包 -->        <heartbeat>select user()</heartbeat>        <writeHost host="hostM1" url="localhost:3306" user="socho" password="Looks137"></writeHost>    </dataHost></mycat:schema>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

上面配置的是连接后端mysql的配置,然后我们还要配置访问MyCAT的用户账号和授权信息,打开conf/server.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mycat:server SYSTEM "server.dtd"><mycat:server xmlns:mycat="http://org.opencloudb/">    <system>    <property name="defaultSqlParser">druidparser</property>    </system>    <user name="test">            <property name="password">test</property>            <property name="schemas">TESTDB</property>    </user>    <user name="user">            <property name="password">user</property>            <property name="schemas">TESTDB</property>            <property name="readOnly">true</property>    </user></mycat:server>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

启动

[mycat@c1 ~]$ mycat startStarting Mycat-server...
  • 1
  • 2

如果启动遇到问题,请查阅日志logs目录下的文件。 
默认数据端口为8066,管理端口为9066。好,我们用一个客户端连接下 
mysql -h192.168.1.201 -P8066 -utest -DTESTDB -ptest

2.测试

连上后就可以当初一个mysql数据库来操作了,后端逻辑对用户透明,建表:

mysql> create table employee (id int not null primary key,name varchar(100),sharding_id int not null);Query OK, 0 rows affected (0.05 sec) 
  • 1
  • 2

成功后,插入数据:

mysql> insert into employee(id,name,sharding_id) values(1,'leader us',10000); insert into employee(id,name,sharding_id) values(2, 'me',10010); insert into employee(id,name,sharding_id) values(3, 'mycat',10000); insert into employee(id,name,sharding_id) values(4, 'mydog',10010);Query OK, 1 row affected (0.04 sec)Query OK, 1 row affected (0.01 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)
  • 1
  • 2
  • 3
  • 4
  • 5

查看下结果

mysql> select * from employee;+----+-----------+-------------+| id | name      | sharding_id |+----+-----------+-------------+|  1 | leader us |       10000 ||  3 | mycat     |       10000 ||  2 | me        |       10010 ||  4 | mydog     |       10010 |+----+-----------+-------------+4 rows in set (0.01 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

这时候分别到数据DB1,DB2查看,DB1数据

mysql> use db1;mysql> select * from employee;+----+-----------+-------------+| id | name      | sharding_id |+----+-----------+-------------+|  1 | leader us |       10000 ||  3 | mycat     |       10000 |+----+-----------+-------------+2 rows in set (0.00 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

DB2数据

mysql> use db2;mysql> select * from employee; +----+-------+-------------+| id | name  | sharding_id |+----+-------+-------------+|  2 | me    |       10010 ||  4 | mydog |       10010 |+----+-------+-------------+2 rows in set (0.00 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

我们终于看大,明分片成功了,哈哈,两个数据库各一半数据。 
从schema.xml配置中看到,employee表是根据规则sharding-by-intfile(分片字段为sharding_id)进行分片,所以A中都是sharding_id=10000的数据,B中是sharding_id=10010。 
运行explain指令,可以看到SQL在哪些分片节点执行的情况:

mysql> explain create table employee (id int not null primary key,name varchar(100),sharding_id int not null);+-----------+------------------------------------------------------------------------------------------------+| DATA_NODE | SQL                                                                                            |+-----------+------------------------------------------------------------------------------------------------+| dn1       | create table employee (id int not null primary key,name varchar(100),sharding_id int not null) || dn2       | create table employee (id int not null primary key,name varchar(100),sharding_id int not null) |+-----------+------------------------------------------------------------------------------------------------+2 rows in set (0.00 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

explain不会真的执行的,放心试试吧!

转自http://blog.csdn.net/socho/article/details/51869199

原创粉丝点击