mycat 单库多表实现水平分片

环境

mycat : 192.168.126.128         root    root
mysql1: 192.168.126.129:3306    root    lizhenghua
mysql2: 192.168.126.131:3306    root    lizhenghua

database:   zwrdb
table1  :   role
table2  :   subject_t
table3  :   suggest
table4  :   cas_user

版本说明

mycat-server-1.6.6.1
mysql-5.7.22

mycat下载链接

mycat部署

1.包的部署

tar -zxvf Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz -C /usr/local

vim /etc/profile
#mycat
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin

source /etc/profile

2.JDK的部署

#*************************************************************************
#         > File Name: jdk.sh
#         > Author: chenglee
#         > Main : chengkenlee@sina.com
#         > Blog : http://www.cnblogs.com/chenglee/
#         > Created Time : 2019年03月07日 星期四 18时08分59秒
#*************************************************************************
#!/bin/bash
jdk_path="/usr/local"
jdk_targ=`basename *jdk-*`

function Cheng_flash(){
    i=0;
    str=""
    arr=("|" "/" "-" "\\")
    while [ $i -le 100 ]
    do
    let index=i%4
    let indexcolor=i%8
    let color=30+indexcolor
    printf "\e[0;$color;1m[%-100s][%d%%]%c\r" "$str" "$i" "${arr[$index]}"
    sleep ${sleeptime2}
    let i++
    str+='='
    done
    printf "\n"
}
function Time_test(){
    echo "please wait a moment ... "
    starttime=`date +'%Y-%m-%d %H:%M:%S'`
    tar tvvf ${jdk_targ} > /dev/null
    endtime=`date +'%Y-%m-%d %H:%M:%S'`
    start_seconds=$(date --date="$starttime" +%s);
    end_seconds=$(date --date="$endtime" +%s);
    sleeptime=$((end_seconds-start_seconds))
    sleeptime2=`awk 'BEGIN{printf "%0.2f",'${sleeptime}'/100}'`
}
function tar_gz(){
    Time_test
    tar xvvf ${jdk_targ} -C ${jdk_path} > /dev/null | Cheng_flash
}
function profile(){
    jdk_Path=`find ${jdk_path} -maxdepth 1 | grep jdk`
(
cat <<EOF
#java jdk
export JAVA_HOME=${jdk_Path}
export JRE_HOME=\${JAVA_HOME}/jre
export CLASSPATH=.:\${JAVA_HOME}/lib:\${JRE_HOME}/lib
export PATH=\${JAVA_HOME}/bin:\$PATH
EOF
) >> /etc/profile
}
function Source(){
    source /etc/profile
}

function main(){
    tar_gz
    profile
    Source
}
main

注: 把此脚本扔在与JDK二进制包同一目录, 运行此脚本即可,这是我常用的shell

mysql方面

注:忽略mysql的部署步骤, 直接跳到启动mysql.

[root@localhost mysql]# ifconfig | grep inet | grep -v inet6 | head -n 1 | awk -F ' ' '{print$2}' ; ps aux | grep mysql
192.168.126.129
root       7139  0.0  0.1 113520  1752 pts/0    S    09:50   0:00 /bin/sh ./bin/mysqld_safe --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
mysql      7307  0.1 14.6 1141352 194172 pts/0  Sl   09:50   0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/logs/mysql.logs --pid-file=/usr/local/mysql/mysql.pid --socket=/data/mysql/mysql.sock


[root@localhostmysql]# ifconfig | grep inet | grep -v inet6 | head -n 1 | awk -F ' ' '{print$2}' ; ps aux | grep mysql
192.168.126.131
root       7367  0.0  0.1 113312  1628 pts/0    S    09:50   0:00 /bin/sh ./bin/mysqld_safe --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
mysql      7531  0.0 13.5 1141332 192852 pts/0  Sl   09:50   0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/logs/mysql.logs --pid-file=/usr/local/mysql/mysql.pid --socket=/data/mysql/mysql.sock

mycat配置

schema.xml

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

    <!-- 本配置文件应对双机单库多表**水平分片**-->
    <!-- 数据库配置  -->
    <schema name="zwrdb" checkSQLschema="false" sqlMaxLimit="100">
        <table name="role"  dataNode="dn1" />
        <table name="subject_t"  dataNode="dn1" />
        <table name="suggest"  dataNode="dn1" />
        <table name="cas_user" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2"
               rule="mod-long" />
    </schema>
    <!-- 分片配置  -->
    <dataNode name="dn1" dataHost="test1" database="zwrdb" />
    <dataNode name="dn2" dataHost="test2" database="zwrdb" />

    <!-- 分表分库 start-->
    <!-- 物理数据库配置  test1模块-->
    <dataHost name="test1" maxCon="1000" minCon="10" balance="0"
              writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user();</heartbeat>
        <writeHost host="hostM1" url="192.168.126.129:3306" user="root"
                   password="lizhenghua">
        </writeHost>
    </dataHost>
    <!-- 物理数据库配置  test2模块-->
    <dataHost name="test2" maxCon="1000" minCon="10" balance="0"
              writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user();</heartbeat>
        <writeHost host="hostS1" url="192.168.126.131:3306" user="root"
                   password="lizhenghua">
        </writeHost>
    </dataHost>
    <!-- 分表分库 end-->
