Mysql-proxy安装

Mysql-proxy安装

mysql-proxy用于实现mysql代理。它的安装过程如下:

  1. 下载mysql-proxy安装包
    wget http://mirrors.dotsrc.org/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz
  2. 解压安装包
    tar -xzf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz
  3. 配置环境变量
    可以把解开的目录放至到自己希望的位置,如:
    mv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit /root/mysql-proxy
    然后在~/.bashrc中添加PATH路径,如:
    export PATH="/root/mysql-proxy/bin:$PATH" 
    添加后 source ~/.bashrc
  4. 在/etc/init.d/下编辑一个启动脚本,机器重启后也可以自动运行
    vi /etc/init.d/mysql-proxy
    #!/bin/sh
    basedir=/root/mysql-proxy
    bindir=$basedir/bin
    vardir=$basedir/var
    export LUA_PATH=$basedir/share/mysql-proxy/?.lua
    
    mode=$1
    if [ -z "$mode" ] ; then
      mode="start" 
    fi
    
    if [ ! -d $basedir/var ];then
        mkdir $basedir/var
    fi
    
    case $mode in
    'start')
        $bindir/mysql-proxy --daemon \
            --basedir=$basedir \
            --pid-file=$vardir/proxy.pid \
            --log-file=$vardir/proxy.log \
            --log-level=info \
            --proxy-address=0.0.0.0:4306 \
            --proxy-backend-addresses=mysql_host:3306
        ;;  
    'stop')
        if [ -e $vardir/proxy.pid ];then
            cat $vardir/proxy.pid | xargs kill
        fi
        ;;
    'restart')
        if $0 stop; then
            $0 start
        else
            echo "retart failed!!!" >&2
            exit 1
        fi
        ;;
    *)
        echo "usage: mysql-proxy start|stop" >&2
        exit 1
    esac
    if [ $? -ne 0 ];then
        echo "failed to $mode mysql-proxy" >&2
        exit 1
    fi
    exit 0
    

    现在解释一下启动脚本:
    --daemon 采用daemon方式启动
    --proxy-address 指定mysql proxy的监听端口
    --proxy-backend-addresses=:3306 指定目标mysql主机的端口
    此外,mysql-proxy还可以配置主从读写分离的多个mysql,具体配置方法参见官方文档。
  5. 启动mysql-proxy
    sh /etc/init.d/mysql-proxy start
  6. 停止mysql-proxy
    sh /etc/init.d/mysql-proxy stop

你可能感兴趣的:(mysql,proxy)