MySQL-Proxy 实现 MySQL 的负载均衡和读写分离


转载时请务必以超链接形式标明文章 原始出处和作者信息及本版权声明
链接:http://www.dbasky.net/archives/2009/07/mysql-proxy-mysql.html
      Mysql proxy是MySQL官方开发一个mysql数据库前端代理,使用C语言编写,并且支持lua来编写proxy规则,可以用来实现mysql的读写分离,负载均衡等等功能.
mysql-proxy.jpg

一:安装Mysql-Proxy

1)系统环境的设置(系统为CentOs 5.2):
确定这些包已经安装:GNU Autotools, flex, pkg-config, bazaar, MySQL client libraries
[root@xutest opt]# yum install autoconf automake libtool flex pkgconfig bzr mysql-devel
因为系统自带的libevent 和 glib的版本都比较低,然而mysql proxy 0.7.0对其版本要求是libevent>1.4.0,glib2>2.1.16.因此要手动编译.

A:libevent
[root@xutest opt]#wget http://www.dbasky.net/tool/libevent-1.4.9-stable.tar.gz
[root@xutest opt]#tar zvfx libevent-1.4.9-stable.tar.gz
[root@xutest opt]#cd libevent-1.4.9-stable
[root@xutest opt]#./configure
[root@xutest opt]#make
[root@xutest opt]#make install

B:GLIB
[root@xutest opt]# wget http://www.dbasky.net/tool/glib-2.18.4.tar.gz
[root@xutest opt]#tar zvfx glib-2.18.4.tar.gz
[root@xutest opt]#cd glib-2.18.4
[root@xutest opt]#./configure
[root@xutest opt]#make
[root@xutest opt]#make install
2)安装lua 5.1
[root@xutest opt]#wget http://www.dbasky.net/tool/lua-5.1.4.tar.gz
[root@xutest opt]#tar zvfx lua-5.1.4.tar.gz
[root@xutest opt]#cd lua-5.1.4
[root@xutest opt]#vi src/Makefile   #在64位机上编译出现了"relocations"错误,需要在CFLAGS里加上-fPIC.
[root@xutest opt]#make linux
[root@xutest opt]#make install
[root@xutest opt]#cp etc/lua.pc /usr/local/lib/pkgconfig/
重要:让pkg-config找到自己编译的库在哪里
[root@xutest opt]#export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
3)安装Mysql-proxy
[root@xutest opt]#wget http://www.dbasky.net/tool/mysql-proxy-0.7.0.tar.tar
[root@xutest opt]#tar zxvf mysql-proxy-0.7.0.tar.tar
[root@xutest opt]#cd mysql-proxy-0.7.0
[root@xutest opt]#./configure
[root@xutest opt]#make
[root@xutest opt]#make check
[root@xutest opt]#make install
编译完成,可以检查一下最终结果:
[root@xutest opt]#mysql-proxy -V
mysql-proxy: error while loading shared libraries: /usr/local/lib/libmysql-chassis.so.0: cannot restore segment prot after reloc: Permission denied
解决办法:
编辑/etc/selinux/config,找到这段:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=enforcing
把 SELINUX=enforcing 注释掉:#SELINUX=enforcing ,然后新加一行为:
SELINUX=disabled
保存,关闭
在你保证SElinux 被disable后.还执行下:chcon -t texrel_shlib_t
例:
[root@testserver mysql-proxy-0.7.0]# chcon -t texrel_shlib_t /usr/local/lib/libmysql-chassis.so.0
再一次检查:
[root@xutest opt]#mysql-proxy -V
mysql-proxy 0.7.0
  glib2: 2.18.4
  libevent: 1.1a
  admin: 0.7.0
  proxy: 0.7.0

二:使用:
[root@xutest opt]#mysql-proxy --help-all           #查看所有的设置选项               
                  
管理功能选项

--admin-address=host:port -- 指定一个mysqo-proxy的管理端口,缺省是 4041
代理功能选项

--proxy-address=host:port -- 这个是mysql-proxy 服务器端的监听端口,缺省是 4040
--proxy-read-only-backend-addresses=<host:port> -- 远程只读Slave服务器的地址和端口,缺省为不设置(本选项在mysql-proxy0.5.0版本中没有)
--proxy-backend-addresses=host:port -- 指定远程MySQL服务器地址和端口,可以设置多个,
--proxy-skip-profiling -- 关闭查询分析功能,缺省是打开的
--proxy-fix-bug-25371 -- 修正 mysql的libmysql版本大于5.1.12的一个#25371号bug
--proxy-lua-script=file -- 指定一个Lua脚本程序来控制mysql-proxy的运行和设置,这个脚本在每次新建连接和脚本发生修改的的时候将重新调用
其他选项

