1、下载解压mycat
2、查看mycat目录结构
[root@aliyun-ecs mycat]# ll
total 24
drwxr-xr-x 2 root root 4096 Apr 18 10:28 bin
drwxrwxrwx 2 root root 4096 Oct 22 21:26 catlet
drwxrwxrwx 4 root root 4096 Feb 9 22:21 conf
drwxr-xr-x 2 root root 4096 Apr 18 10:28 lib
drwxrwxrwx 2 root root 4096 Jan 16 23:04 logs
-rwxrwxrwx 1 root root 227 Feb 9 22:22 version.txt
以上mycat就搭建完成了,注意mycat是用java语言开发的。
所以安装mycat需要有jdk环境
3、配置mycat
3.1 修改wrapper.conf
[root@aliyun-ecs mycat]# vim conf/wrapper.conf
非正式环境修改下面两个参数为256M,否则会出现内存不足
修改前:
wrapper.java.additional.10=-Xmx4G
wrapper.java.additional.11=-Xms1G
修改后:
wrapper.java.additional.10=-Xmx256M
wrapper.java.additional.11=-Xms256M
3.2 配置server.xml
[root@aliyun-ecs mycat]# vim conf/server.xml
添加一个mycat用户:scwyfy
并设置对应的逻辑库:admin_db,order_db,product_db
<user name="scwyfy">
<property name="password">123456</property>
<property name="schemas">admin_db,order_db,product_db</property>
<property name="readOnly">true</property>
</user>
3.3 配置schema.xml
配置schema->table->dataNode->dataHost->writeHost->readHost
[root@aliyun-ecs mycat]# vim conf/schema.xml
<!--name:给新增的mycat用户配置对应的逻辑库-->
<schema name="admin_db" checkSQLschema="false" sqlMaxLimit="100">
<!-- auto sharding by id (long):约定范围 0-1k = 0 1k-10k = 1 10k-100k=2 -->
<!-- name: 数据表名称-->
<!-- dataNode: 数据表存放的实际节点(真实服务器+数据库)-->
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<!-- global table is auto cloned to all defined data nodes ,so can join with any table whose sharding node is in the same data node -->
<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
<!-- random sharding using mod sharind rule -->
<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3" rule="mod-long" />
<!--配置有联表关系的表数据存放在同一个分片上-->
<table name="customer" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile">
<!--将orders表的customer_id与customer表的id关联起来,这样在orders表插入时,会首先判断customer_id在哪个分片上,然后将数据插入该分片
这样便实现了将关联数据存放在一个分片的目的-->
<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="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" database="db3" />
<!--配置数据库服务器信息-->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<!--用来检测心跳-->
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts :配置写库-->
<writeHost host="hostM1" url="localhost:3306" user="root" password="123456">
<!-- can have multi read hosts :配置从库-->
<readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" />
</writeHost>
</dataHost>