什么是mysql_proxy? mysql_proxy是一个简单的位于客户端和mysql服务器程序,它可以监测分析改变起通信,灵活行,允许很多用途,常使用的:负载均衡;备援;查询分析;查询过滤等等。
下面我将要使用mysql_proxy来实现读写分离.首先我这篇文章是结合上一篇mysql master-slave 同步来写的,清大家一定要结合上篇文章。
linux OS : ubuntu 8.04.1
software : mysql-server libmysqlclient15-dev libmysqlclient15off lua libeventdb-dev
mysql1 :192.168.6.4 //
mysql2 :192.168.6.5 // 接上篇文章的两台 mysql服务器
mysql_proxy : 192.168.6.3
在 mysql1 于mysql2上执行
建立一个空的数据库 test;
建立一个空表
create table proxy(id int(5),name
char(10));
1.在mysql-proxy服务器上安装 mysql-proxy
apt-get install libmysqlclient15-dev libmysqlclient15off libeventdb-dev
lua5.1
tar zxvf mysql-proxy-0.6.1.tar.gz
cd mysql-proxy-0.6.1
./configure --prefix=/usr/local/mysql-proxy
make
make install
/usr/local/mysql-proxy/sbin/mysql-proxy --help-all
Usage:
mysql-proxy [OPTION...] - MySQL Proxy
Help Options:
-?, --help Show help options
--help-all Show all help options
--help-admin Show options for the admin-module
--help-proxy Show options for the proxy-module
admin module
--admin-address= listening address:port of internal admin-server (default: :4041)
proxy-module
--proxy-address= listening address:port of the proxy-server (default: :4040)
--proxy-read-only-backend-addresses= address:port of the remote slave-server (default: not set)
--proxy-backend-addresses= 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
--proxy-lua-script= filename of the lua script (default: not set)
--no-proxy Don't start proxy-server
Application Options:
-V, --version Show version
--daemon Start in daemon-mode
--pid-file= PID file in case we are started as daemon
--admin-address= 管理地址 内部管理服务器默认端口 4041
--proxy-address= 代理地址 端口代理服务器 默认端口4040
--proxy-read-only-backend-addresses= 代理只读后端服务器地址
--proxy-backend-addresses= 代理后断服务器地址 远程后断服务器默认 127.0.0.1:3306
--proxy-skip-profiling 还不清楚什么意思~
-proxy-fix-bug-25371 应该是修复了漏洞吧,解释是这样的。
--proxy-lua-script= proxy-lua脚本 默认是没有设置的
--no-proxy 没有代理不启动代理服务器
-V, --version 显示 mysql-proxy版本
--daemon 以守护进程启动模式
--pid-file= daemon 启动模式的 pid文件
mysql-proxy 启动,这里我用的一个CU 朋友写的脚本,期间有些改动也是这位朋友告诉我几个要改的地方。再次感谢 KDr2 这个脚本我会上传开放给大家
/usr/loca/mysql-proxy/sbin/mysql-proxy --proxy-read-only-backend-addresses=192.168.6.5:3306 --proxy-backend-addresses=192.168.6.4:3306 --proxy-lua-script=/usr/local/mysql-proxy/mysql.lua &
netstat -ant
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN
启动成功
可以远程连接了,在一台linux主机上连接,进行一些读写操作
mysql -uroot -p -P4040 -h192.168.6.3
ludy@ludy:~$ mysql -uroot -p -P4040 -h192.168.6.3
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection> Server version: 5.0.51a-3ubuntu5.4-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use test;
Database changed
mysql> insert into proxy(name) values("10");
Query OK, 1 row affected (0.01 sec)
mysql>select * from proxy;
Empty set (0.00 sec)
我们插入了数据没有结果~这就对了读写分离了~~~~
接下来我们进入 mysql1 192.168.6.4看看
ludy@ludy:~$ mysql -uroot -p -h192.168.6.4
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection> Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from proxy;
+------+------+