MYSQL读写分离解决方案:MYCAT部署实录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL读写分离解决方案:MYCAT部署实录相关的知识,希望对你有一定的参考价值。

MASTER (KING01)

[[email protected] ~]# mysql -uroot -pabcd.1234
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 |      331 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

SLAVE(KING02)

[[email protected] ~]# mysql -uroot -pabcd.1234
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.201
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 331
               Relay_Log_File: relay-bin.000005
                Relay_Log_Pos: 494
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 331
              Relay_Log_Space: 824
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 6dc7ad57-25b9-11e8-9384-08002746e9d6
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)


MYCAT(KING03)

[[email protected] ~]# yum remove -y java-1.7.0-openjdk
[[email protected] ~]# yum remove -y java-1.6.0-openjdk
[[email protected] ~]# tar zxvf jdk-7u80-linux-x64.tar.gz
[[email protected] ~]# mv jdk1.7.0_80 /usr/local


[[email protected] ~]# vi /etc/profile
JAVA_HOME=/usr/local/jdk1.7.0_80
JAVA_BIN=/usr/local/jdk1.7.0_80/bin
PATH=$PATH:$JAVA_BIN
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
[[email protected] ~]# source /etc/profile


[[email protected] ~]# java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)


[[email protected] ~]# tar zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local

[[email protected] ~]# vi /etc/profile
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
[[email protected] ~]# source /etc/profile


[[email protected] ~]# cd /usr/local/mycat/conf/
[[email protected] conf]# vi server.xml 
        <user name="tpcc" defaultAccount="true">
                <property name="password">tpcc</property>
                <property name="schemas">tpcc</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>
        <!--
        <user name="user">
                <property name="password">user</property>
                <property name="schemas">TESTDB</property>
                <property name="readOnly">true</property>
        </user>
        -->


[[email protected] conf]# vi schema.xml 
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
        <schema name="tpcc" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn_tpcc"> </schema>
        <dataNode name="dn_tpcc" dataHost="dh_tpcc" database="tpcc1000" />
        <dataHost name="dh_tpcc" maxCon="1000" minCon="10" balance="1"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="2"  slaveThreshold="100">
                <heartbeat>show slave status</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="tpcc_m1" url="192.168.1.201:3306" user="tpcc"
                                   password="tpcc">
                        <!-- can have multi read hosts -->
                        <readHost host="tpcc_s1" url="192.168.1.202:3306" user="tpcc" password="tpcc" />
                </writeHost>
                <writeHost host="tpcc_m2" url="192.168.1.201:3306" user="tpcc"
                                   password="tpcc" />
        </dataHost>
</mycat:schema>
[[email protected] conf]# vi log4j2.xml 
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d [%-5p][%t] %m %throwable{full} (%C:%F:%L) %n"/>
        </Console>

        <RollingFile name="RollingFile" fileName="${sys:MYCAT_HOME}/logs/mycat.log"
                     filePattern="${sys:MYCAT_HOME}/logs/$${date:yyyy-MM}/mycat-%d{MM-dd}-%i.log.gz">
        <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] (%l) - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
                <TimeBasedTriggeringPolicy/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <!--<AsyncLogger name="io.mycat" level="info" includeLocation="true" additivity="false">-->
            <!--<AppenderRef ref="Console"/>-->
            <!--<AppenderRef ref="RollingFile"/>-->
        <!--</AsyncLogger>-->
        <asyncRoot level="debug" includeLocation="true">

            <!--<AppenderRef ref="Console" />-->
            <AppenderRef ref="RollingFile"/>

        </asyncRoot>
    </Loggers>
</Configuration>


[[email protected] ~]# mycat start
Starting Mycat-server...
[[email protected] ~]# mycat status
Mycat-server is running (9648).
[[email protected] ~]# netstat -tunlp
tcp        0      0 :::8066                     :::*                        LISTEN      9650/java           
tcp        0      0 :::9066                     :::*                        LISTEN      9650/java


[[email protected] ~]# cd /usr/local/mycat/logs/
[[email protected] logs]# cat wrapper.log 
STATUS | wrapper  | 2018/03/15 14:21:50 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2018/03/15 14:21:50 | Launching a JVM...
INFO   | jvm 1    | 2018/03/15 14:21:55 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2018/03/15 14:21:55 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2018/03/15 14:21:55 | 
INFO   | jvm 1    | 2018/03/15 14:21:58 | MyCAT Server startup successfully. see logs in logs/mycat.log


[[email protected] ~]# mysql -utpcc -ptpcc -P 8066 -h 192.168.1.203 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2013, 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> show databases;
+----------+
| DATABASE |
+----------+
| tpcc     |
+----------+
1 row in set (0.01 sec)


以上是关于MYSQL读写分离解决方案:MYCAT部署实录的主要内容,如果未能解决你的问题,请参考以下文章

mysql读写分离实现 jdk+mycat环境部署

MyCat实现读写分离+分库分表+全局表

docker安装mycat并实现mysql读写分离和分库分表

MySQL高可用读写分离集群视频课程 MyCat教程

MyCat 部署读写分离-分库,未分表

MySQL(18) 通过Docker搭建Mycat实现读写分离