先规划下数据库的基础架构,先来个最简单基础的。
三台虚机,各安装了mysql5.7
用mycat建立逻辑数据库,建立5个表格,其中一个表格分库,一个表格做全局表,剩余三个表格每个虚机的数据库各放一个。
统计信息:
三个虚机的IP分别为:
192.168.211.138
192.168.211.139
192.168.211.142
真实的dataNode就是这三个虚机啦。
mysql的登录用户就用[email protected]%,密码都是:[email protected]
mycat逻辑库的名字命名为:hello
五张表格:
t1,t2,t3,t4,t5
t1做分库,t2做全局,t3放到dn1,t4放到dn2,t5放到dn3
预先需要考虑的问题:
分库的规则是什么?
先开始做吧,到哪一步再来思考。
首先配置schema.xml文件
先把配置文件备份一下
[[email protected] conf]# cp schema.xml schema.xml.bk2
清空配置文件
[[email protected] conf]# echo " " > schema.xml
开始配置:
[[email protected] 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="[email protected]">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.211.138:3306" user="root" password="[email protected]"/>
</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="[email protected]">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.211.139:3306" user="root" password="[email protected]"/>
</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="[email protected]">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.211.142:3306" user="root" password="[email protected]"/>
</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>
启动服务 [[email protected] conf]# ../bin/mycat start
Starting Mycat-server...
登录看看 [[email protected] conf]# mysql -uroot -p123456 -P8066 -h 127.0.0.1
稍微等一下登录,我发现马上登录会报错,说登录不上去。可以执行:
[[email protected] conf]# ps -ef |grep mycat
看看进程启动没有
操作看看
我这边登录成功了,参考信息如下
登录有两个端口,9066是管理端口,可以查看监控信息,8066是普通数据库登录端口,下面我是用9066端口登录,检查信息
[[email protected] 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,正常。
[[email protected] 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
达到了我们设计的要求。
原文地址:http://blog.51cto.com/goome/2058960