32_分库分表概述 配置mycat
Posted luwei0915
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了32_分库分表概述 配置mycat相关的知识,希望对你有一定的参考价值。
版本:5.7.28
1.搭建mycat 分片服务器
数据库主机 192.168.4.54 使用db1库存储数据
数据库主机 192.168.4.55 使用db2库存储数据
主机 192.168.4.56 运行mycat服务,逻辑库名称为test,连接用户名为admin,密码123456
主机 192.168.4.53 访问测试配置
1.1 在54(主机c1)和55(主机c2)上面分别创建db1和db2库
mysql> create database db1; //c1上面创建db1库
mysql> create database db2; //c2上面创建db2库
1.2 54 55 都授权
mysql> grant all on *.* to admin@"%" identified by "123456";
1.3 修改数据库的配置文件(54 55)
注意:1代表不区分表名的大小写,0为区分大小写
]# vim /etc/my.cnf
[mysqld]
lower_case_table_names=1 //表名忽略大小写
]# systemctl restart mysqld
1.4 在56主机上面安装JDK
]# rpm -qa | grep -i jdk //安装自带的即可
]# yum -y install java-1.8.0-openjdk
1.5 在56主机上面安装mycat
]# tar -xvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
]# mv mycat /usr/local/
]# ls /usr/local/mycat/
bin catlet conf lib logs version.txt
]# cd /usr/local/mycat/
]# ./bin/mycat --help
Usage: ./bin/mycat { console | start | stop | restart | status | dump }
1.6 修改配置文件
目录结构说明:
bin mycat命令,如 启动 停止 等
catlet 扩展功能
conf 配置文件
lib mycat使用的jar
log mycat启动日志和运行日志
wrapper.log mycat服务启动日志
mycat.log 记录SQL脚本执行后的报错内容
重要配置文件说明:
server.xml 设置连mycat的账号信息
schema.xml 配置mycat的真实库表
rule.xml 定义mycat分片规则
配置标签说明
<user>.. ..</user> 定义连mycat用户信息
<datanode>.. ..</datanode> 指定数据节点
<datahost>.. ..</datahost> 指定数据库地址及用户信息
查看server.xml配置文件
]# cd conf/
conf]# vim server.xml
</system>
<user name="test"> //连接mycat服务时使用的用户名 test
<property name="password">test</property>
//使用test用户连接mycat用户时使用的密码
<property name="schemas">TESTDB</property>
//连接上mycat服务后,可以看到的库名多个时,使用逗号分隔 (是逻辑上的库名,服务器上没有这个库名,随便取,但要记住)
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
//定义只读权限,使用定义的user用户连接mycat服务后只有读记录的权限,不写这一行则是可读可写
</user>
修改schema.xml配置文件
conf]# vim schema.xml
<table name="travelrecord" dataNode="dn1,dn2" rule="auto-sharding-long" />
//travelrecord(逻辑上的,名字不能随便写,一般不动)表分片到数据节点dn1和dn2,dn1和dn2随便取的名字
<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2" />
<table name="hotnews" primaryKey="ID" dataNode="dn1,dn2" rule="mod-long" />
...
<dataNode name="dn1" dataHost="c1" database="db1" />
//数据节点对应的服务器 name="dn1"名称要与上面的对应 dataHost="c1"写本机主机名,database="db1"存在的数据库名,定义分片使用的库,所在的物理主机,真正存储数据的db1库在物理主机mysql54上
<dataNode name="dn2" dataHost="c2" database="db2" />
//定义分片使用的库,所在的物理主机,真正存储数据的db2库在物理主机mysql55上
指定c1名称主机对应的ip地址
<dataHost name="c1" 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="c1" url="192.168.4.54:3306" user="admin"
password="123456">
//访问数据库时,mycat服务连接数据库服务器时使用的用户名和密码
<!-- can have multi read hosts -->
</writeHost>
</dataHost>
指定c2名称主机对应的ip地址(完整复制一对datahost)
<dataHost name="c2" 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="c2" url="192.168.4.55:3306" user="admin"
password="123456">
//访问数据库时,mycat服务连接数据库服务器时使用的用户名和密码
<!-- can have multi read hosts -->
</writeHost>
</dataHost>
注意:
rang-long 算法默认需要 3个 dotanode,测试拓扑只有两个dotanode,所以需要修改 autopartition-long.txt文件
#查看 rule.xml 中配置,找到 rang-long 算法的函数对应配置 autopartition-long.txt
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
编辑配置 注释掉最后一个 datanode
conf]# vim conf/autopartition-long.txt
#默认是三个,我们需要删除最后一个,不然就会报错,说节点少了
#K=1000条记录,M=10000条记录,那么下面三个配置就是0~500万的记录会存在数据库节点1的表中,500万~1000万会存在节点2的表中
0-500M=0
500M-1000M=1
#1000M-1500M=2
mod-long 算法默认需要 3个 dotanode,测试拓扑只有两个dotanode,需要修改 rule.xml 文件,修改count数为2即可
conf]# vim rule.xml
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
</function>
添加PATH路径
conf]# export PATH=/usr/local/mycat/bin:$PATH
conf]# echo "PATH=/usr/local/mycat/bin:$PATH" >> /etc/profile
conf]# source /etc/profile
conf]# echo $PATH
conf]# which mycat
/usr/local/mycat/bin/mycat
启动服务并查看端口
conf]# mycat start
conf]# netstat -antup | grep :8066
tcp6 0 0 :::8066 :::* LISTEN 29998/java
conf]# ps -C java
PID TTY TIME CMD
29998 ? 00:00:01 java
53登陆:
]# mysql -h192.168.4.54 -uadmin -p123456
mysql> show processlist;
+----+-------+---------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------+---------------+------+---------+------+----------+------------------+
| 4 | root | localhost | NULL | Sleep | 3935 | | NULL |
| 5 | admin | mysql56:59206 | db1 | Sleep | 61 | | NULL |
| 6 | admin | mysql56:59218 | db1 | Sleep | 1 | | NULL |
| 7 | admin | mysql56:59220 | db1 | Sleep | 71 | | NULL |
| 8 | admin | mysql56:59210 | db1 | Sleep | 11 | | NULL |
| 9 | admin | mysql56:59212 | db1 | Sleep | 71 | | NULL |
| 10 | admin | mysql56:59214 | db1 | Sleep | 31 | | NULL |
| 11 | admin | mysql56:59222 | db1 | Sleep | 71 | | NULL |
| 12 | admin | mysql56:59224 | db1 | Sleep | 41 | | NULL |
| 13 | admin | mysql56:59216 | db1 | Sleep | 51 | | NULL |
| 14 | admin | mysql56:59208 | db1 | Sleep | 21 | | NULL |
| 15 | admin | mysql53:50960 | NULL | Query | 0 | starting | show processlist |
+----+-------+---------------+------+---------+------+----------+------------------+
]# mysql -h192.168.4.55 -uadmin -p123456
mysql> show processlist;
+----+-------+---------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------+---------------+------+---------+------+----------+------------------+
| 4 | root | localhost | NULL | Sleep | 4016 | | NULL |
| 5 | admin | mysql56:41962 | db2 | Sleep | 67 | | NULL |
| 6 | admin | mysql56:41964 | db2 | Sleep | 17 | | NULL |
| 7 | admin | mysql56:41954 | db2 | Sleep | 37 | | NULL |
| 8 | admin | mysql56:41956 | db2 | Sleep | 27 | | NULL |
| 9 | admin | mysql56:41958 | db2 | Sleep | 87 | | NULL |
| 10 | admin | mysql56:41966 | db2 | Sleep | 47 | | NULL |
| 11 | admin | mysql56:41968 | db2 | Sleep | 7 | | NULL |
| 12 | admin | mysql56:41950 | db2 | Sleep | 57 | | NULL |
| 13 | admin | mysql56:41960 | db2 | Sleep | 97 | | NULL |
| 14 | admin | mysql56:41952 | db2 | Sleep | 77 | | NULL |
| 15 | admin | mysql53:32892 | NULL | Query | 0 | starting | show processlist |
+----+-------+---------------+------+---------+------+----------+------------------+
56上
conf]# ls /usr/local/mycat/logs/
mycat.log mycat.pid wrapper.log //wrapper.log为错误日志
conf]# ldconfig -v //更新加载的模块
1.7 客户端访(53)
命令: mysql -hmycat主机的IP -P端口号 -u用户 -p密码
]# mysql -h192.168.4.56 -P8066 -utest -ptest
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
mysql> USE TESTDB;
mysql> show tables;
+------------------+
| Tables in TESTDB |
+------------------+
| company |
| customer |
| employee |
| goods |
| hotnews |
| travelrecord |
+------------------+
1.8 客户端测试:
mysql> create table employee(id int not null primary key,name varchar(100),sharding_id int not null);
mysql> DESC employee;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(100) | YES | | NULL | |
| sharding_id | int(11) | NO | | NULL | |
+-------------+--------------+------+-----+---------+-------+
mysql> insert into employee(id,name,sharding_id) values(1,"bob",10000);
mysql> insert into employee(id,name,sharding_id) values(1,"lucy",10010);
mysql> select * from employee;
+----+------+-------------+
| id | name | sharding_id |
+----+------+-------------+
| 1 | bob | 10000 |
| 1 | lucy | 10010 |
+----+------+-------------+
在c1上面查看结果
mysql> use db1;
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| employee |
+---------------+
mysql> select * from employee; //查看结果
+----+------+-------------+
| id | name | sharding_id |
+----+------+-------------+
| 1 | bob | 10000 |
+----+------+-------------+
在c2上面查看结果
mysql> use db2;
mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| employee |
+---------------+
mysql> select * from employee; //查看结果
+----+------+-------------+
| id | name | sharding_id |
+----+------+-------------+
| 1 | lucy | 10010 |
+----+------+-------------+
即从53登陆56的数据库将数据写到了54和55.
MyCat支持的10种分片规则
以上是关于32_分库分表概述 配置mycat的主要内容,如果未能解决你的问题,请参考以下文章
Linux 部署 Mycat 实现 MariaDB 分库分表
Linux 部署 Mycat 实现 MariaDB 分库分表