</mycat:schema>

注:

数据库模块配置说明: 库设置为zwrdb, 在这个库里面的表就是前面三个表都写入了dn1这块片节点, 而cas_user分到了dn1和dn2两个片节点, 分片规则是mod-long

分片配置说明:就是dn1和dn2, 它们分别指定的各自的dataHost与共同的database

物理数据库配置说明:根据两个不通的dataHost指定两台mysql的登录方式

rule.xml

注:修改有关mod-long分片规则属性, 它默认是3块分片, 而我这里只设置了2个dataNode

<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
    <!-- how many data nodes -->
    <property name="count">2</property>
</function>

server.xml

注:修改最后的参数,就是配置mycat的连接账号密码与库

	<user name="root" defaultAccount="true">
		<property name="password">root</property>
		<property name="schemas">zwrdb</property>
	    <property name="readOnly">false</property>
		<!-- 表级 DML 权限设置 -->
		<!--
		<privileges check="false">
			<schema name="TESTDB" dml="0110" >
				<table name="tb01" dml="0000"></table>
				<table name="tb02" dml="1111"></table>
			</schema>
		</privileges>
		 -->
	</user>

注:这里我把mycat的连接账号和密码都是root

启动

mycat start

日志

wrapper.log

STATUS | wrapper  | 2019/04/04 09:56:49 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2019/04/04 09:56:49 | Launching a JVM...
INFO   | jvm 1    | 2019/04/04 09:56:50 | Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO   | jvm 1    | 2019/04/04 09:56:52 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2019/04/04 09:56:52 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2019/04/04 09:56:52 |
INFO   | jvm 1    | 2019/04/04 09:56:55 | MyCAT Server startup successfully. see logs in logs/mycat.log

启动成功,分片分表日志写进mycat.log

端口说明

[root@localhost logs]# netstat -tlnp | grep java
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      7466/java
tcp6       0      0 :::9066                 :::*                    LISTEN      7466/java
tcp6       0      0 :::37972                :::*                    LISTEN      7466/java
tcp6       0      0 :::1984                 :::*                    LISTEN      7466/java
tcp6       0      0 :::8066                 :::*                    LISTEN      7466/java
tcp6       0      0 :::46694                :::*                    LISTEN      7466/java

注:8066为mycat数据连接端口, 9066为mycat管理端口

连接说明

mysql -u root -proot -h 192.168.126.128 -P 8066

展示

mysql> show databases;
+----------+
| DATABASE |
+----------+
| zwrdb    |
+----------+
1 row in set (0.00 sec)

mysql> use zwrdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables in zwrdb |
+-----------------+
| role            |
| subject_t       |
| suggest         |
| cas_user        |
+-----------------+
4 rows in set (0.01 sec)

mysql>

管理命令说明

mysql -u root -proot -h 192.168.126.128 -P 9066

 1.节点查询

mysql> show @@dataNode where schema=zwrdb;
+------+-------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME | DATHOST     | INDEX | TYPE  | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+------+-------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| dn1  | test1/zwrdb|     0 | mysql |      0 |   10 | 1000 |     380 |          0 |        0 |       0 |            -1 |
| dn2  | test2/zwrdb|     0 | mysql |      0 |   10 | 1000 |     380 |          0 |        0 |       0 |            -1 |
+------+-------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
2 rows in set (0.10 sec)

注:DATAHOST:表示对应的datahost属性的值,即数据主机,ACTIVE:表示活跃的连接数量,IDLE:表示空闲的连接数量。SIZE:表示对应的总连接数量

2.心跳

mysql> show @@heartbeat;
+--------+-------+-----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| NAME   | TYPE  | HOST            | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME    | STOP  |
+--------+-------+-----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| hostM1 | mysql | 192.168.126.129 | 3306 |       1 |     0 | idle   |   30000 | 33,5,5       | 2019-04-04 11:00:35 | false |
| hostS1 | mysql | 192.168.126.131 | 3306 |       1 |     0 | idle   |   30000 | 31,4,4       | 2019-04-04 11:00:35 | false |
+--------+-------+-----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
2 rows in set (0.01 sec)

注:RS_CODE状态如下:
  OK_STATUS=1代表正常状态。
  ERROR_STATUS =-1 代表连接错误
  TIMEOUT_STATUS=-2代表连接超时
  INIT_STATUS=0代表初始化状态
若节点发生故障,则会连续进行默认的5个周期检测,心跳连接失败后就会变成-1,节点故障确认,然后可能发生切换。

