当前位置:Gxlcms >
数据库问题 >
MySQL 高可用:mysql+mycat实现数据库分片(分库分表)
MySQL 高可用:mysql+mycat实现数据库分片(分库分表)
时间:2021-07-01 10:21:17
帮助过:19人阅读
- 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
- [root@server1 ~]# 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 做简单的读写分离(一)
MySQL 高可用:mysql+mycat实现数据库分片(分库分表)
标签:2.0 bin ima imu class tle host roo tty