mycat實現mysql讀寫分離

1、mycat的下載地址 https://github.com/MyCATApache/Mycat-download
這裏下載的版本是Mycat-server-1.4-release-20151019230038-win.tar.gz
解壓即可

2、需要配置JDK的環境變量;然後修改conf文件夾下的wrapper.conf文件的:

# Java Application
wrapper.java.command=E:\Programs\Java\jdk1.8.0_31\bin
wrapper.working.dir=..

3、修改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://org.opencloudb/">
    <system>
    <property name="defaultSqlParser">druidparser</property>
      <!--  <property name="useCompression">1</property>--> <!--1爲開啓mysql壓縮協議-->
    <!-- <property name="processorBufferChunk">40960</property> -->
    <!-- 
    <property name="processors">1</property> 
    <property name="processorExecutor">32</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="mutiNodeLimitType">1</property> 0:開啓小數量級(默認) ;1:開啓億級數據排序
            <property name="mutiNodePatchSize">100</property> 億級數量排序批量
            <property name="processors">32</property> <property name="processorExecutor">32</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="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
    </system>
    <user name="root">
        <property name="password">root</property>
        <property name="schemas">qs</property>
    </user>

    <user name="user">
        <property name="password">user</property>
        <property name="schemas">qs</property>
        <property name="readOnly">true</property>
    </user>
    <!-- <cluster> <node name="cobar1"> <property name="host">127.0.0.1</property> 
        <property name="weight">1</property> </node> </cluster> -->
    <!-- <quarantine> <host name="1.2.3.4"> <property name="user">test</property> 
        </host> </quarantine> -->

</mycat:server>

注:qs必須和schema.xml的schemas對應;此處的user root 和user 指的是mycat的用戶名;可以通過>mysql -uroot -proot -hlocalhost -P8066 -Dqs 登錄查詢。

4、修改schema.xml文件:

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

    <schema name="qs" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
    </schema>

    <dataNode name="dn1" dataHost="localhost1" database="db1" />


    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
        writeType="0" dbType="mysql" dbDriver="native" switchType="2"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>

        <writeHost host="hostM1" url="192.168.1.56:3306" user="root"
            password="root">

        <readHost host="hostS1" url="192.168.0.83:3306" user="root"
            password="root" />
        </writeHost>

    </dataHost>

</mycat:schema>

沒有涉及到分庫配置比較簡單;database=”db1” db1是兩臺mysql服務器中都存在的數據庫(因爲兩個數據庫已經配置好主從複製,所以數據一樣的)。
注:192.168.1.56 和 192.168.0.83 在已經配置好mysql主從複製的情況下,然後通過mycat的讀寫分離來實現讀取數據庫只在192.168.0.83,寫入數據庫只在192.168.1.56。

balance 屬性
負載均衡類型,目前的取值有 3 種:
1. balance=”0”, 不開啓讀寫分離機制,所有讀操作都發送到當前可用的 writeHost 上。
2. balance=”1”,全部的 readHost 與 stand by writeHost 參與 select 語句的負載均衡,簡單的說,當雙
主雙從模式(M1->S1,M2->S2,並且 M1 與 M2 互爲主備),正常情況下,M2,S1,S2 都參與 select 語句的負載
均衡。
3. balance=”2”,所有讀操作都隨機的在 writeHost、readhost 上分發。
4. balance=”3”,所有讀請求隨機的分發到 wiriterHost 對應的 readhost 執行,writerHost 不負擔讀壓
力,注意 balance=3 只在 1.4 及其以後版本有,1.3 沒有。

writeType 屬性
負載均衡類型,目前的取值有 3 種:
1. writeType=”0”, 所有操作發送到配置的第一個 writeHost,第一個掛了切到還生存的第二個
writeHost,重新啓動後已切換後的爲準,切換記錄在配置文件中:dnindex.properties .
2. writeType=”1”,所有寫操作都隨機發送到配置的 writeHost,1.5 以後廢棄不推薦。

switchType 屬性
- 0 表示不自動切換
- 1 默訃值,自動切換
- 2 基於 MySQL 主從同步的狀態決定是否切換

根據主從延時切換:
1.4 開始支持 MySQL 主從複製狀態綁定的讀寫分離機制,讓讀更加安全可靠,配置如下:
MyCAT 心跳檢查語句配置爲 show slave status ,dataHost 上定義兩個新屬性: switchType=”2” 於
slaveThreshold=”100”,此時意味着開啓 MySQL 主從複製狀態綁定的讀寫分離於切換機制,Mycat 心跳機制
通過檢測 show slave status 中的 “Seconds_Behind_Master”, “Slave_IO_Running”,
“Slave_SQL_Running” 三個字段來確定當前主從同步的狀態以及 Seconds_Behind_Master 主從複製時延,
當 Seconds_Behind_Master>slaveThreshold 時,讀寫分離篩選器會過濾掉此 Slave 機器,防止讀到很久之前
的舊數捤,而當主節點宕機後,切換邏輯會檢查 Slave 上的 Seconds_Behind_Master 是否爲 0,爲 0 時則
表示主從同步,可以安全切換,否則不會切換。

read測試 mycat和192.168.0.83一致:

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -hlocalhost -P8066 -Dqs
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.5.8-mycat-1.4-release-20151019230038 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2016, 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> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -h192.168.0.83 -P3306 -Ddb1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 411
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

測試寫入(已關閉mysql主從複製功能):
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -hlocalhost -P8066 -Dqs
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.5.8-mycat-1.4-release-20151019230038 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2016, 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> insert test1 values(2,’localhost’);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -h192.168.0.83 -P3306 -Ddb1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 412
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -h192.168.1.56 -P3306 -Ddb1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 784
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> select * from test1;
+—-+————-+
| id | name |
+—-+————-+
| 1 | 5656565656 |
| 2 | localhost |
+—-+————-+
2 rows in set (0.00 sec)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章