Centos7 部署MyCAT与测试 | Eddie'Blog
Centos7 部署MyCAT与测试

Centos7 部署MyCAT与测试

eddie 773 2020-10-22

目录

一、准备工作

规划:

序号地址用途
1192.168.8.245:8066MyCAT
2192.168.8.246:3306MySQL
3192.168.8.247:3306MySQL

二、安装MySQL

1、官网下载 *.rpm
https://dev.mysql.com/doc/refman/8.0/en/linux-installation.html

2、安装步骤

cd /opt
yum install -y mysql80-community-release-el7-3.noarch.rpm 
yum install -y mysql-community-server
service mysqld start
service mysqld status

官方安装步骤
https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html

3、登录与修改密码

grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Abc@123456';
flush privileges;

4、 创建用户与赋予权限

create user 'eddie'@'%' identified with mysql_native_password by 'Abc@123456';
grant all on *.* to 'eddie'@'%'; 

三、安装MyCAT

1、官网地址
https://github.com/MyCATApache/Mycat-Server
http://mycat.org.cn/

2、解压

tar -zxvf Mycat-server-1.6.7.3-release-20190828135747-linux.tar.gz

3.1 配置文件

  • user标签部分需要修改对应信息
[root@localhost conf]# ls -la /opt/mycat/conf
total 108
drwxrwxrwx. 4 root root 4096 Oct 22 17:02 .
drwxr-xr-x. 8 root root   99 Oct 22 16:04 ..
-rwxrwxrwx. 1 root root   95 Oct 22 15:59 autopartition-long.txt    #  策略
-rwxrwxrwx. 1 root root 4808 Aug 21  2019 rule.xml   
-rwxrwxrwx. 1 root root 4083 Oct 22 16:54 schema.xml   # 分片信息
-rwxrwxrwx. 1 root root 5537 Oct 22 16:02 server.xml   # 服务器信息

3.2 server.xml 修改后

  • schema.xml 对应 <schema name="user" ... >
