0%

MyCat实践

接着上一篇MySql主从配置完成后,下面实战下Mycat做读写分离和分库分表

参考:

https://foofish.net/mysql-for-mycat.html

https://www.cnblogs.com/joylee/p/7513038.html

[TOC]

环境说明:

windows 下4台服务器,真实机IP:192.168.2.107,其中

服务器 IP 说明
master 172.17.0.2 物理数据库,主库
mycat 172.17.0.3 mycat服务器,连接数据库时,连接此服务器
slave1 172.17.0.4 物理数据库,从库
slave2 172.17.0.5 物理数据库,从库
1
2
3
4
5
6
C:\Users\mma>docker ps
CONTAINER ID PORTS NAMES
b04f945297ac 0.0.0.0:1024->1024/tcp, 0.0.0.0:6379->6379/tcp, 0.0.0.0:9001->9001/tcp, 0.0.0.0:220->22/tcp, 0.0.0.0:8080->80/tcp, 0.0.0.0:33060->3306/tcp master
dcccd23b6177 0.0.0.0:8066->8066/tcp, 0.0.0.0:223->22/tcp, 0.0.0.0:8083->80/tcp, 0.0.0.0:31024->1024/tcp, 0.0.0.0:33063->3306/tcp, 0.0.0.0:36379->6379/tcp, 0.0.0.0:9004->9001/tcp myCat
9ae039d46474 0.0.0.0:221->22/tcp, 0.0.0.0:8081->80/tcp, 0.0.0.0:10241->1024/tcp, 0.0.0.0:33061->3306/tcp, 0.0.0.0:16379->6379/tcp, 0.0.0.0:9002->9001/tcp slave1
06e5a050e74b 0.0.0.0:222->22/tcp, 0.0.0.0:8082->80/tcp, 0.0.0.0:10242->1024/tcp, 0.0.0.0:33062->3306/tcp, 0.0.0.0:26379->6379/tcp, 0.0.0.0:9003->9001/tcp slave2

master:

1
2
3
4
5
6
7
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | zhimma | | |
+------------------+----------+--------------+------------------+-------------------+

mycat:

1
2
[root@dcccd23b6177 mycat]# mycat status
Mycat-server is running (317).

slave1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: zhimma
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: 9ae039d46474-relay-bin.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 581
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: f781e2b4-28e1-11e8-a1c0-0242ac110002
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

salve2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.107
Master_User: zhimma
Master_Port: 33060
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: 06e5a050e74b-relay-bin.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 581
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: f781e2b4-28e1-11e8-a1c0-0242ac110002
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

安装

省略

目录结构:

1
2
3
4
5
[root@dcccd23b6177 mycat]# pwd
/etc/mycat
[root@dcccd23b6177 mycat]# ls
bin catlet conf lib logs tmlogs version.txt
[root@dcccd23b6177 mycat]#
目录 说明
bin mycat命令,启动、重启、停止等
catlet catlet为Mycat的一个扩展功能
conf Mycat 配置信息,重点关注
lib Mycat引用的jar包,Mycat是java开发的
logs 日志文件,包括Mycat启动的日志和运行的日志。

配置

Mycat的配置文件都在conf目录里面,这里介绍几个常用的文件:

文件 说明
server.xml Mycat的配置文件,设置账号、参数等
schema.xml Mycat对应的物理数据库和数据库表的配置
rule.xml Mycat分片(分库分表)规则

Mycat的架构

Mycat的架构其实很好理解,Mycat是代理,Mycat后面就是物理数据库。和Web服务器的Nginx类似。对于使用者来说,访问的都是Mycat,不会接触到后端的数据库。

mycat

配置

server.xml

1
2
3
4
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">zhimma_mycat</property>
</user>

重点关注下面这段,其他默认即可。

参数 说明
user 用户配置节点

name
登录的用户名,也就是连接Mycat的用户名

password
登录的密码,也就是连接Mycat的密码

schemas
数据库名,这里会和schema.xml中的配置关联,多个用逗号分开,例如需要这个用户需要管理两个数据库db1,db2,则配置db1,db2

privileges
配置用户针对表的增删改查的权限,具体见文档吧

我这里配置了一个账号root密码是123456,针对数据库zhimma_mycat,读写权限都有,没有针对表做任何特殊的权限。

schema.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="zhimma_mycat" checkSQLschema="false" sqlMaxLimit="100">
<table name="users" dataNode="dn1" />
<table name="articles" dataNode="dn1" />
<table name="categories" dataNode="dn1" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="zhimma" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select users()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.2.107:33060" user="root"
password="123456">
<!-- can have multi read hosts -->
<readHost host="hostS1" url="192.168.2.107:33061" user="root" password="123456" />
<readHost host="hostS2" url="192.168.2.107:33062" user="root" password="123456" />
</writeHost>
</dataHost>

</mycat:schema>
参数 说明
schema 数据库设置,此数据库为逻辑数据库,name与server.xml中schema对应,此处为zhimma_mycat
dataNode 分片信息,也就是分库相关配置
dataHost 物理数据库,真正存储数据的数据库

每个节点的属性逐一说明:

schema:

属性 说明
name 逻辑数据库名,与server.xml中的schema对应
checkSQLschema 布尔值,默认和推荐都是关闭。

如果开启则会拦截SQL语句,将其中的mycat相关字段删除
如:select * from zhimma_mycat.users;将会把SQL语句变为:
select * from users;
sqlMaxLimit SQL返回条数,最好加上限制,防止查询量过大导致卡死

table:

属性 说明
name 表名,物理数据库中表名
dataNode 表存储到哪些节点,多个节点用逗号分隔。节点为下文dataNode设置的name
primaryKey 主键字段名,自动生成主键时需要设置
autoIncrement 是否自增
rule 分片规则名,具体规则下文rule详细介绍

dataNode

属性 说明
name 节点名,与table中dataNode对应
datahost 物理数据库名,与datahost中name对应
database 物理数据库中数据库名

dataHost

属性 说明
name 物理数据库名,与dataNode中dataHost对应
balance 均衡负载的方式
writeType 写入方式
dbType 数据库类型
heartbeat 心跳检测语句,注意语句结尾的分号要加。