三十八. 分库分表概述 配置mycat
1.搭建mycat 分片服务器
数据库主机 192.168.4.55 使用db1库存储数据
数据库主机 192.168.4.56 使用db2库存储数据
主机 192.168.4.54 运行mycat服务,逻辑库名称为test,连接用户名为admin,密码123456
在主机 192.168.4.254 访问测试配置
![](https://img2018.cnblogs.com/blog/1621514/201903/1621514-20190307205544603-1621002934.png)
![](https://img2018.cnblogs.com/blog/1621514/201903/1621514-20190307205554952-1924418443.png)
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.4-beta-20150604171601-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库在物理主机mysql55上
<dataNode name="dn2" dataHost="c2" database="db2" />
//定义分片使用的库,所在的物理主机,真正存储数据的db1库在物理主机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>
添加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]# netstat -antup | grep :8066
tcp6 0 0 :::8066 :::* LISTEN 6496/java
conf]# ps -C java
PID TTY TIME CMD
6496 ? 00:00:01 java
真机登陆:
]# mysql -h192.168.4.54 -uadmin -p123456
mysql> show processlist;
+----+-------+---------------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------+---------------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Sleep | 1386 | | NULL |
| 5 | admin | 192.168.4.56:38610 | db1 | Sleep | 98 | | NULL |
| 6 | admin | 192.168.4.56:38616 | db1 | Sleep | 78 | | NULL |
| 7 | admin | 192.168.4.56:38612 | db1 | Sleep | 38 | | NULL |
| 8 | admin | 192.168.4.56:38614 | db1 | Sleep | 88 | | NULL |
| 9 | admin | 192.168.4.56:38618 | db1 | Sleep | 8 | | NULL |
| 10 | admin | 192.168.4.56:38620 | db1 | Sleep | 48 | | NULL |
| 11 | admin | 192.168.4.56:38622 | db1 | Sleep | 68 | | NULL |
| 12 | admin | 192.168.4.56:38626 | db1 | Sleep | 28 | | NULL |
| 13 | admin | 192.168.4.56:38628 | db1 | Sleep | 58 | | NULL |
| 14 | admin | 192.168.4.56:38624 | db1 | Sleep | 18 | | NULL |
| 15 | admin | 192.168.4.254:40452 | NULL | Query | 0 | starting | show processlist |
+----+-------+---------------------+------+---------+------+----------+------------------+
]# mysql -h192.168.4.55 -uadmin -p123456
mysql> show processlist;
+----+-------+---------------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------+---------------------+------+---------+------+----------+------------------+
| 3 | root | localhost | NULL | Sleep | 1526 | | NULL |
| 5 | admin | 192.168.4.56:37200 | db2 | Sleep | 96 | | NULL |
| 6 | admin | 192.168.4.56:37202 | db2 | Sleep | 16 | | NULL |
| 7 | admin | 192.168.4.56:37194 | db2 | Sleep | 76 | | NULL |
| 8 | admin | 192.168.4.56:37196 | db2 | Sleep | 6 | | NULL |
| 9 | admin | 192.168.4.56:37198 | db2 | Sleep | 86 | | NULL |
| 10 | admin | 192.168.4.56:37204 | db2 | Sleep | 66 | | NULL |
| 11 | admin | 192.168.4.56:37190 | db2 | Sleep | 26 | | NULL |
| 12 | admin | 192.168.4.56:37192 | db2 | Sleep | 36 | | NULL |
| 13 | admin | 192.168.4.56:37206 | db2 | Sleep | 46 | | NULL |
| 14 | admin | 192.168.4.56:37208 | db2 | Sleep | 56 | | NULL |
| 15 | admin | 192.168.4.254:37330 | 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 客户端访问(真机)
命令: 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 |
| customer_addr |
| employee |
| goods |
| hotnews |
| orders |
| order_items |
| 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 |
+----+------+-------------+
MyCat支持的10中分片规则
MyCat支持的10中分片规则
![](https://img2018.cnblogs.com/blog/1621514/201903/1621514-20190307210703798-1625671798.png)