mycat讀寫分離

一、mycat安裝mysql

mycat安裝依賴jdk,因此要先安裝jdk。linux

mycat官網:http://www.mycat.io/sql

下載:http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz數據庫

下載到/usr/local/src/目錄express

wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gzapache

解壓:tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gzapp

解壓後的目錄移動到/usr/local/: mv mycat   /usr/local/負載均衡

二、配置less

進入/usr/local/mycat/conf/目錄測試

備份schema.xml和server.xml

cp schema.xml schema.xml.bak

cp server.xml server.xml.bak

schema.xml配置後內容以下:

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

   <!-- 定義MyCat的邏輯庫 -->
   <schema name="pcx_schema" checkSQLschema="false" sqlMaxLimit="100" dataNode="pcxNode"></schema>

   <!-- 定義MyCat的數據節點 -->
   <dataNode name="pcxNode" dataHost="dtHost" database="foo"/>

   <!-- 定義數據主機dtHost,連接到MySQL讀寫分離集羣,schema中的每個dataHost中的host屬性值必須惟一
        dataHost實際上配置的就是後臺的數據庫集羣,一個dataHost表明一個數據庫集羣
        balance=「1」,所有的readHost與stand by writeHost參與select語句的負載均衡
        writeType=「0」,全部寫操做發送到配置的第一個writeHost,這裏就是咱們的hostmaster, 第一個掛了切到還生存的第二個writeHost -->

   <dataHost name="dtHost" maxCon="500" minCon="20" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
      <!-- 心跳檢測 -->
      <heartbeat>show slave status</heartbeat>

      <!-- 配置後臺數據庫的IP地址和端口號,還有帳號密碼-->
      <writeHost host="hostMaster" url="172.16.125.133:3306" user="foo" password="foo123" >
         <readHost host="hostSlave" url="172.16.125.134:3306" user="foo" password="foo123"/>
      </writeHost>
      <!-- <writeHost host="hostSlave" url="172.16.125.134:3306" user="foo" password="foo123" /> -->
   </dataHost>
</mycat:schema>

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/">
   <system>
      <!-- 這裏配置的都是一些系統屬性,能夠本身查看mycat文檔 -->
      <!-- <property name="defaultSqlParser">druidparser</property>
      <property name="charset">utf8mb4</property> -->
   </system>


   <!-- 用戶1,對應的MyCat邏輯庫鏈接到的數據節點對應的主機爲主從複製集羣 -->
    <user name="user1">
        <property name="password">root</property>
        <property name="schemas">pcx_schema</property>
    </user>

    <!-- 用戶2,只讀權限 -->
    <user name="user2">
        <property name="password">root</property>
        <property name="schemas">pcx_schema</property>
        <property name="readOnly">true</property>
    </user>
</mycat:server>

三、測試

啓動mycat, 進入目錄/usr/local/mycat/bin

./mycat start

查看是否啓動成功 lsof -i:8066

相關文章
相關標籤/搜索