Mycat基本搭建
1.Java环境检查与安装(略)
【检查】
[root@mysqldb tmp]# java -verson
-bash: java: command not found
【直接解压安装】
[root@mysqldb tmp]#tar xvf jdk-8u101-linux-x64.tar.gz -C /usr/local
[root@mysqldb tmp]# /usr/local/jdk1.8.0_101/bin/java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
【加入/etc/profile环境变量,也可以配置单独用户下】
[root@mysqldb tmp]#echo "export JAVA_HOME=/usr/local/jdk1.8.0_101">>/etc/profile
[root@mysqldb tmp]#echo "export PATH=\$JAVA_HOME/bin:$PATH">>/etc/profile
[root@mysqldb tmp]#source /etc/profile
[root@mysqldb tmp]#java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
[root@mysqldb tmp]#
2. 编译安装MySQL 5.6(略)
#安装的MySQL实例
192.168.2.130 3306
3. 安装MYCAT
【MYCAT下载】
Mycat 1.5地址: https://github.com/MyCATApache/Mycat-download/tree/master/1.5-RELEASE
Mycat-server-1.5.1-RELEASE-20161130213509-linux.tar.gz
【安装MYCAT】
# 解压
[root@localhost u01]# tar -zxvf Mycat-server-1.5.1-RELEASE-20161130213509-linux.tar.gz
# copy
[root@localhost u01]# mv mycat/ /usr/local/
# 加入系统路径 /etc/profile
export MYCAT_HOME=/usr/local/mycat
export PATH=$JAVA_HOME/bin:$MYCAT_HOME/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# 验证
[root@localhost u01]# mycat --version
Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
4.MYCAT配置分片
4.1 MySQL中新建三个数据库 db1 db2 db3
mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.01 sec)
mysql> CREATE DATABASE db2;
Query OK, 1 row affected (0.01 sec)
mysql> CREATE DATABASE db3;
Query OK, 1 row affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| db3 |
| jfedu |
| mysql |
| performance_schema |
| replTestDB |
| sakila |
| test |
| testdb |
| xtrabackup |
| zabbix |
+--------------------+
13 rows in set (0.00 sec)
4.2 MyCat配置文件介绍
conf/rule.xml 定义分片规则
conf/schema.xml 定义逻辑库、表以及分片节点等内容
conf/server.xml 定义用户授权及服务器参数相关配置
4.3配置schema
[root@localhost local]# cd /usr/local/mycat/conf
[root@localhost conf]# vim schema.xml
<?xml version="1.0"?> #第一行必须是XML文件的第一个元素且前面不能空格。 否则报错"[xX][mM][lL]" 的处理指令目标
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
#TESTDB 是MyCat默认的一个测试逻辑数据库,需要在此节点下定义逻辑表,但在这里只是指定表的名称,并不对表进行详细的定义。
#下面这条语句就是指逻辑表tb_user_info将在dn1,dn2,dn3上创建 使用的分片规则是 auto-sharding-long
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
</schema>
<dataNode name="dn1" dataHost="192.168.2.130" database="db1" />
<dataNode name="dn2" dataHost="192.168.2.130" database="db2" />
<dataNode name="dn3" dataHost="192.168.2.130" database="db3" />
<dataHost name="192.168.2.130" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.2.130:3306" user="root"
password="root123">
</writeHost>
</dataHost>
</mycat:schema>
此时在schema.xml中配置好的表名,实际上只是一个逻辑的表,这个表在物理数据库中并不存在,需要在MyCat通过Create Table 来创建这个表,执行Create语句以后,MyCat会在真实MySql配置的数据库中创建表。
4.4 auto-sharding-long分片规则的实现原理
[root@localhost local]# cd /usr/local/mycat/conf
[root@localhost local]# cat rule.xml
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<function name="rang-long" class="org.opencloudb.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
可见这个TableRule是通过id 来进行分片的,分片的算法是rang-long,算法中使用了autopartition-long.txt
[root@localhost local]# cat autopartition-long.txt
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2
K表示1000条记录,M表示10000条记录,上面的三个配置就是0500万的记录会存在数据库db1的表中,500万1000万会存在db2的表中,1000万~1500万会存在db3的表中。
4.5 启动mycat
#启动 Mycat
[root@localhost conf]# mycat start
Starting Mycat-server...
#查看mycat状态
[root@localhost conf]# mycat status
Mycat-server is running (60202).
#查看MyCat进程
[root@localhost conf]# ps -ef |grep mycat
root 60202 1 0 18:14 ? 00:00:00 /usr/local/mycat/bin/./wrapper-linux-x86-64 /usr/local/mycat/conf/wrapper.conf wrapper.syslog.ident=mycat wrapper.pidfile=/usr/local/mycat/logs/mycat.pid wrapper.daemonize=TRUE wrapper.lockfile=/var/lock/subsys/mycat
root 61948 91636 0 18:23 pts/4 00:00:00 grep --color=auto mycat
# 查看端口监听情况
ss -tanl
#通过mycat访问数据库
mysql@localhost ~]$ mysql -h 192.168.2.130 -P8066 -u test -ptest
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.8-mycat-1.5.1-RELEASE-20161130213509 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
5.mycat基本操作
#mycat查询逻辑数据库
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.00 sec)
#逻辑表 此时表物理上还未创建
mysql> use TESTDB;
Database changed
mysql> show tables;
Current database: TESTDB
+------------------+
| Tables in TESTDB |
+------------------+
| travelrecord |
+------------------+
1 row in set (0.03 sec)
#创建表
mysql> create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
Query OK, 0 rows affected (0.25 sec)
mysql> show create table travelrecord;
+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| travelrecord | CREATE TABLE `travelrecord` (
`id` bigint(20) NOT NULL,
`user_id` varchar(100) DEFAULT NULL,
`traveldate` date DEFAULT NULL,
`fee` decimal(10,0) DEFAULT NULL,
`days` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.03 sec)
#查看物理表
mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| travelrecord |
+---------------+
1 row in set (0.00 sec)
mysql> use db2;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| travelrecord |
+---------------+
1 row in set (0.00 sec)
mysql> use db3
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| travelrecord |
+---------------+
1 row in set (0.00 sec)
#可以在MySql中的三个数据库中看到,表确实已经创建了。
#验证路由
mysql> explain select * from travelrecord;
+-----------+--------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------+
| dn1 | SELECT * FROM travelrecord LIMIT 100 |
| dn2 | SELECT * FROM travelrecord LIMIT 100 |
| dn3 | SELECT * FROM travelrecord LIMIT 100 |
+-----------+--------------------------------------+
3 rows in set (0.13 sec)
#下面我们分别向表中插入三条数据分别ID是 1000,5100000,11000000,看是否正常分配到三个表中:
#能过在MyCat中执行explain SQL语句,可以查看插入的记录将会被分配到哪个表中:
insert into travelrecord(id,user_id,traveldate,fee,days) values(1000,'chinesern','2017-10-11',100,10);
insert into travelrecord(id,user_id,traveldate,fee,days) values(5100000,'chinesern','2017-10-11',100,10);
insert into travelrecord(id,user_id,traveldate,fee,days) values(11000000,'chinesern','2017-10-11',100,10);
#验证路由
mysql> explain insert into travelrecord(id,user_id,traveldate,fee,days) values(1000,'chinesern','2017-10-11',100,10);
+-----------+-------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+-------------------------------------------------------------------------------------------------------+
| dn1 | insert into travelrecord(id,user_id,traveldate,fee,days) values(1000,'chinesern','2017-10-11',100,10) |
+-----------+-------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> explain insert into travelrecord(id,user_id,traveldate,fee,days) values(5100000,'chinesern','2017-10-11',100,10);
+-----------+----------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+----------------------------------------------------------------------------------------------------------+
| dn2 | insert into travelrecord(id,user_id,traveldate,fee,days) values(5100000,'chinesern','2017-10-11',100,10) |
+-----------+----------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> explain insert into travelrecord(id,user_id,traveldate,fee,days) values(11000000,'chinesern','2017-10-11',100,10);
+-----------+-----------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+-----------------------------------------------------------------------------------------------------------+
| dn3 | insert into travelrecord(id,user_id,traveldate,fee,days) values(11000000,'chinesern','2017-10-11',100,10) |
+-----------+-----------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#物理表中验证
mysql> use db1;
Database changed
mysql> select * from travelrecord;
+------+-----------+------------+------+------+
| id | user_id | traveldate | fee | days |
+------+-----------+------------+------+------+
| 1000 | chinesern | 2017-10-11 | 100 | 10 |
+------+-----------+------------+------+------+
1 row in set (0.01 sec)
mysql> use db2;
Database changed
mysql> select * from travelrecord;
+---------+-----------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+-----------+------------+------+------+
| 5100000 | chinesern | 2017-10-11 | 100 | 10 |
+---------+-----------+------------+------+------+
1 row in set (0.00 sec)
mysql> use db3;
Database changed
mysql> select * from travelrecord;
+----------+-----------+------------+------+------+
| id | user_id | traveldate | fee | days |
+----------+-----------+------------+------+------+
| 11000000 | chinesern | 2017-10-11 | 100 | 10 |
+----------+-----------+------------+------+------+
1 row in set (0.00 sec)
#数据已经按照规则分布到不同的分片了!
Mycat基本搭建的更多相关文章
- 一、MyCat的搭建
一.什么是mycat 简单直接点就是,MyCat其实就是一个数据库的中间件!一般我们都是app直接到数据库!有了MyCat以后,就是app到MyCat然后再访问数据库. mycat是个中间件,它负责连 ...
- mycat快速搭建入门
mycat是一个数据库中间件,在此不做多介绍,今天我们直接上入门级的搭建demo. 本例子用的是两台mysql数据库 搭建mycat环境之前确保自己的机器上已经安装了djk1.7及以上虚拟机,因为my ...
- mycat环境搭建
最近工作中突然让搞mycat,特意私下在家先搞一套练个手: 1.先下载一个CentOS7 mini版本就可以(本人机器性能有限): 2.使用VMware创建虚拟机,过程百度下一大堆,这里不做详细介绍. ...
- MyCAT+MySQL 搭建高可用企业级数据库集群——第2章 MyCat入门
2-1 章节综述 2-2 什么是MyCat 2-3 什么是数据库中间层 2-4 MyCat的主要作用 2-5 MyCat基本元素 2-6 MyCat的安装 2-1 章节综述 1.掌握Mycat的基础概 ...
- MyCAT+MySQL 搭建高可用企业级数据库集群——第3章 MyCat核心配置讲解
3-1 章节综述 3-2 常用配置文件间的关系 3-3 server.xml配置详解 3-4 log4j2.xml配置文件 3-5 rule.xml 3-6 常用分片算法(上) 3-7 常用分片算法( ...
- mycat+mysql搭建高可用集群1--垂直分库
mycat垂直分库 本文主要介绍了如何使用mycat对mysql数据库进行垂直分库,包括: 垂直分库的步骤 垂直分库的环境准备 配置mycat垂直分库 1. 垂直分库的步骤 收集分析业务模块间的关系 ...
- MyCAT+MySQL搭建高可用企业级数据库集群视频课程
原文地址:https://www.guangboyuan.cn/mycatmysql%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8%E4%BC%81%E4% ...
- Mycat环境搭建教程收集(待实践)
先收集,后续再实践. http://blog.csdn.net/dreamcode/article/details/44307377 http://blog.csdn.net/lanonola/art ...
- Mycat - 实现数据库的读写分离与高可用
前言 开心一刻 上语文课,不小心睡着了,坐在边上的同桌突然叫醒了我,并小声说道:“读课文第三段”.我立马起身大声读了起来.正在黑板写字的老师吓了一跳,老师郁闷的看着我,问道:“同学有什么问题吗?”,我 ...
随机推荐
- 012——VUE中todos示例讲解class中应用表达式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- vim编辑16进制
你可以在vim中可以把文件转换为16进制来显示: :%!xxd 解释:把所有的行(%)用本地(!)的xxd程序打开. xxd本是linux下一个显示.编辑.转换二进制的命令. 返回正常显示: :%!x ...
- 通过IndexOf获得DataRow在DataTable中的行号
Row = dt.Rows.IndexOf(dr);
- Oracle中的填充函数lpad和rpad的用法(转)
原文链接:http://blog.csdn.net/myzhanglt/article/details/7392999 今日学习遇到一个不熟悉的函数LPAD,查了一下文档,将其用法总结如下: Lpad ...
- 【html】html笔记综合
基本标签与属性 <html>全部 <body>主体 <h1>标题 <p>段落 <br>空行,一般都会额外添加,并不总是需要自己添加,可以在& ...
- Java并发编程之CyclicBarrier
一.场景描述 有四个游戏玩家玩游戏,游戏有三个关卡,每个关卡必须要所有玩家都到达后才能允许通过.其实这个场景里的玩家中如果有玩家A先到了关卡1,他必须等到其他所有玩家都到达关卡1时才能通过,也就是说线 ...
- TCP服务器端口数,最大连接数以及MaxUserPort的关系辨真
原文连接:http://www.jianshu.com/p/4a58761d758f 关于TCP服务器最大并发连接数有一种误解就是"因为端口号上限为65535,所以TCP服务器理论上的可承载 ...
- Java进阶知识点6:并发容器背后的设计理念 - 锁分段、写时复制和弱一致性
一.背景 容器是Java编程中使用频率很高的组件,但Java默认提供的基本容器(ArrayList,HashMap等)均不是线程安全的.当容器和多线程并发编程相遇时,程序员又该何去何从呢? 通常有两种 ...
- Git 下载、安装与SSH配置
一.Git学习笔记(基于Github) Git简介 Git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都 ...
- self = [super init]
Objective-C的推荐init方法写法如下: - (id) init { if(self = [super init]) { //为子类增加属性进行初始化 } return self; } 返回 ...