[root@localhost conf]# cat /opt/mycat/conf/server.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:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
        <system>
        <property name="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户-->
        <property name="useHandshakeV10">1</property>
        <property name="useSqlStat">0</property>  <!-- 1为开启实时统计、0为关闭 -->
        <property name="useGlobleTableCheck">0</property>  <!-- 1为开启全加班一致性检测、0为关闭 -->
                <property name="sqlExecuteTimeout">300</property>  <!-- SQL 执行超时 单位:秒-->
                <property name="sequnceHandlerType">2</property>
                <!--<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>-->
                <!--必须带有MYCATSEQ_或者 mycatseq_进入序列匹配流程 注意MYCATSEQ_有空格的情况-->
                <property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>
        <property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的情况下,检查关联字段中是否有分片字段 .默认 false -->
      <!--  <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
        <!--  <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->
        <!-- <property name="processorBufferChunk">40960</property> -->
        <!-- 
        <property name="processors">1</property> 
        <property name="processorExecutor">32</property> 
         -->
        <!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->
                <property name="processorBufferPoolType">0</property>
                <!--默认是65535 64K 用于sql解析时最大文本长度 -->
                <!--<property name="maxStringLiteralLength">65535</property>-->
                <!--<property name="sequnceHandlerType">0</property>-->
                <!--<property name="backSocketNoDelay">1</property>-->
                <!--<property name="frontSocketNoDelay">1</property>-->
                <!--<property name="processorExecutor">16</property>-->
                <!--
                        <property name="serverPort">8066</property> <property name="managerPort">9066</property> 
                        <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
                        <property name="dataNodeIdleCheckPeriod">300000</property> 5 * 60 * 1000L; //连接空闲检查
                        <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
                <!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
                <property name="handleDistributedTransactions">0</property>

                        <!--
                        off heap for merge/order/group/limit      1开启   0关闭
                -->
                <property name="useOffHeapForMerge">0</property>

                <!--
                        单位为m
                -->
        <property name="memoryPageSize">64k</property>

                <!--
                        单位为k
                -->
                <property name="spillsFileBufferSize">1k</property>

                <property name="useStreamOutput">0</property>

                <!--
                        单位为m
                -->
                <property name="systemReserveMemorySize">384m</property>

                <!--是否采用zookeeper协调切换  -->
                <property name="useZKSwitch">false</property>

                <!-- XA Recovery Log日志路径 -->
                <!--<property name="XARecoveryLogBaseDir">./</property>-->

                <!-- XA Recovery Log日志名称 -->
                <!--<property name="XARecoveryLogBaseName">tmlog</property>-->
                <!--如果为 true的话 严格遵守隔离级别,不会在仅仅只有select语句的时候在事务中切换连接-->
                <property name="strictTxIsolation">false</property>

                <property name="useZKSwitch">true</property>

        </system>

        <!-- 全局SQL防火墙设置 -->
        <!--白名单可以使用通配符%或着*-->
        <!--例如<host host="127.0.0.*" user="root"/>-->
        <!--例如<host host="127.0.*" user="root"/>-->
        <!--例如<host host="127.*" user="root"/>-->
        <!--例如<host host="1*7.*" user="root"/>-->
        <!--这些配置情况下对于127.0.0.1都能以root账户登录-->
        <!--
        <firewall>
           <whitehost>
              <host host="1*7.0.0.*" user="root"/>
           </whitehost>
       <blacklist check="false">
       </blacklist>
        </firewall>
        -->

        <user name="root" defaultAccount="true">
                <property name="password">123456</property>
                <!-- <property name="schemas">TESTDB</property>-->
                <property name="schemas">user</property> <!-- 可以使用逗号分隔,比如:user,order  schemas.xml有多少个 schemas标签 就多少个逗号追加  -->

                <!-- 表级 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">user</property>  # schema.xml 对应 <schema name="user" ... >
                <property name="readOnly">true</property>
        </user>

</mycat:server>

3.2 schema.xml 修改后

  • <dataHost ...> 标签, 节点主机, 包含host、写host
  • <dataNode ...> 标签, 数据节点, 指定到具体的数据库
  • <schema ...> 标签, 表名、数据节点、分片规则等
[root@localhost conf]# cat /opt/mycat/conf/schema.xml 
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

	<!-- 
		checkSQLschema 是否去掉SQL中的 schema 
		sqlMaxLimit: select 默认的limit值, 仅对分片表有效
		table标签,定义表:
			name属性,定义逻辑表的表明
			dataNode属性, 定义逻辑表的数据节点
			rule属性:定义分片表的分片规则,必需与rule.xml中的tableRule对应
			ruleRequired属性,是否绑定分片规则, 如果为true, 没有绑定分片规则, 程序报错
		
	 -->
        <schema name="user" checkSQLschema="true" sqlMaxLimit="100">
                <!-- auto sharding by id (long) -->
                <table name="user" dataNode="dn246,dn247" rule="auto-sharding-long" />

                <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
                        /> -->
        </schema>
        <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
                /> -->
        <dataNode name="dn246" dataHost="db246" database="user_246" />
        <dataNode name="dn247" dataHost="db247" database="user_247" />
        <!-- <dataNode name="dn3" dataHost="localhost1" database="db3" />  -->

        <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
         <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
        <dataNode       name="jdbc_dn2" dataHost="jdbchost" database="db2" />
        <dataNode name="jdbc_dn3"       dataHost="jdbchost" database="db3" /> -->

	<!-- 最大连接数:maxCon="1000" -->
	<!-- 最小连接数:minCon="1000" -->
	<!--
		balance 负载均衡类型: 
			0不开启读写分离; 1和2读写均衡分配; 3读落在readHost上;
	 -->
	<!--
		writeType=“0”, 所有写操作都发送到可用的writeHost上。 (官方推荐)
		writeType=“1”,所有写操作都随机的发送到readHost。
	 	writeType=“2”,所有写操作都随机的在writeHost、readhost分上发。
	 -->
        <dataHost name="db246" 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="M1" url="192.168.8.246:3306" user="eddie"
                                   password="Abc@123456">
                        <!-- can have multi read hosts -->
                        <!-- <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" /> -->
                </writeHost>
                <!-- <writeHost host="hostS1" url="localhost:3316" user="root"
                                   password="123456" /> -->

                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>

        <dataHost name="db247" 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="M1" url="192.168.8.247:3306" user="eddie"
                                   password="Abc@123456">
                        <!-- can have multi read hosts -->
                        <!-- <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" /> -->
                </writeHost>
                <!-- <writeHost host="hostS1" url="192.168.8.247:3316" user="root"
                                                        password="123456" /> -->

                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>


        <!--
                <dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc">
                <heartbeat>             </heartbeat>
                 <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng"  password="jifeng"></writeHost>
                 </dataHost>

          <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0"   dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat>
                <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql>
                <writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base"       password="123456" > </writeHost> </dataHost>

                <dataHost name="jdbchost" maxCon="1000"         minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc">
                <heartbeat>select       user()</heartbeat>
                <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost>

                <dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc">
                <heartbeat> </heartbeat>
                 <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng"         password="jifeng"></writeHost> </dataHost> -->

        <!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql"
                dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1"
                url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost>
                </dataHost> -->
