SuSE上运行MyCat(二):配置单点MyCat

 https://www.jianshu.com/p/8848f6a802b1

 

SuSE上运行MyCat(二):配置单点MyCat

目标

通过本文操作,搭建静态结构如下图的一个MyCat集群。为了节省资源,3个物理库都将存放在同一个MySQL实例当中。

 

部署MySQL节点

创建分片节点(库)


CREATE DATABASE mycat00;
CREATE DATABASE mycat01;
CREATE DATABASE mycat02;

CREATE TABLE mycat00.test_tbl(
id INT UNSIGNED NOT NULL PRIMARY KEY,
date DATETIME NOT NULL );
CREATE TABLE mycat01.test_tbl(
id INT UNSIGNED NOT NULL PRIMARY KEY,
date DATETIME NOT NULL );
CREATE TABLE mycat02.test_tbl(
id INT UNSIGNED NOT NULL PRIMARY KEY,
date DATETIME NOT NULL );

创建虚拟表用户


CREATE USER 'root'@'%' IDENTIFIED BY 'root';
GRANT ALL PRIVILIGES ON `mycat%`.* TO 'root'@'%';

插入原始数据


INSERT INTO mycat00.test_tbl(id,date)
VALUES (0,'2000-01-01');

INSERT INTO mycat01.test_tbl(id,date)
VALUES (1,'2000-02-01');

INSERT INTO mycat02.test_tbl(id,date)
VALUES (2,'2000-03-01');

配置MyCat

MyCat的配置文件集中在其配置文件夹/usr/local/mycat/conf内,以下工作将以该文件夹为基准。

路由规则(rule.xml)

备份原有路由规则文件rule.xml后,将整个rule.xml改写成如下内容:


<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
  - you may not use this file except in compliance with the License. - You
  may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
  - - Unless required by applicable law or agreed to in writing, software -
  distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
  License for the specific language governing permissions and - limitations
  under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
  <tableRule name="cicular_month_rule">
    <rule>
      <columns>date</columns>
      <algorithm>cicular_month</algorithm>
    </rule>
  </tableRule>

  <function name="cicular_month"
    class="io.mycat.route.function.PartitionByMonth">
    <property name="dateFormat">yyyy-MM-dd</property>
    <property name="sBeginDate">2000-01-01</property>
    <property name="sEndDate">2000-03-01</property>
    <property name="nPartition">3</property>
  </function>
</mycat:rule>

配置虚拟库及虚拟表(schema.xml)

备份原有虚拟库文件schema.xml后,将整个schema.xml改写成如下内容:


<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

  <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
    <table name="test_tbl" dataNode="dn00,dn01,dn02" rule="cicular_month_rule" />
  </schema>

  <dataNode name="dn00" dataHost="dh00" database="mycat00" />
  <dataNode name="dn01" dataHost="dh00" database="mycat01" />
  <dataNode name="dn02" dataHost="dh00" database="mycat02" />

  <dataHost name="dh00"
    dbType="mysql" dbDriver="native"
    maxCon="9" minCon="1" balance="0" switchType="0">
    <heartbeat>select 1</heartbeat>
    <writeHost host="host00"
      url="192.168.35.125:3306" user="root" password="root" />
  </dataHost>
</mycat:schema>

配置MyCat服务信息(server.xml及myid.properties)

备份原有服务器配置文件server.xml后,将整个server.xml改写成如下内容:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">

  <system>
    <property name="useSqlStat">0</property>
    <property name="useGlobleTableCheck">0</property>
    <property name="sequnceHandlerType">2</property>
    <property name="processorBufferPoolType">0</property>
    <property name="handleDistributedTransactions">0</property>
    <property name="useOffHeapForMerge">1</property>
    <property name="memoryPageSize">1m</property>
    <property name="spillsFileBufferSize">1k</property>
    <property name="useStreamOutput">0</property>
    <property name="systemReserveMemorySize">384m</property>
    <property name="useZKSwitch">false</property>
    <property name="serverPort">8066</property>
    <property name="managerPort">9066</property>
  </system>

  <user name="mycat_root">
    <property name="password">123456</property>
    <property name="schemas">TESTDB</property>
  </user>

  <user name="user">
    <property name="password">user</property>
    <property name="schemas">TESTDB</property>
    <property name="readOnly">true</property>
  </user>

</mycat:server>

备份原有ZooKeeper连接文件myid.properties后,将整个myid.properties改写成如下内容:


loadZk=false
zkURL=192.168.35.121:2181,192.168.35.123:2181
clusterId=mycat-cluster-1
myid=mycat_fz_01
clusterNodes=mycat_fz_01,mycat_fz_02
type=server
boosterDataHosts=dn00,dn01,dn02

部署结果

启动MyCat

确认MySQL数据节点(192.168.35.125)正常运作后,在MyCat节点(192.168.35.121)上,以操作系统用户root执行以下命令,启动MyCat。


sudo -u mysql /usr/local/mycat/bin/mycat start

MyCat虚拟库访问测试

在MySQL数据节点(192.168.35.125)上,使用如下语句打开到MyCat的连接


mysql -h192.168.35.121 -P8066 -umycat_root -p123456

  • 查询

    使用到MyCat的连接,执行以下SQL,查询虚拟表TESTDB.test_tbl的所有内容。


USE TESTDB
SELECT * FROM test_tbl;

屏幕显示如下,标志着我们已经可以通过访问虚拟表TESTDB.test_tbl,来获取分布在不同物理库mycat00、mycat01和mycat02里的同名物理表test_tbl的所有数据。


+----+---------------------+
| id | date                |
+----+---------------------+
|  0 | 2000-01-01 00:00:00 |
|  2 | 2000-03-01 00:00:00 |
|  1 | 2000-02-01 00:00:00 |
+----+---------------------+
3 rows in set (0.01 sec)

  • 增删改

    使用到MyCat的连接,执行以下SQL,进行以下操作:

  1. 插入1999年、2000年4月后的合计6条记录;

  2. 删除原始数据(2000年1月~3月的3条记录);

  3. 修改新插入的2000年4月后的三条记录,让它们的id列变成3位数;

  4. 查询虚拟表TESTDB.test_tbl这时候有的所有记录。


USE TESTDB

INSERT INTO test_tbl(id,date)
VALUES (3,'1999-10-01');
INSERT INTO test_tbl(id,date)
VALUES (4,'1999-11-01');
INSERT INTO test_tbl(id,date)
VALUES (5,'1999-12-01');

INSERT INTO test_tbl(id,date)
VALUES (6,'2000-04-01');
INSERT INTO test_tbl(id,date)
VALUES (7,'2000-05-01');
INSERT INTO test_tbl(id,date)
VALUES (8,'2000-06-01');

DELETE FROM test_tbl
WHERE date BETWEEN
'2000-01-01' AND '2000-03-01';

UPDATE test_tbl
SET id=id+100
WHERE date > '2000-01-01';

SELECT * FROM test_tbl;

屏幕最后会显示如下内容。我们已经可以通过虚拟表TESTDB.test_tbl进行增删改操作了。


+-----+---------------------+
| id  | date                |
+-----+---------------------+
|   3 | 1999-10-05 00:00:00 |
| 106 | 2000-04-01 00:00:00 |
|   4 | 1999-11-05 00:00:00 |
| 107 | 2000-05-01 00:00:00 |
|   5 | 1999-12-05 00:00:00 |
| 108 | 2000-06-01 00:00:00 |
+-----+---------------------+
6 rows in set (0.02 sec)

MySQL物理库检查

在MySQL数据节点(192.168.35.125)上,使用如下语句打开到MySQL物理库的连接,查看各个物理库的内容。


mysql -h192.168.35.125 -P3306 -uroot -proot -e"
SELECT 'mycat00',id,date 
FROM mycat00.test_tbl;

SELECT 'mycat01',id,date 
FROM mycat01.test_tbl;

SELECT 'mycat02',id,date 
FROM mycat02.test_tbl;
"

屏幕最后会显示如下内容。我们设置的分片路由规则起了效果,增删改的操作反映到了对应的物理库中。


+---------+-----+---------------------+
| mycat00 | id  | date                |
+---------+-----+---------------------+
| mycat00 |   3 | 1999-10-05 00:00:00 |
| mycat00 | 106 | 2000-04-01 00:00:00 |
+---------+-----+---------------------+
+---------+-----+---------------------+
| mycat01 | id  | date                |
+---------+-----+---------------------+
| mycat01 |   4 | 1999-11-05 00:00:00 |
| mycat01 | 107 | 2000-05-01 00:00:00 |
+---------+-----+---------------------+
+---------+-----+---------------------+
| mycat02 | id  | date                |
+---------+-----+---------------------+
| mycat02 |   5 | 1999-12-05 00:00:00 |
| mycat02 | 108 | 2000-06-01 00:00:00 |
+---------+-----+---------------------+

 

posted @ 2020-06-22 14:15  kelelipeng  阅读(302)  评论(0编辑  收藏  举报