mysql-proxy做客户端连接转发【外网访问内网mysql】

功能
用于外网客户端连接内网的MySQL,将此工具安装在中转服务器上。
软件版本
mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz
简单的配置过程
解压后有5个目录
bin include lib libexec share
进bin目录,查看mysql_proxy的使用帮助
[root@ora01 bin]# ./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=<host:port> listening address:port of the admin-server (default: :4041)
 --admin-username=<string> username to allow to log in
 --admin-password=<string> password to allow to log in
 --admin-lua-script=<filename> script to execute by the admin plugin
 
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)
 
Application Options:
 -V, --version Show version
 --defaults-file=<file> configuration file
 --verbose-shutdown Always log the exit code when shutting down
 --daemon Start in daemon-mode
 --user=<user> Run mysql-proxy as user
 --basedir=<absolute path> Base directory to prepend to relative paths in the config
 --pid-file=<file> PID file in case we are started as daemon
 --plugin-dir=<path> path to the plugins
 --plugins=<name> plugins to load
 --log-level=(error|warning|info|message|debug) log all messages of level ... or higher
 --log-file=<file> log all messages in a file
 --log-use-syslog log all messages to syslog
 --log-backtrace-on-crash try to invoke debugger on crash
 --keepalive try to restart the proxy if it crashed
 --max-open-files maximum number of open files (ulimit -n)
 --event-threads number of event-handling threads (default: 1)
 --lua-path=<...> set the LUA_PATH
 --lua-cpath=<...> set the LUA_CPATH

应用

./mysql-proxy --admin-username='root' --admin-password='12345' --admin-lua-script=/opt/mysql-proxy-0.8.1-linux-rhel5-x86-64bit/lib/mysql-proxy/lua/admin.lua --proxy-backend-addresses=192.168.60.134:3306 --daemon

参数说明:
指定MySQL的用户名和密码、执行mysql-proxy工具的脚本、mysql服务器地址和端口、服务模式
测试

# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 29237/mysql-proxy 
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 29237/mysql-proxy

4040端口是默认的代理端口,客户端连接的时候直接指向此端口
4041是默认的管理模块端口
总结:现在就可以连接中转服务器的4040端口,访问内网的MySQL服务器。除了可以进行连接的转发外,还可以进行查询的监控、过滤、分析、负载均衡(Load Balance)、失败漂移(Failover)等,更多功能以后再研究。