MyCat 安装

Posted 那个吧..你懂的!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyCat 安装相关的知识,希望对你有一定的参考价值。

MyCat 安装

  1. 下载 mycat

    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
    tar -zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
    mv mycat /usr/local/
    cd /usr/local/mycat
    
  2. 配置server 用户

    vim bin/server.xml
    
    <!-- 配置 root 密码及 schemas  -->
    <user name="root" defaultAccount="true">
        <property name="password">root</property>
        <property name="schemas">user</property>
        <property name="defaultSchema">TESTDB</property>         
    </user>
    <user name="user">
        <property name="password">user</property>
        <property name="schemas">user</property>
        <property name="readOnly">true</property>
        <property name="defaultSchema">TESTDB</property>
    </user>
    
  3. 配置 schema 数据库配置, 笔者两个数据库 host 分别为192.168.31.202:3306, 192.168.31.203:3306, 两个库分别有数据库 user_node_1 和 user_node_2,每个库中都有一个 user 表

    vim bin/schema.xml
    
    <schema name="user" checkSQLschema="true" sqlMaxLimit="100" randomDataNode="dn1">
                    <table name="user" dataNode="dn1,dn2" rule="auto-sharding-long" splitTableNames ="true"/>
    
    </schema>
    
    <dataNode name="dn1" dataHost="node-1" database="user_node_1" />
    <dataNode name="dn2" dataHost="node-2" database="user_node_2" />
    
    
    <dataHost name="node-1" maxCon="1000" minCon="10" balance="0"
                              writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="hostM1" url="192.168.31.201:3306" user="root"
                   password="root">
    </dataHost>
    <dataHost name="node-2" maxCon="1000" minCon="10" balance="0"
                writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="M2" url="192.168.31.203:3306" user="root"
                   password="root">
        </writeHost>
    </dataHost>
    
  4. 由于本案例只有两个 database,所以需要修改默认auto-sharding-long的规则

    vim conf/autopartition-long.txt
    0-500M=0
    500M-1000M=1
    
  5. 启动 mycat

    ./bin/mycat console
    
  6. 连接 mycat, 注意端口是 8066

可以看到里面有配置好的逻辑分库 user 和逻辑分表 user

  1. 测试插入

    insert into `user` (id,username) values(1, \'thomas-fan\');
    

    可以看到插入成功,并且在分库上写入成功

  1. 配置开机自启脚本

    vim /etc/init.d/mycat
    
    !/bin/sh
    #
    # Simple Mycat init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.
    
    ### BEGIN INIT INFO
    # Provides:     mycat
    # Default-Start:        2 3 4 5
    # Default-Stop:         0 1 6
    # Short-Description:    Mycat data structure server
    # Description:          Mycat data structure server. See https://github.com/MyCATApache/Mycat-Server
    ### END INIT INFO
    
    EXEC=/usr/local/mycat/bin/mycat
    PIDFILE=/usr/local/mycat/logs/mycat.pid
    
    case "$1" in
        start)
            if [ -f $PIDFILE ]
            then
                    echo "$PIDFILE exists, process is already running or crashed"
            else
                    echo "Starting Mycat server..."
                    $EXEC start
            fi
            ;;
        stop)
            if [ ! -f $PIDFILE ]
            then
                    echo "$PIDFILE does not exist, process is not running"
            else
                    PID=$(cat $PIDFILE)
                    echo "Stopping ..."
                    $EXEC stop
                    while [ -x /proc/$PID ]
                    do
                        echo "Waiting for Mycat to shutdown ..."
                        sleep 1
                    done
                    echo "Mycat stopped"
            fi
            ;;
        *)
            echo "Please use start or stop as first argument"
            ;;
    esac
    
    cd /etc/init.d/
    chmod +x mycat
    chkconfig mycat on
    systemctl start mycat
    

以上是关于MyCat 安装的主要内容,如果未能解决你的问题,请参考以下文章

安装mycat

MyCat的安装

mycat中间件--linux安装mycat1.6版本

Mycat

Cento7.4安装mycat和mycat-eye

Linux下MyCat和MyCat_web的安装和配置