--daemon -- mysql-proxy以守护进程方式运行
--pid-file=file -- 设置mysql-proxy的存储PID文件的路径

例:
  监听本地Mysql 3307端口:
[root@xutest opt]#/usr/local/sbin/mysql-proxy --proxy-backend-addresses=127.0.0.1:3307
通过其它机器的mysql客户端连接:
[root@server ~]#/usr/local/mysql/bin/mysql -uroot -h 192.168.1.6 -p 4040
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.0.45

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

例:对两台或者多台Mysql的负载均衡:
        假设有两台Slave的读数据服务器,我们使用mysql_proxy多个数据库的轮询技术来进行两台Slave数据库的负载均衡分散访问措施,mysql-proxy可以做到,缺省就具有了简单的均衡功能,它自带的均衡功能是当有多台MySQL服务器的时候,进行逐个访问的原则,比如有A、B两台MySQL服务器,那么第一个连接就访问A,第二个连接访问B,第三个连接访问A,一次分布,达到A、B两台MySQL访问均衡。
       对于mysql-proxy这个功能,我们进行检测一下。增加一个Lua检测脚本 test-conn.lua,就一个函数:

function read_query( packet )
    print("read_query: connection.backend_ndx: ", proxy.connection.backend_ndx)
end

执行:
[root@xutest opt]#/usr/local/sbin/mysql-proxy --proxy-address=192.168.1.6:3306 --proxy-backend-addresses=192.168.1.231:3306 --proxy-backend-addresses=192.168.1.10:3306 --proxy-lua-script=/opt/test-conn.lua

通过其它机器的mysql客户端进行多次的连接登陆,在mysql-proxy的服务端输出:
read_query: connection.backend_ndx:     1
read_query: connection.backend_ndx:     2
read_query: connection.backend_ndx:     1
read_query: connection.backend_ndx:     2
read_query: connection.backend_ndx:     1
read_query: connection.backend_ndx:     2
附:
 启动脚本:
 [root@xutest opt]# vi mysql-proxy.sh

 #!/bin/sh
export LUA_PATH=/usr/local/share/mysql-proxy/?.lua
case "$1" in
start)
     /usr/local/sbin/mysql-proxy --daemon \
     --proxy-address=192.168.1.6:3306 \
     --proxy-backend-addresses=192.168.1.6:3307 \
     #--proxy-read-only-backend-addresses=192.168.1.10:3306 \
     --proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua
     ;;
     stop)
     killall mysql-proxy
      ;;
     *)
    echo "Usage: mysql-proxy {start|stop}"
    exit 1
   esac
三:管理Mysql Proxy
            在启动 mysql-proxy 之后,就会打开两个端口,一个是4040,这个是缺省用于提供给MySQL客户端连接使用的端口,可以通过 --proxy-address 选项修改为其他的,比如设置为 --proxy-address=192.168.1.6:3306,就完全模拟成了一台MySQL数据库了。另外默认开放 4041 的端口,这个是用来提供给管理 mysql-proxy 使用的,可以使用标准的mysql客户端来连接,同样能够通过 --admin-address 选项来设置为其他管理端口。
例:

 [root@xutest opt]#/usr/local/mysql/bin/mysql -u root -h 192.168.1.6 -P 4041

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 10
Server version: 5.0.45-agent MySQL Enterprise Agent
mysql> select * from proxy_connections;
+------+--------+-------+------------+
| id   | type   | state | db         |
+------+--------+-------+------------+
|    0 | server | 0     |            |
|    1 | proxy  | 0     |            |
|    2 | proxy  | 10    | replaction |
|    3 | server | 10    |            |
+------+--------+-------+------------+
4 rows in set (0.02 sec)

mysql> select * from proxy_config;
+----------------------------+------------------+
| option                     | value            |
+----------------------------+------------------+
| admin.address              | :4041            |
| proxy.address              | 192.168.1.10:3306 |
| proxy.lua_script           | NULL             |
| proxy.backend_addresses[0] | 192.168.1.231:3306   |
| proxy.fix_bug_25371        | 0                |
| proxy.profiling            | 1                |
+----------------------------+------------------+
6 rows in set (0.02 sec)
基本的运行信息都包含了。

发表评论

个人介绍

Xu
Evangelist
Oracle DBA / Blogger
Life@Beijing
Work@恩世教育
详细介绍...

按月归档

Close