1、理论部分
1.1、MySQL Proxy的简介
MySQL Proxy是一个中间层代理(连接池),负责将前台应用连接请求转发给后台数据库,并通过lua脚本,可以实现负载的连接控制过滤,从而实现读写分离和负载均衡。对于应用来说,MySQL Proxy是完全透明的,应用则只需要连接到MySQL Proxy的监听端口。
1.2、常见用途
1)负载均衡
2)读写分离
3)故障、查询分析
4)查询过滤和修改
1.3、官方下载
http://www.mysql.com/
2、实验部分
2.1、主机信息
Master
ipadddress=10.168.0.103
hostname=Master
Slave
ipaddress=10.168.0.104
hostname=Slave
MySQLProxy
ipaddress=10.168.0.111
hostname=MySQLProxy
Client
ipaddress=10.168.0.8
hostname=client
2.2、实验前提
完成主备的配置,详细文档如下:
http://cmdschool.blog.51cto.com/2420395/1696474
2.3、yum源安装
In MySQLProxy
yum -y install lua lsof
In Client
yum -y install mysql
2.3、实验步骤
2.3.1、安装MySQL-Proxy
In MySQLProxy
wget http://dev.mysql.com/get/Downloads/MySQL-Proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz tar xvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local cd /usr/local/ ln -s mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit/ mysql-proxy
vim编辑/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
min_idle_connections = 1, max_idle_connections = 1,
注意:以上生产环境可以不修改
2.3.2、启动MySQL-Proxy
In MySQLProxy
1)先尝试检查端口与获取帮助
lsof -i TCP:3306 /usr/local/mysql-proxy/bin/mysql-proxy --help-proxy
显示如下:
Usage: mysql-proxy [OPTION...] - MySQL Proxy proxy-module -P, --proxy-address=<host:port> listening address:port of the proxy-server (default: :4040) -r, --proxy-read-only-backend-addresses=<host:port> address:port of the remote slave-server (default: not set) -b, --proxy-backend-addresses=<host:port> address:port of the remote backend-servers (default: 127.0.0.1:3306) --proxy-skip-profiling disables profiling of queries (default: enabled) --proxy-fix-bug-25371 fix bug #25371 (mysqld > 5.1.12) for older libmysql versions -s, --proxy-lua-script=<file> filename of the lua script (default: not set) --no-proxy don't start the proxy-module (default: enabled) --proxy-pool-no-change-user don't use CHANGE_USER to reset the connection coming from the pool (default: enabled) --proxy-connect-timeout connect timeout in seconds (default: 2.0 seconds) --proxy-read-timeout read timeout in seconds (default: 8 hours) --proxy-write-timeout write timeout in seconds (default: 8 hours)
如果你看不懂英文,大致意思如下:
-P 指定Proxy服务器工作的地址和端口 -b 指定写服务器的地址和端口 -r 指定读服务器的地址和端口 -s 指定判断的脚本 --daemon 以后台进程的方式启动
2)启动MySQL-Proxy
/usr/local/mysql-proxy/bin/mysql-proxy \ -P 10.168.0.111 \ -b 10.168.0.103:3306 \ -r 10.168.0.104:3306 \ --daemon \ -s /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua&
检查端口是否在监听
lsof -i TCP:3306
2.3.3、设置防火墙
In MySQLProxy
vim编辑/etc/sysconfig/iptables并加入如下内容:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
重启防火墙:
/etc/init.d/iptables restart
2.3.4、授权访问
In Master
授权代理可以访问真实数据库:
mysql> grant all on cmdschool.* to [email protected] identified by'prxp'; mysql> flush privileges;
In MySQLProxy
代理端尝试连接真实数据库:
mysql -h10.168.0.103 -prxu -prxp
2.3.5、检查与测试
In Master
创建测试数据库与测试表:
mysql> create database cmdschool; mysql> create table t1(id varchar(255));
In Client
客户端连接代理作查询、插入数据、查询对比前后结果:
mysql -h10.168.0.111 -uprxu -pprxp mysql> select * from cmdschool.t1; mysql> insert into cmdschool.t1 values ( "1" ); mysql> select * from cmdschool.t1;
In Slave
停止从服务器的复制功能:
mysql -p mysql> stop slave;
In Client
插入第二条测试数据,并尝试查询:
mysql> insert into cmdschool.t1 values ( "2" ); mysql> select * from cmdschool.t1;
In Master
查询主库对比代理查询的结果:
mysql> select * from cmdschool.t1;
In Slave
查询从库对比代理查询的结果,查询完毕后重新启动从复制:
mysql> select * from cmdschool.t1; mysql> start slave;
In Client
通过代理插入第三条测试数据:
mysql> insert into cmdschool.t1 values ( "3" ); mysql> select * from cmdschool.t1;
In Master
查询主库的结果对比代理的查询结果:
mysql> select * from cmdschool.t1;
In Slave
查询从库的结果对比代理的查询结果:
mysql> select * from cmdschool.t1;
2.3.6、优化和自启动
In MySQLProxy
1)先检查文件打开数
ulimit -a
2)调整文件打开数
ulimit -n 10240
vim编辑/etc/rc.d/rc.local并加入如下内容:
ulimit -n 10240 /usr/local/mysql-proxy/bin/mysql-proxy \ -P 10.168.0.111 \ -b 10.168.0.103:3306 \ -r 10.168.0.104:3306 \ --daemon
参考文档:
1)官方参考资料
http://dev.mysql.com/doc/mysql-proxy/en/
安装MySQL-Proxy
http://dev.mysql.com/doc/mysql-proxy/en/mysql-proxy-install-binary.html
2)其他参考资料
http://www.linuxidc.com/Linux/2011-03/33425.htm
http://www.linuxidc.com/Linux/2014-01/95056.htm
http://www.it165.net/database/html/201404/5892.html
3)官方下载网址
http://dev.mysql.com/downloads/mysql-proxy/