</mycat:schema>

3.3 配置数据的分配规则

查看 schema.xml

图片.png

跟住上面截图,可以看出是rule="auto-sharding-long", 知道是 rule 那么进入 rule.xml 文件, 全局查找:

        <tableRule name="auto-sharding-long">
                <rule>
                        <columns>id</columns>
                        <algorithm>rang-long</algorithm>
                </rule>
        </tableRule>

在根据 rang-long 继续往下查找:

        <function name="rang-long"
                class="io.mycat.route.function.AutoPartitionByLong">
                <property name="mapFile">autopartition-long.txt</property>
        </function>

然后,我们知道 autopartition-long.txt 是修改数据的分配规则

  • 注释 1000M-1500M=2 是因为只有两个节点
  • 0-500M=0 意思是这个范围的id值,存入 user_246
  • 500M-1000M=1 意思是这个范围的id值,存入 user_247
[root@localhost conf]# cat /opt/mycat/conf/autopartition-long.txt
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
#1000M-1500M=2

3.4 MyCat命令

[root@localhost bin]# cd /opt/mycat/bin
./mycat console     # 前台运行
./mycat status      # 运行状态
./mycat start       # 后台运行
./mycat stop        # 停止服务

四、测试

  1. 使用 Navicat 或者其他工具 连接8.246和8.247的数据库,同时在两个MySQL创建库与表:

192.168.8.246:3306
eddie/Adc@123456

CREATE DATABASE user_246;

CREATE TABLE `user` (
  `id` int NOT NULL,
  `username` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

192.168.8.247:3306
eddie/Adc@123456

CREATE DATABASE user_247;

CREATE TABLE `user` (
  `id` int NOT NULL,
  `username` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

192.168.8.245:8066 / 管理端口:9066
root/123456

# 插入到 user_246
INSERT INTO `user`(id,username) VALUES(1,'eddie');

# 插入到 user_247
INSERT INTO `user`(id,username) VALUES(6000000,'eddie');

2、 如若直接连接MyCat,不使用 Navicat 等图形化工具,会出现新旧加密密码错误情况,解决如下:

  • 在安装MySQL的机器上运行
  • --default-auth=要使用的名称默认身份验证客户端插件.
[root@tracker ~]# mysql -uroot -p -P8066 -h192.168.8.245 --default-auth=mysql_native_password
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.29-mycat-1.6.7.3-release-20190828215749 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2020, 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>