首页 > 数据库 > MySQL Proxy在window下的配置及使用

MySQL Proxy在window下的配置及使用

2009年6月13日 发表评论 阅读评论

1,原本为Cilent-Server直连这种拓扑,现在引入MySQL Proxy,变成Client-Proxy-Server。
2,原本为了实现高容载能力,对于Server采取了多台并存,Master/Slave甚至是Master/Master等方式的复制集群,配置管理都比较复杂。而引入了Proxy以后,中间存在了这样一个Store-Forward的proxy环节,对于程序开发完全透明。
3,在这个环节,我们可以通过一种脚本语言来控制这个proxy的行为,例如对于Client进来的查询依照某种条件过滤,甚至依照某种条件改写,再导向后端的Server。
4,凭借自己实现的filtering或rewriting,我们可以实现很多目的,例如Failover,例如Load balance。或者更无聊些我们可以对Client进来的带有弱智语法错误的SQL语句进行修正。

mysql-proxy可以做读写分离、负载均衡、故障转移。
1、下载windows的包,解压后,直接运行就可以,并且也支持lua脚本,太方便了;
2、命令行运行的参数:
–help-all — show all help options.
–help-admin — show options for the admin-module.
–help-proxy — Show options for the proxy-module.
–admin-address=host:port — specify the host name (or IP address) and port for the administration port.
The default is localhost:4041.
–proxy-address=host:port — the listening host name (or IP address) and port of the proxy server.
The default is localhost:4040.
–proxy-read-only-backend-address=host:port — the listening host name (or IP address) and
port of the proxy server for read-only connections. The default is for this information not to be set.
–proxy-backend-addresses=host:port — the host name (or IP address) and port of the MySQL server to connect to.
You can specify multiple backend servers by supplying multiple options.
Clients are connected to each backend server in round-robin fashion.
For example, if you specify two servers A and B, the first client connection will go to server A;
the second client connection to server B and the third client connection to server A.
–proxy-skip-profiling — disables profiling of queries (tracking time statistics). The default is for tracking to be enabled.
–proxy-fix-bug-25371 — gets round an issue when connecting to a MySQL server later than 5.1.12
when using a MySQL client library of any earlier version.
–proxy-lua-script=file — specify the Lua script file to be loaded.
Note that the script file is not physically loaded and parsed until a connection is made.
Also note that the specified Lua script is reloaded for each connection;
if the content of the Lua script changes while mysql-proxy is running then the updated content will automatically
be used when a new connection is made.
–daemon — starts the proxy in daemon mode.
–pid-file=file — sets the name of the file to be used to store the process ID.
–version — show the version number.

3、典型配置案例:
1、最基础的应用,代理单个数据库服务器,这样就可以通过4040端口访问mysql数据库。
mysql-proxy –proxy-backend-addresses=192.168.18.110:3306
2、连接多个数据库服务器,假如A and B. 第一个客户端连接到 A,第二个连接到 B。
如果有一台停止服务时,代理会自动检测到,有新连接时会自动连接到一台正常的数据库。
mysql-proxy \
–proxy-backend-addresses=narcissus:3306 \
–proxy-backend-addresses=nostromo:3306
3、数据库读写分离,192.168.18.110负责写入,192.168.18.107负责读取数据,当然也可以再增加读取数据的服务器。
mysql-proxy \
–proxy-backend-addresses=192.168.18.110:3306 \
–proxy-read-only-backend-addresses=192.168.18.107:3306
这种方式常被理解为可以读写分离了,但是实际不是的。mysql-proxy不能区分哪些是发往从服务器的,还需要自己用脚本控制,见第四种方式。
4、 Lua 脚本能很好的控制连接和分布, 以及查询及返回的结果集.
使用Lua脚本时,必须使用 –proxy-lua-script 指定脚本的名称。
直到产生连接的时候才会读取脚本,也就是修改脚本后不用重新启动服务。
mysql-proxy –proxy-lua-script=rw-splitting.lua –proxy-backend-addresses=192.168.18.110:3306 –proxy-read-only-backend-addresses=192.168.18.107:3306
注意的问题:
1、proxy的读写分离机制是先把最初的几条查询发到master上建立连接,
当发送到master上的查询数超过连接池的最小值时开始把查询
2、LAST_INSERT_ID不能发送到主服务器上, 226 行修改为下面的就可以了
elseif not is_insert_id and token.token_name == “TK_FUNCTION” then
3、使用默认的rw-splitting.lua时,会提示找不到proxy-command,我把mysql-proxy的路径设置为系统路径,然后在share目录下运行就一切Ok了,在运行中输入cmd,然后cd C:\tools\mysql-proxy\share。
4、字符乱码
通过proxy连上数据库之后,查到的字符串始终是乱码,即便手工执行了set names ‘utf8’也没有效果。
解决办法,mysql server必须设置
1.[mysqld]
2.skip-character-set-client-handshake
3. init-connect=’SET NAMES utf8’
4. default-character-set=utf8

分类: 数据库 标签: 14,299 次阅读
原文链接:http://www.wenhq.com/article/view_325.html
欢迎转载,请注明出处:亲亲宝宝
  1. 小朱
    2013年5月1日01:25 | #1

    请问楼主,我在Windows系统中安装了MySQL,想通过MySQL-proxy代理来访问我的MySQL数据库,但是配置-admin-username,和–proxy-backend-address这些的时候不成功,楼主有没有详细的配置过程?

    [回复]

  1. 本文目前尚无任何 trackbacks 和 pingbacks.