The separate read and write Mysql Mycat

Mycat is an open source distributed database system is a realization of Mysql server protocol, the front-end user can see it as a proxy database with Mysql Mysql access client and command-line tools, and the rear end can be native protocol and a Mysql server communication, you can also use JDBC protocol to communicate with the majority of mainstream server database, its core function is to partition tables and warehouses, is about to split a large table level N small table in the back-end storage Mysql server where the database or other in.

  • Mycat port:
    • Data port 8066
    • Management port 9066

Deployment Mycat


Preparing the Environment

## 用 hostnamectl set-hostname 根据角色更改主机名
## 关闭防火墙和selinux
[root@mycat ~]# systemctl stop firewalld
[root@mycat ~]# systemctl disable firewalld
[root@mycat ~]# setenforce 0
## 更改 /etc/hosts 文件
[root@mycat ~]# cat >/etc/hosts << EOF
192.168.1.1 mycat
192.168.1.2 master
192.168.1.3 slave1
192.168.1.4 slave2
EOF
## 分发到其他服务器上
[root@mycat ~]# scp /etc/hosts root@master:/etc/hosts
[root@mycat ~]# scp /etc/hosts root@slave1:/etc/hosts
[root@mycat ~]# scp /etc/hosts root@slave2:/etc/hosts
## 在 master、slave1和slave2做Mysql一主双从复制(M-S-S)

Installation myCat
myCat official

## Mycat依赖于Java环境
[root@mycat ~]# yum install java -y
[root@mycat ~]# java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
## 下载Mycat
[root@mycat ~]# wget http://dl.mycat.io/1.6.6.1/Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz
[root@mycat ~]# mkdir /soft/
[root@mycat ~]# tar xf Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz -C /soft/

Configuring applications to connect Mycat account password

## 在配置文件最低下有 Mycat 账号密码和对应的权限配置
[root@mycat ~]# vim /soft/mycat/conf/server.xml

        <!-- 应用连接 mycat 账户(defaultAccount默认连接用户,false=否,true=是) -->
        <user name="test" defaultAccount="true">
                <!-- 应用连接 mycat 密码 -->
                <property name="password">123456</property>
                <!-- 针对哪个库进行授权,多库用逗号隔开 -->
                <property name="schemas">test,www</property>

                <!-- 表级 DML 权限设置(check是否开启,false=否,true=是) -->
                <privileges check="false">
                        <!-- 针对数据下的所有表授权(1=有,0=没有,分别对应:insert,update,select,delete) -->
                        <schema name="TEST" dml="0110" >
                                <!-- 针对数据下的单个表授权 -->
                                <table name="t1" dml="0000"></table>
                                <table name="t2" dml="1111"></table>
                        </schema>
                </privileges>
        </user>

        <user name="www">
                <property name="password">123456</property>
                <property name="schemas">www</property>
                <!-- 是否只读 -->
                <property name="readOnly">false</property>
        </user>

Configuring Mycat connect to the backend database

  • balance Load balancing poll
    • balance="0" Not open the separate read and write mechanism, all reads are sent to the currently available writeHost.
    • balance="1" All hosts are involved in the load balancing select statement, but the statement was written by the writeHost
    • balance="2" All read operations are random in writeHost, readhost on the distribution.
  • writeType Load balancing state
    • writeType="0" All writes to the configuration of the first transmission writeHost, the first bolt tangential to the second still exist
    • writeType="1" All writes are sent to writeHost random configuration, after discarding 1.5 is not recommended.

Author: Johnny_Kam
link: https://www.jianshu.com/p/4e15cad5ce6a
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

[root@mycat ~]# vim /soft/mycat/conf/schema.xml 
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
     <!-- 定义逻辑库(逻辑库名、是否分片、返回最大行数、数据节点) -->
     <schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> </schema>
     <schema name="www" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn2"> </schema>
    <!-- 定义数据节点(数据节点名、数据库实例、真实数据库名) -->
    <dataNode name="dn1" dataHost="dn1pool" database="test" />
    <dataNode name="dn2" dataHost="dn2pool" database="www" />
    <!-- 定义 test 数据库实例(实例名、最大连接数、最初连接数、负载均衡轮询、负载均衡状态、数据库类型、连接类型、是否自动切换、) -->
    <dataHost name="dn1pool" maxCon="1000" minCon="10" balance="1"
        writeType="0" dbType="mysql" dbDriver="native" switchType="1">
        <!-- 健康检查 -->
        <heartbeat>select user()</heartbeat>
        <!-- 定义读写主机 , 可以定义多个 -->
        <writeHost host="master" url="master:3306" user="test" password="Sgy123.com">
            <readHost host="slave1" url="slave1:3306" user="test" password="Sgy123.com" />
            <readHost host="slave2" url="slave2:3306" user="test" password="Sgy123.com" />
        </writeHost>
     </dataHost>
     <dataHost name="dn2pool" maxCon="1000" minCon="10" balance="1"
         writeType="0" dbType="mysql" dbDriver="native" switchType="1">
        <!-- 健康检查 -->
        <heartbeat>select user()</heartbeat>
            <writeHost host="master" url="master:3306" user="www" password="Sgy123.com">
                <readHost host="slave1" url="slave1:3306" user="www" password="Sgy123.com" />
                <readHost host="slave2" url="slave2:3306" user="www" password="Sgy123.com" />
            </writeHost>
    </dataHost>
</mycat:schema>

Add the appropriate analog data and user and database configuration master

## 模拟数据省略。。。
mysql> create databse test;
mysql> create databse www;
## 模拟数据省略。。。
mysql> grant all on *.* to test@'192.168.1.%' identified by 'Sgy123.com';
mysql> grant all on www.* to www@'192.168.1.%' identified by 'Sgy123.com';

Start Mycat Middleware

[root@mycat conf]# /soft/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat logs]# lsof -i :8066
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 7905 root 81u IPv6 107719 0t0 TCP *:8066 (LISTEN)
[root@mycat logs]# lsof -i :9066
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 7905 root 77u IPv6 107717 0t0 TCP *:9066 (LISTEN)

Connection Mycat middleware

[root@mycat ~]# mysql -P8066 -utest -p'123456' -h127.0.0.1
[root@mycat ~]# mysql -P9066 -utest -p'123456' -h127.0.0.1

[root@mycat ~]# mysql -P8066 -uwww -p'123456' -h127.0.0.1
[root@mycat ~]# mysql -P9066 -uwww -p'123456' -h127.0.0.1

Guess you like

Origin www.cnblogs.com/songguoyou/p/11883870.html