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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
先规划下数据库的基础架构,先来个最简单基础的。
三台虚机,各安装了mysql5.7
用mycat建立逻辑数据库,建立5个表格,其中一个表格分库,一个表格做全局表,剩余三个表格每个虚机的数据库各放一个。
统计信息:
三个虚机的IP分别为:
192.168.211.138
192.168.211.139
192.168.211.142
真实的dataNode就是这三个虚机啦。
mysql的登录用户就用root@%,密码都是:Alex2010@
mycat逻辑库的名字命名为:hello
五张表格:
t1,t2,t3,t4,t5
t1做分库,t2做全局,t3放到dn1,t4放到dn2,t5放到dn3
预先需要考虑的问题:
分库的规则是什么?
先开始做吧,到哪一步再来思考。
首先配置schema.xml文件
先把配置文件备份一下
[root@mysql1 conf]
# cp schema.xml schema.xml.bk2
清空配置文件
[root@mysql1 conf]
# echo " " > schema.xml
开始配置:
[root@mysql1 conf]
# vi schema.xml
<?xml version=
"1.0"
?>
<!DOCTYPE mycat:schema SYSTEM
"schema.dtd"
>
<mycat:schema xmlns:mycat=
"http://io.mycat/"
> 这是定义文件头,xml格式默认必须有
<schema name=
"hello"
checkSQLschema=
"false"
sqlMaxLimit=
"100"
> 定义mycat逻辑库
<!-- auto sharding by
id
(long) --> 注释说明下面是分库表
<table name=
"t1"
dataNode=
"dn1,dn2,dn3"
rule=
"auto-sharding-long"
/> 定义t1分库
<!-- global table is auto cloned to all defined data nodes ,so can
join
with any table whose sharding node is
in
the same data node --> 注释说明是全局表
<table name=
"t2"
primaryKey=
"ID"
type
=
"global"
dataNode=
"dn1,dn2,dn3"
/> 定义t2是全局表
<table name=
"t3"
dataNode=
"dn1"
/>
<table name=
"t4"
dataNode=
"dn2"
/>
<table name=
"t5"
dataNode=
"dn3"
/> 这里还需要测试 定义各个分别存放在哪里
<
/schema
> 结束标签
<!-- <dataNode name=
"dn1$0-743"
dataHost=
"localhost1"
database=
"db$0-743"
/> -->
<dataNode name=
"dn1"
dataHost=
"mysql1"
database=
"db1"
/> 定义NODE
<dataNode name=
"dn2"
dataHost=
"mysql2"
database=
"db2"
/>
<dataNode name=
"dn3"
dataHost=
"mysql3"
database=
"db3"
/>
<dataHost name=
"mysql1"
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=
"hostM1"
url=
"192.168.211.138:3306"
user=
"root"
password=
"Alex2010@"
>
<!-- can have multi
read
hosts -->
<readHost host=
"hostS2"
url=
"192.168.211.138:3306"
user=
"root"
password=
"Alex2010@"
/>
<
/writeHost
>
<
/dataHost
>
<dataHost name=
"mysql2"
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=
"hostM1"
url=
"192.168.211.139:3306"
user=
"root"
password=
"Alex2010@"
>
<!-- can have multi
read
hosts -->
<readHost host=
"hostS2"
url=
"192.168.211.139:3306"
user=
"root"
password=
"Alex2010@"
/>
<
/writeHost
>
<
/dataHost
>
<dataHost name=
"mysql3"
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=
"hostM1"
url=
"192.168.211.142:3306"
user=
"root"
password=
"Alex2010@"
>
<!-- can have multi
read
hosts -->
<readHost host=
"hostS2"
url=
"192.168.211.142:3306"
user=
"root"
password=
"Alex2010@"
/>
<
/writeHost
>
<
/dataHost
>
<
/mycat
:schema> 结束标签
配置server.xml
<user name=
"root"
>
<property name=
"password"
>123456<
/property
>
<property name=
"schemas"
>TESTDB<
/property
> TESTDB改成自己的逻辑库,我的是hello
<!-- 表级 DML 权限设置 -->
<!--
<privileges check=
"false"
>
<schema name=
"TESTDB"
dml=
"0110"
>
<table name=
"tb01"
dml=
"0000"
><
/table
>
<table name=
"tb02"
dml=
"1111"
><
/table
>
<
/schema
>
<
/privileges
>
-->
<
/user
>
<user name=
"user"
>
<property name=
"password"
>user<
/property
>
<property name=
"schemas"
>TESTDB<
/property
>
<property name=
"readOnly"
>
true
<
/property
>
<
/user
>
启动服务 [root@mysql1 conf]
# ../bin/mycat start
Starting Mycat-server...
登录看看 [root@mysql1 conf]
# mysql -uroot -p123456 -P8066 -h 127.0.0.1
稍微等一下登录,我发现马上登录会报错,说登录不上去。可以执行:
[root@mysql1 conf]
# ps -ef |grep mycat
看看进程启动没有
操作看看
我这边登录成功了,参考信息如下
登录有两个端口,9066是管理端口,可以查看监控信息,8066是普通数据库登录端口,下面我是用9066端口登录,检查信息
[root@mysql1 logs]
# mysql -uroot -p123456 -P9066 -h 127.0.0.1
mysql: [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.6.29-mycat-1.6-RELEASE-20161028204710 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>
mysql> show @@help;
+------------------------------------------+--------------------------------------------+
| STATEMENT | DESCRIPTION |
+------------------------------------------+--------------------------------------------+
| show @@
time
.current | Report current timestamp |
| show @@
time
.startup | Report startup timestamp |
| show @@version | Report Mycat Server version |
| show @@server | Report server status |
| show @@threadpool | Report threadPool status |
| show @@database | Report databases |
| show @@datanode | Report dataNodes |
| show @@datanode where schema = ? | Report dataNodes |
| show @@datasource | Report dataSources |
| show @@datasource where dataNode = ? | Report dataSources |
| show @@datasource.synstatus | Report datasource data synchronous |
| show @@datasource.syndetail where name=? | Report datasource data synchronous detail |
| show @@datasource.cluster | Report datasource galary cluster variables |
| show @@processor | Report processor status |
| show @@
command
| Report commands status |
| show @@connection | Report connection status |
| show @@cache | Report system cache usage |
| show @@backend | Report backend connection status |
| show @@session | Report front session details |
| show @@connection.sql | Report connection sql |
| show @@sql.execute | Report execute status |
| show @@sql.detail where
id
= ? | Report execute detail status |
| show @@sql | Report SQL list |
| show @@sql.high | Report Hight Frequency SQL |
| show @@sql.slow | Report slow SQL |
| show @@sql.resultset | Report BIG RESULTSET SQL |
| show @@sql.
sum
| Report User RW Stat |
| show @@sql.
sum
.user | Report User RW Stat |
| show @@sql.
sum
.table | Report Table RW Stat |
| show @@parser | Report parser status |
| show @@router | Report router status |
| show @@heartbeat | Report heartbeat status |
| show @@heartbeat.detail where name=? | Report heartbeat current detail |
| show @@slow where schema = ? | Report schema slow sql |
| show @@slow where datanode = ? | Report datanode slow sql |
| show @@sysparam | Report system param |
| show @@syslog limit=? | Report system mycat.log |
| show @@white | show mycat white host |
| show @@white.
set
=?,? |
set
mycat white host,[ip,user] |
| show @@directmemory=1 or 2 | show mycat direct memory usage |
| switch @@datasource name:index | Switch dataSource |
|
kill
@@connection id1,id2,... | Kill the specified connections |
| stop @@heartbeat name:
time
| Pause dataNode heartbeat |
| reload @@config | Reload basic config from
file
|
| reload @@config_all | Reload all config from
file
|
| reload @@route | Reload route config from
file
|
| reload @@user | Reload user config from
file
|
| reload @@sqlslow= | Set Slow SQL Time(ms) |
| reload @@user_stat | Reset show @@sql @@sql.
sum
@@sql.slow |
| rollback @@config | Rollback all config from memory |
| rollback @@route | Rollback route config from memory |
| rollback @@user | Rollback user config from memory |
| reload @@sqlstat=
open
| Open real-
time
sql stat analyzer |
| reload @@sqlstat=close | Close real-
time
sql stat analyzer |
| offline | Change MyCat status to OFF |
| online | Change MyCat status to ON |
|
clear
@@slow where schema = ? | Clear slow sql by schema |
|
clear
@@slow where datanode = ? | Clear slow sql by datanode |
+------------------------------------------+--------------------------------------------+
58 rows
in
set
(0.01 sec)
以上是可以使用的监控命令,下面操作几个看看
mysql> show @@datanode;
+------+------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME | DATHOST | INDEX | TYPE | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+------+------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| dn1 | mysql1
/db1
| 0 | mysql | 0 | 10 | 1000 | 94 | 0 | 0 | 0 | -1 |
| dn2 | mysql2
/db2
| 0 | mysql | 0 | 10 | 1000 | 92 | 0 | 0 | 0 | -1 |
| dn3 | mysql3
/db3
| 0 | mysql | 0 | 10 | 1000 | 94 | 0 | 0 | 0 | -1 |
+------+------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
3 rows
in
set
(0.01 sec)
三个datanode,正常。
[root@mysql1 logs]
# mysql -uroot -p123456 -P8066 -h 127.0.0.1
普通口登录操作看看
mysql> show databases;
+----------+
| DATABASE |
+----------+
| hello |
+----------+
1 row
in
set
(0.00 sec)
mysql>
mysql> show tables;
+-----------------+
| Tables
in
hello |
+-----------------+
| t1 |
| t2 |
| t3 |
| t4 |
| t5 |
+-----------------+
5 rows
in
set
(0.00 sec)
mysql> desc t1;
ERROR 1146 (42S02): Table
'db1.t1'
doesn't exist
mysql>
报错,因为实际我们还没有创建表格
mysql> create table t1(
id
int not null,name varchar(20));
Query OK, 0 rows affected (0.66 sec)
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
|
id
| int(11) | NO | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows
in
set
(0.08 sec)
mysql>
mysql> explain create table t1(
id
int not null,name varchar(20));
+-----------+---------------------------------------------------+
| DATA_NODE | SQL |
+-----------+---------------------------------------------------+
| dn1 | create table t1(
id
int not null,name varchar(20)) |
| dn2 | create table t1(
id
int not null,name varchar(20)) |
| dn3 | create table t1(
id
int not null,name varchar(20)) |
+-----------+---------------------------------------------------+
3 rows
in
set
(0.04 sec)
注意t1分库,三个node都会生成
mysql> create table t2(
id
int not null,money varchar(100));
Query OK, 0 rows affected (0.12 sec)
mysql> desc t2;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
|
id
| int(11) | NO | | NULL | |
| money | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows
in
set
(0.08 sec)
mysql>
mysql> explain create table t2(
id
int not null,money varchar(100));
+-----------+-----------------------------------------------------+
| DATA_NODE | SQL |
+-----------+-----------------------------------------------------+
| dn1 | create table t2(
id
int not null,money varchar(100)) |
| dn2 | create table t2(
id
int not null,money varchar(100)) |
| dn3 | create table t2(
id
int not null,money varchar(100)) |
+-----------+-----------------------------------------------------+
3 rows
in
set
(0.00 sec)
mysql>
注意t2全局表,三个node都会生成
mysql> create table t3(
id
int not null,bu varchar(20));
Query OK, 0 rows affected (0.04 sec)
mysql> desc t3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
|
id
| int(11) | NO | | NULL | |
| bu | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows
in
set
(0.13 sec)
mysql>
mysql> explain create table t3(
id
int not null,bu varchar(20));
+-----------+-------------------------------------------------+
| DATA_NODE | SQL |
+-----------+-------------------------------------------------+
| dn1 | create table t3(
id
int not null,bu varchar(20)) |
+-----------+-------------------------------------------------+
1 row
in
set
(0.00 sec)
mysql>
t3,我们默认是设置放到node dn1上的,实现要求。
mysql> explain create table t4(
id
int not null,city varchar(10);
+-----------+--------------------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------------------+
| dn2 | create table t4(
id
int not null,city varchar(10) |
+-----------+--------------------------------------------------+
1 row
in
set
(0.00 sec)
mysql> explain create table t5(
id
int not null,company varchar(50));
+-----------+------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+------------------------------------------------------+
| dn3 | create table t5(
id
int not null,company varchar(50)) |
+-----------+------------------------------------------------------+
1 row
in
set
(0.00 sec)
mysql>
t4放到node dn2
t5放到node dn3
达到了我们设计的要求。
|
本文转自 am2012 51CTO博客,原文链接:http://blog.51cto.com/goome/2058960