本文引用于http://blog.csdn.net/kk185800961/article/details/51147029
MySQL 高可用:mysql+mycat实现数据库分片(分库分表)
什么是MYCAT:
一个彻底开源的,面向企业应用开发的大数据库集群
支持事务、ACID、可以替代MySQL的加强版数据库
一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群
一个融合内存缓存技术、NoSQL技术、HDFS大数据的新型SQL Server
结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品
一个新颖的数据库中间件产品
Mycat关键特性 :
支持SQL92标准
遵守Mysql原生协议,跨语言,跨平台,跨数据库的通用中间件代理。
基于心跳的自动故障切换,支持读写分离,支持MySQL主从,以及galera cluster集群。
支持Galera for MySQL集群,Percona Cluster或者MariaDB cluster
基于Nio实现,有效管理线程,高并发问题。
支持数据的多片自动路由与聚合,支持sum,count,max等常用的聚合函数,支持跨库分页。
支持单库内部任意join,支持跨库2表join,甚至基于caltlet的多表join。
支持通过全局表,ER关系的分片策略,实现了高效的多表join查询。
支持多租户方案。
支持分布式事务(弱xa)。
支持全局序列号,解决分布式下的主键生成问题。
分片规则丰富,插件化开发,易于扩展。
强大的web,命令行监控。
支持前端作为mysq通用代理,后端JDBC方式支持Oracle、DB2、SQL Server 、 mongodb 、巨杉。
支持密码加密
支持服务降级
支持IP白名单
支持SQL黑名单、sql注入攻击拦截
支持分表(1.6)
集群基于ZooKeeper管理,在线升级,扩容,智能优化,大数据处理(2.0开发版)。
JDK 下载(建议要求jdk7及以上):
http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html
Linux x86 147.68 MB jdk-7u80-linux-i586.tar.gz
MyCAT 相关:
Mycat 捐赠地址:http://www.mycat.io/donate.html
Mycat 官斱网站:http://www.mycat.io/
Mycat 源码:https://github.com/MyCATApache/Mycat-Server
Mycat 下载地址:https://github.com/MyCATApache/Mycat-download
既然 mycat 那么强大,那就测试看看:
#测试环境:
# 有3张表 users,item,item_detail (item 的子表) 和 3 个数据库 db01,db02,db03
# 表 users 存储在数据库 db01, 表 item 和 item_detail 分布存储在数据 db01 和 db02
# 现在在3个数据库都创建相同的表
[sql] view plain copy
- create database db01;
- create database db02;
- create database db03;
- CREATE TABLE users (
- id INT NOT NULL AUTO_INCREMENT,
- name varchar(50) NOT NULL default ‘‘,
- indate DATETIME NOT NULL default ‘0000-00-00 00:00:00‘,
- PRIMARY KEY (id)
- )AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
- CREATE TABLE item (
- id INT NOT NULL AUTO_INCREMENT,
- value INT NOT NULL default 0,
- indate DATETIME NOT NULL default ‘0000-00-00 00:00:00‘,
- PRIMARY KEY (id)
- )AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
- CREATE TABLE item_detail (
- id INT NOT NULL AUTO_INCREMENT,
- value INT NOT NULL default 0,
- name varchar(50) NOT NULL default ‘‘,
- item_id INT NOT NULL,
- PRIMARY KEY (id),
- key (item_id)
- )AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
# JDK 安装配置
[plain] view plain copy
- cd /usr/local/src
- tar xvf jdk-7u80-linux-i586.tar.gz
- mkdir -p /usr/local/java
- mv /usr/local/src/jdk1.7.0_80 /usr/local/java/
- chown -R root:root /usr/local/java
- cd /usr/local/java/
#配置环境变量
[plain] view plain copy
- vi /etc/profile
- vi ~/.bash_profile
- export JAVA_HOME=/usr/local/java/jdk1.7.0_80
- export PATH=$PATH:$JAVA_HOME/bin
- export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
#设置生效(最好重启系统)
[plain] view plain copy
- source /etc/profile
- source ~/.bash_profile
#测试是否安装成功
[plain] view plain copy
- [[email protected] ~]# java -version
- java version "1.7.0_80"
- Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
- Java HotSpot(TM) Client VM (build 24.80-b11, mixed mode)
# Mycat 安装配置
[plain] view plain copy
- cd /usr/local/src
- tar xvf Mycat-server-1.5.1-RELEASE-20160405120037-linux.tar.gz
- mv /usr/local/src/mycat /usr/local/
- groupadd mycat
- useradd -g mycat mycat
- passwd mycat
- chown -R mycat.mycat /usr/local/mycat
#添加环境变量
[plain] view plain copy
- vi /etc/profile
- vi ~/.bash_profile
- export MYCAT_HOME=/usr/local/mycat
#设置生效(最好重启系统)
[plain] view plain copy
- source /etc/profile
- source ~/.bash_profile
#服务器名和IP绑定
[plain] view plain copy
- vi /etc/hosts
- 192.168.100.50 server1
- 127.0.0.1 server1
# 设置 wrapper.java.command 的java 路径
( 同时可设置 -Xmx 和 -Xms ,参考: mycat 启动失败 The specified size exceeds the maximum representable size)
[plain] view plain copy
- vi /usr/local/mycat/conf/wrapper.conf
- wrapper.java.command=%JAVA_HOME%/bin/java
#配置 server.xml 和 schema.xml (本案例配置信息在文章底部)
[plain] view plain copy
- vi /usr/local/mycat/conf/server.xml
- vi /usr/local/mycat/conf/schema.xml
#启动mycat服务 ./mycat { console | start | stop | restart | status | dump }
[plain] view plain copy
- /usr/local/mycat/bin/mycat console
[plain] view plain copy
- #mycat 进程
- ps -ef | grep mycat
- #日子信息
- tail -20 /usr/local/mycat/logs/wrapper.log
测试:
#访问 mycat (凭据参考 /usr/local/mycat/conf/server.xml)
[plain] view plain copy
- mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB
[sql] view plain copy
- mysql> show databases;
- +----------+
- | DATABASE |
- +----------+
- | TESTDB |
- +----------+
- mysql> show tables;
- +------------------+
- | Tables in TESTDB |
- +------------------+
- | item |
- | item_detail |
- | users |
- +------------------+
#在 mycat 中插入数据并查询:
[sql] view plain copy
- insert into users(name,indate) values(‘kk‘,now());
- insert into item(id,value,indate) values(1,100,now());
- insert into item_detail(value,name,item_id) values(‘pad‘,40,1);
- insert into item_detail(value,name,item_id) values(‘phone‘,50,1);
- insert into item(id,value,indate) values(999,100,now());
- insert into item_detail(value,name,item_id) values(‘pad‘,40,999);
- insert into item_detail(value,name,item_id) values(‘phone‘,50,999);
[sql] view plain copy
- mysql> select * from users;
- +----+------+---------------------+
- | id | name | indate |
- +----+------+---------------------+
- | 1 | kk | 2016-04-13 06:28:00 |
- +----+------+---------------------+
- mysql> select * from item;
- +-----+-------+---------------------+
- | id | value | indate |
- +-----+-------+---------------------+
- | 999 | 100 | 2016-04-13 06:34:12 |
- | 1 | 100 | 2016-04-13 06:32:58 |
- +-----+-------+---------------------+
- mysql> select * from item_detail;
- +----+-------+------+---------+
- | id | value | name | item_id |
- +----+-------+------+---------+
- | 1 | 0 | 40 | 999 |
- | 2 | 0 | 50 | 999 |
- | 1 | 0 | 40 | 1 |
- | 2 | 0 | 50 | 1 |
- +----+-------+------+---------+
#打开另一个终端,使用mysql账号登陆访问查看表情况
[plain] view plain copy
- mysql -uroot -ptest -p
#具体的表数据存储情况:
[sql] view plain copy
- -----------------------------------------
- mysql> select * from db01.users;
- +----+------+---------------------+
- | id | name | indate |
- +----+------+---------------------+
- | 1 | kk | 2016-04-13 06:28:00 |
- +----+------+---------------------+
- mysql> select * from db02.users;
- mysql> select * from db03.users;
- -----------------------------------------
- mysql> select * from db01.item;
- mysql> select * from db02.item;
- +-----+-------+---------------------+
- | id | value | indate |
- +-----+-------+---------------------+
- | 999 | 100 | 2016-04-13 06:34:12 |
- +-----+-------+---------------------+
- mysql> select * from db03.item;
- +----+-------+---------------------+
- | id | value | indate |
- +----+-------+---------------------+
- | 1 | 100 | 2016-04-13 06:32:58 |
- +----+-------+---------------------+
- -----------------------------------------
- mysql> select * from db01.item_detail;
- mysql> select * from db02.item_detail;
- +----+-------+------+---------+
- | id | value | name | item_id |
- +----+-------+------+---------+
- | 1 | 0 | 40 | 999 |
- | 2 | 0 | 50 | 999 |
- +----+-------+------+---------+
- mysql> select * from db03.item_detail;
- +----+-------+------+---------+
- | id | value | name | item_id |
- +----+-------+------+---------+
- | 1 | 0 | 40 | 1 |
- | 2 | 0 | 50 | 1 |
- +----+-------+------+---------+
测试完成!!~数据进行了分库分表!
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
# server.xml 和 schema.xml 配置情况
[plain] view plain copy
- # vi /usr/local/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="processors">32</property>
- <property name="processorExecutor">32</property>
- <property name="bindIp">0.0.0.0</property>
- <property name="frontWriteQueueSize">4096</property>
- <property name="idleTimeout">300000</property>
- <property name="mutiNodePatchSize">100</property>
- -->
- <property name="defaultSqlParser">druidparser</property>
- <property name="mutiNodeLimitType">1</property>
- <property name="serverPort">8066</property>
- <property name="managerPort">9066</property>
- </system>
- <!-- 任意设置登陆 mycat 的用户名,密码,数据库 -->
- <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>
- <!--
- <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>
[plain] view plain copy
- # vi /usr/local/mycat/conf/schema.xml
- <?xml version="1.0"?>
- <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
- <mycat:schema xmlns:mycat="http://org.opencloudb/">
- <!-- 设置表的存储方式.schema name="TESTDB" 与 server.xml中的 TESTDB 设置一致 -->
- <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
- <table name="users" primaryKey="id" type="global" dataNode="node_db01" />
- <table name="item" primaryKey="id" dataNode="node_db02,node_db03" rule="mod-long">
- <childTable name="item_detail" primaryKey="id" joinKey="item_id" parentKey="id" />
- </table>
- </schema>
- <!-- 设置dataNode 对应的数据库,及 mycat 连接的地址dataHost -->
- <dataNode name="node_db01" dataHost="dataHost01" database="db01" />
- <dataNode name="node_db02" dataHost="dataHost01" database="db02" />
- <dataNode name="node_db03" dataHost="dataHost01" database="db03" />
- <!-- mycat 逻辑主机dataHost对应的物理主机.其中也设置对应的mysql登陆信息 -->
- <dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
- <heartbeat>select user()</heartbeat>
- <writeHost host="server1" url="127.0.0.1:3306" user="root" password="mysql"/>
- </dataHost>
- </mycat:schema>
其他参考:
http://www.csdn.net/article/2015-07-16/2825228 (原理及应用)
http://www.songwie.com/articlelist/50(理论和实践 写的好)
http://ly.blog.itpub.net/29733787/viewspace-2049139/(示例) MyCAT安装使用及简单测试
http://www.cnblogs.com/ivictor/p/5111495.html(示例) MyCAT简易入门
http://www.2cto.com/database/201503/385641.html(示例) Mycat水平分表,垂直分表实践(2)
http://www.51testing.com/html/34/369434-3686088.html(示例)使用Mycat 做简单的读写分离(一)