• linux系统安装mycat,并配置读写分离规则


    一.准备工作

    1.因为Mycat是在java环境开发的,所以需要在实验环境下安装java,官方建议jdk1.7及以上版本,我这里也写一下将jdk1.6替换为jdk1.7的步骤,用来记录一下,以免下次在遇到。

    jdk1.7下载路径:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html,我这里下载的是jdk-7u80-linux-x64.gz,将该文件解压到你要存放的目录,我这里存放的位置是/usr/local/java,如下图

    2.使用vi /etc/profile命令修改环境变量,具体配置如下

    之前的jdk版本是1.6,所以将之前的地址改变为1.7的安装地址

    3.检查是否安装成功

    到此,java环境准备完毕

    4.下载linux版本的mycat,我这里也给出地址:https://github.com/MyCATApache/Mycat-download/tree/master/1.3.0.3-release ,我下载的是Mycat-server-1.3.0.3-release-20150527095523-linux.tar.gz,下载成功之后将它传到服务器上,我这里存放的地址是:/usr/local/mycat/ ,接下来就说说具体的配置

    二.配置Mycat文件,实现简单的读写分离

    1.修改mycat/conf/server.xml文件,具体修改如下

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <!-- - - Licensed under the Apache License, Version 2.0 (the "License");
    3.  
      - you may not use this file except in compliance with the License. - You
    4.  
      may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
    5.  
      - - Unless required by applicable law or agreed to in writing, software -
    6.  
      distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
    7.  
      WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
    8.  
      License for the specific language governing permissions and - limitations
    9.  
      under the License. -->
    10.  
      <!DOCTYPE mycat:server SYSTEM "server.dtd">
    11.  
      <mycat:server xmlns:mycat="http://org.opencloudb/">
    12.  
      <system>
    13.  
      <property name="defaultSqlParser">druidparser</property>
    14.  
      <!--
    15.  
      <property name="processorBufferChunk">40960</property>
    16.  
      <property name="processors">1</property>
    17.  
      <property name="processorExecutor">32</property>
    18.  
      -->
    19.  
      <!--默认是65535 64K 用于sql解析时最大文本长度 -->
    20.  
      <!--<property name="maxStringLiteralLength">65535</property>-->
    21.  
      <!--<property name="sequnceHandlerType">0</property>-->
    22.  
      <!--<property name="backSocketNoDelay">1</property>-->
    23.  
      <!--<property name="frontSocketNoDelay">1</property>-->
    24.  
      <!--<property name="processorExecutor">16</property>-->
    25.  
      <!--
    26.  
      <property name="mutiNodeLimitType">1</property> 0:开启小数量级(默认) ;1:开启亿级数据排序
    27.  
      <property name="mutiNodePatchSize">100</property> 亿级数量排序批量
    28.  
      <property name="processors">32</property> <property name="processorExecutor">32</property>
    29.  
      <property name="serverPort">8066</property> <property name="managerPort">9066</property>
    30.  
      <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
    31.  
      <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property>
    32.  
       -->
    33.  
      </system>
    34.  
      <user name="root">
    35.  
      <property name="password">123456</property>
    36.  
      <property name="schemas">TESTDB</property>
    37.  
      </user>
    38.  
       
    39.  
      <user name="user">
    40.  
      <property name="password">123456</property>
    41.  
      <property name="schemas">TESTDB</property>
    42.  
      <property name="readOnly">true</property>
    43.  
      </user>
    44.  
        <!--以上配置的是连接mycat的用户名和密码-->
    45.  
      <!--
    46.  
      <cluster>
    47.  
      <node name="cobar1"> <property name="host">127.0.0.1</property>
    48.  
      <property name="weight">1</property> </node>
    49.  
      </cluster>
    50.  
      -->
    51.  
      <!--
    52.  
      <quarantine>
    53.  
      <host name="1.2.3.4"> <property name="user">test</property> </host>
    54.  
      </quarantine>
    55.  
       -->
    56.  
       
    57.  
      </mycat:server>
    2.修改mycat/conf/schema.xml文件,具体修改如下
    1.  
      <?xml version="1.0"?>
    2.  
      <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    3.  
      <mycat:schema xmlns:mycat="http://org.opencloudb/">
    4.  
       
    5.  
      <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
    6.  
      <!-- auto sharding by id (long) -->
    7.  
      <!-- <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />-->
    8.  
       
    9.  
      <!-- global table is auto cloned to all defined data nodes ,so can join with any table whose sharding node is in the same data node -->
    10.  
      <!-- <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />-->
    11.  
      <!-- <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />-->
    12.  
      <!-- random sharding using mod sharind rule -->
    13.  
      <!-- <table name="hotnews" primaryKey="ID" dataNode="dn1,dn2,dn3" rule="mod-long" />-->
    14.  
      <!-- <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3" rule="mod-long" /> -->
    15.  
      <!-- <table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" />-->
    16.  
      <!--<table name="customer" primaryKey="ID" dataNode="dn1,dn2"rule="sharding-by-intfile">
    17.  
      <childTable name="orders" primaryKey="ID" joinKey="customer_id" parentKey="id">
    18.  
      <childTable name="order_items" joinKey="order_id" parentKey="id" /></childTable>
    19.  
      <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" parentKey="id" />
    20.  
      </table>-->
    21.  
      </schema>
    22.  
       
    23.  
      <dataNode name="dn1" dataHost="localhost1" database="test" />
    24.  
      <!--上面的database属性设置的就是你要连接的数据库-->
    25.  
       <!--
    26.  
      <dataNode name="dn2" dataHost="localhost1" database="db2" />
    27.  
      <dataNode name="dn3" dataHost="localhost1" database="db3" />
    28.  
      <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
    29.  
      <dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" />
    30.  
      <dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" />
    31.  
      -->
    32.  
      <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native">
    33.  
      <heartbeat>select user()</heartbeat>
    34.  
      <!-- can have multi write hosts -->
    35.  
      <!--设置执行写操作的数据库的IP地址、用户名以及密码-->
    36.  
      <writeHost host="hostM1" url="127.0.0.1:3306" user="root" password="123456">
    37.  
      <!-- 以下是设置多个执行读操作的数据库的IP地址、用户名以及密码 -->
    38.  
      <readHost host="hostS1" url="192.168.1.1:3306" user="root" password="123456"/>
    39.  
      <readHost host="hostS1" url="192.168.1.2:3306" user="root" password="123456"/>
    40.  
      <readHost host="hostS1" url="192.168.1.3:3306" user="root" password="123456"/>
    41.  
      </writeHost>
    42.  
      <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
    43.  
      </dataHost>
    44.  
      <!--
    45.  
      <dataHost name="jdbchost" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc">
    46.  
      <heartbeat>select user()</heartbeat>
    47.  
      <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost>
    48.  
      </dataHost>
    49.  
      -->
    50.  
      <!--
    51.  
      <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0"
    52.  
      dbType="mysql" dbDriver="jdbc">
    53.  
      <heartbeat>select user()</heartbeat>
    54.  
      <writeHost host="hostM1" url="jdbc:mysql://localhost:3306"
    55.  
      user="root" password="123456">
    56.  
      </writeHost>
    57.  
      </dataHost>
    58.  
      -->
    59.  
      </mycat:schema>
    3.修改mycat/conf/wrapper.conf文件,具体修改如下
    找到wrapper.java.command=java 将其改为wrapper.java.command=%JAVA_HOME%/bin/java
    
    4.启动mycat服务

    因为我这里之前已经启动过了,所以提示已经在运行,可以使用ps -ef |grep mycat检查进程

    如果使用该命令查询出来的不是如图所示的话,有可能mycat服务就没启动起来,这是进入log目录查看日志文件,使用 cat /usr/local/mycat/logs/wrapper.log,我第一次启动的时候就失败了,查看日志文件发现如下错误

    这是不要慌,仔细排查日志,这里说的就是我的端口名称未被识别,这是就应该想到肯定跟hosts文件有关,使用cat /etc/sysconfig/network

    这是我们将上图中的HOSTNAME属性改为localhost,这是应重启才会使修改生效,重启完之后启动mycat,然后查看日志文件,提示启动成功

    三.测试

  • 相关阅读:
    命令模式
    软件设计师_例题
    软件设计师_计算机系统基础(1.1)
    ForkJoin之ForkJoinTask框架学习笔记
    Oracle使用语句块之循环插入数据
    SpringCloud搭建分布式配置中心(基于git)
    Docker配置JDK1.8
    Linux命令查看文件内容
    Mac配置maven环境命令
    Docker安装mysql8
  • 原文地址:https://www.cnblogs.com/wljsir/p/9390344.html
Copyright © 2020-2023  润新知