3.版本

mysql> show @@version;
+---------------------------------------------+
| VERSION                                     |
+---------------------------------------------+
| 5.6.29-mycat-1.6.6.1-release-20181031195535 |
+---------------------------------------------+
1 row in set (0.01 sec)

4.当前连接状态

mysql> show @@connection;
+------------+------+-----------+------+------------+------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
| PROCESSOR  | ID   | HOST      | PORT | LOCAL_PORT | USER | SCHEMA | CHARSET | NET_IN | NET_OUT | ALIVE_TIME(S) | RECV_BUFFER | SEND_QUEUE | txlevel | autocommit |
+------------+------+-----------+------+------------+------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
| Processor0 |    1 | 127.0.0.1 | 9066 |      46274 | root | NULL   | utf8:33 |    321 |    2635 |           422 |        4096 |          0 |     |            |
+------------+------+-----------+------+------------+------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
1 row in set (0.00 sec)

5.后端连接状态

mysql> show @@backend;
+------------+------+---------+-----------------+------+--------+--------+---------+------+--------+----------+------------+--------+----------+---------+------------+
| processor  | id   | mysqlId | host            | port | l_port | net_in | net_out | life | closed | borrowed | SEND_QUEUE | schema | charset  | txlevel | autocommit |
+------------+------+---------+-----------------+------+--------+--------+---------+------+--------+----------+------------+--------+----------+---------+------------+
| Processor0 |    1 |       2 | 192.168.126.129 | 3306 |  58962 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    2 |       4 | 192.168.126.129 | 3306 |  58968 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    3 |       8 | 192.168.126.129 | 3306 |  58970 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    4 |       5 | 192.168.126.129 | 3306 |  58964 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    5 |       3 | 192.168.126.129 | 3306 |  58966 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    6 |       9 | 192.168.126.129 | 3306 |  58972 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    7 |       6 | 192.168.126.129 | 3306 |  58976 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    8 |      10 | 192.168.126.129 | 3306 |  58980 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |    9 |       7 | 192.168.126.129 | 3306 |  58978 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   10 |      11 | 192.168.126.129 | 3306 |  58974 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   11 |       2 | 192.168.126.131 | 3306 |  52040 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   12 |       5 | 192.168.126.131 | 3306 |  52046 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   13 |       9 | 192.168.126.131 | 3306 |  52054 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   14 |       8 | 192.168.126.131 | 3306 |  52052 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   15 |       4 | 192.168.126.131 | 3306 |  52044 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   16 |      10 | 192.168.126.131 | 3306 |  52056 |   3369 |     847 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   17 |      11 | 192.168.126.131 | 3306 |  52048 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   18 |       3 | 192.168.126.131 | 3306 |  52042 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   19 |       6 | 192.168.126.131 | 3306 |  52038 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
| Processor0 |   20 |       7 | 192.168.126.131 | 3306 |  52050 |   3449 |     866 | 4158 | false  | false    |          0 | zwrdb  | latin1:5 | 3       | true       |
+------------+------+---------+-----------------+------+--------+--------+---------+------+--------+----------+------------+--------+----------+---------+------------+
20 rows in set (0.00 sec)

6.缓存信息

mysql> show @@cache;
+-------------------------------------+-------+------+--------+------+------+-------------+----------+
| CACHE                               | MAX   | CUR  | ACCESS | HIT  | PUT  | LAST_ACCESS | LAST_PUT |
+-------------------------------------+-------+------+--------+------+------+-------------+----------+
| ER_SQL2PARENTID                     |  1000 |    0 |      0 |    0 |    0 |           0 |        0 |
| SQLRouteCache                       | 10000 |    0 |      0 |    0 |    0 |           0 |        0 |
| TableID2DataNodeCache.TESTDB_ORDERS | 50000 |    0 |      0 |    0 |    0 |           0 |        0 |
+-------------------------------------+-------+------+--------+------+------+-------------+----------+
3 rows in set (0.02 sec)

注:SQLRouteCache: SQL语句路由缓存,TableID2DateNodeCache:缓存表主键与分片对应关系,ER_SQL2PARENTID:缓存ER分片中子表与父表对应关系。

7.数据源状态

mysql> show @@datasource;
+----------+--------+-------+-----------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME   | TYPE  | HOST            | PORT | W/R  | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+-----------------+------+------+--------+------+------+---------+-----------+------------+
| dn1      | hostM1 | mysql | 192.168.126.129 | 3306 | W    |      0 |   10 | 1000 |     443 |         0 |          0 |
| dn2      | hostS1 | mysql | 192.168.126.131 | 3306 | W    |      0 |   10 | 1000 |     443 |         0 |          0 |
+----------+--------+-------+-----------------+------+------+--------+------+------+---------+-----------+------------+
2 rows in set (0.00 sec)

  

 

转载于:https://www.cnblogs.com/chenglee/p/10653626.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值