基於centos7安裝mycat服務配置讀寫分離

1、mycat讀寫分離架構思路

在這裏插入圖片描述

  1. mycat官網
  2. mycat權威指南

2、環境準備

  1. 兩臺服務器都安裝了mysql數據庫均能正常使用。(192.168.78.230主,192.168.78.235從)
    未安裝mysql的小夥伴可以參考文章:https://blog.csdn.net/qq_34846877/article/details/107044396
  2. 一臺服務器安裝mycat,如果資源不夠可以安裝在兩臺服務器中其中一臺。(192.168.78.231)
  3. mycat安裝包地址:Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
  4. linux下載命令:wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz

3、mycat安裝

上傳安裝包到 /usr/local 並解壓後得到mycat文件,目錄如下圖
tar -zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
在這裏插入圖片描述
隨後刪除安裝包:rm -rf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
存儲不足,各位隨意,可以移到其它位置。

4、配置

進入到mycat進行配置讀寫分離,刪除多餘的配置信息。

4.1、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://io.mycat/">

    <!-- 讀寫都可用的用戶 -->
    <user name="root" defaultAccount="true">
        <property name="password">root</property>
        <property name="schemas">TESTDB</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>

</mycat:server>

4.2、schema.xml 配置

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

        <schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1">
        		<!-- auto sharding by id (long) -->
                <!-- <table name="record" primaryKey="id" autoIncrement="true" subTables="record_$0-3" dataNode="dn1" rule="rule1" splitTableNames ="true"/> -->

        </schema>

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

        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="hostM1" url="192.168.78.230:3306" user="root" password="root">
                    <!-- 可以配置多個從庫 -->
                    <readHost host="hostS1" url="192.168.78.235:3306" user="root" password="root" />
                </writeHost>

        </dataHost>

</mycat:schema>

4.3、rule.xml 配置

由於schema.xml中註釋了對錶的配置,故沒有采用規則,所有rule.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:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
    <!-- 定義一個表路由規則 -->
    <tableRule name="rule1">
          <rule>
                <!-- id爲真實分表字段,這裏爲路由字段,表示通過這個字段拆分 -->
                <columns>id</columns>
                <!-- 指定路由則規則所使用的算法 -->
                <algorithm>rang-mod</algorithm>
          </rule>
    </tableRule>

    <!-- 枚舉規則 -->
    <tableRule name="rule2">
        <rule>
           <columns>city</columns>
           <algorithm>hash-int</algorithm>
        </rule>
    </tableRule>

    <!-- 定義取模算法,name表示名稱,class表示所使用的算法類 -->
    <function name="rang-mod" class="io.mycat.route.function.PartitionByMod">
          <!-- count表示分表的個數 -->
          <property name="count">4</property>
    </function>

    <!-- 定義枚舉算法,mapFile:指定枚舉映射文件
                type:字段屬性類型,默認值爲0,0表示Integer,非零表示String
                defaultNode:指定默認節點,如果city值沒有在partition-hash-int.txt中配置枚舉映射,則會被映射到默認的節點上,
                其值從0開始,對應了schema.xml文件<table>的subTables屬性值
    -->
    <function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
                <property name="mapFile">partition-hash-int.txt</property>
                <property name="type">1</property>
                <property name="defaultNode">1</property>
    </function>

</mycat:rule>

配置完成後啓動mycat服務
啓動命令:./mycat start
停止命令:./mycat stop
重啓命令:./mycat restart
在這裏插入圖片描述
查看相應的日誌是否啓動成功
查看日誌:tailf wrapper.log
在這裏插入圖片描述

5、功能測試

分別用不用的用戶連接mycat後測試

  1. user 用戶連接 --> 該用戶只讀。
    在這裏插入圖片描述
  2. root 用戶連接 --> 插入成功
    在這裏插入圖片描述
    利用mycat可以很好緩解數據庫負載過大引起的緩慢問題,以上就是基於mycat讀寫分離的配置。自我總結學習並分享給大家。
    上一篇:基於centos7安裝MySQL8主從複製配置
    下一篇:基於mycat整合springboot項目實現讀寫分離
    學習參考:螞蟻課堂mycat官網
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章