1. 程式人生 > >MYSQL讀寫分離解決方案:MYCAT部署實錄

MYSQL讀寫分離解決方案:MYCAT部署實錄

mysql mycat 讀寫分離

MASTER (KING01)

[root@king01 ~]# 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)

[root@king02 ~]# 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)

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


[root@king03 ~]# 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
[root@king03 ~]# source /etc/profile


[root@king03 ~]# 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)


[root@king03 ~]# tar zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local

[root@king03 ~]# vi /etc/profile
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
[root@king04 ~]# source /etc/profile


[root@king03 ~]# cd /usr/local/mycat/conf/
[root@king03 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>
        -->


[root@king03 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>
[root@king03 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>


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


[root@king03 ~]# cd /usr/local/mycat/logs/
[root@king03 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


[root@king04 ~]# 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部署實錄