在CentOS 5(FINAL)x86_64上安装MySQL代理
本教程将介绍如何在CentOS 5(x86_64)系统上安装MySQL代理 。 MySQL代理是一个简单的程序,位于您的客户端和MySQL服务器之间,可以监视,分析或转换其通信。 其灵活性允许无限使用; 常见的包括:负载平衡; 故障切换 查询分析; 查询过滤和修改; 还有很多。
在新鲜的Minium Centos 5最后x86_64安装:
yum install gcc.x86_64 libevent.x86_64 libevent-devel.x86_64 readline.x86_64 readline-devel.x86_64 ncurses.x86_64 ncurses-devel.x86_64 glib2.x86_64 glib2-devel.x86_64
cd /usr/local/src/
wget http://www.lua.org/ftp/lua-5.1.3.tar.gz
tar zxvf lua-5.1.3.tar.gz
cd lua-5.1.3
make linux
make install
wget http://dev.mysql.com/get/Downloads/MySQL-Cluster-6.2/mysql-5.1.23-ndb-6.2.15-linux-x86_64-glibc23.tar.gz/\
from/http://www.mirrorservice.org/sites/ftp.mysql.com/
tar xzvf mysql-5.1.23-ndb-6.2.15-linux-x86_64-glibc23.tar.gz
ln -s mysql-5.1.23-ndb-6.2.15-linux-x86_64-glibc23 mysql
PATH=$PATH:/usr/local/mysql/bin
export PATH
编辑您的.profile
以使其成为永久性:
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:/usr/local/mysql/bin:$HOME/bin export PATH unset USERNAME
wget http://dev.mysql.com/get/Downloads/MySQL-Proxy/mysql-proxy-0.6.1.tar.gz/from/http://www.mirrorservice.org/sites/ftp.mysql.com/
tar zxvf mysql-proxy-0.6.1.tar.gz
cd mysql-proxy-0.6.1
./configure LDFLAGS="-lm -ldl" LUA_CFLAGS="-I/usr/local/include/" LUA_LIBS=/usr/local/lib/liblua.a
make
make install
我们创建一个示例LUA脚本,以便您可以看到一些日志。
mkdir /var/log/mysql-proxy/
mkdir -p /usr/local/mysql/lua-scripts/
vi /usr/local/mysql/lua-scripts/simple-log.lua
(见: http : //www.oreillynet.com/pub/a/databases/2007/07/12/getting-started-with-mysql-proxy.html?page=3
修改脚本以获取IP并使用proxy.connection.server.thread_id
。)
local log_file = '/var/log/mysql-proxy/mysql.log' local fh = io.open(log_file, "a+") function read_query( packet ) if string.byte(packet) == proxy.COM_QUERY then local query = string.sub(packet, 2) fh:write( string.format("%s %6d -- %s :IP %s :USER: %s\n", os.date('%Y-%m-%d %H:%M:%S'), proxy.connection.server.thread_id, query, proxy.connection.client.address, proxy.connection.client.username)) fh:flush() end end
现在使用变量-proxy-backend-addresses
启动代理,将代理指向服务器。
/usr/local/sbin/mysql-proxy --proxy-lua-script=/usr/local/mysql/lua-scripts/simple-log.lua --proxy-backend-addresses=192.168.1.33:3306 --proxy-backend-addresses=192.168.1.34:3306 --daemon
192.168.1.33
和192.168.1.34
是代理将连接到的MySQL节点。
通过防火墙允许代理连接:
### ALLOWED TO CONNECT TO MYSQL PROXY ### ### LOCAL ADMINS -A INPUT -s SRC-IP -d DST-IP -p tcp -m state --state NEW -m tcp --dport 4040 -j ACCEPT
其中DST-IP
是我的代理服务器, SRC-IP
是我的本地框(客户机)。
现在从您的本地框(不是mysql代理服务器)尝试通过代理连接到后端数据库(具有相关权限的用户必须存在于db中)。
mysql -u dba_admin -p -h PROXY-SERVER -P 4040
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16 to server version: 5.1.23-ndb-6.2.15
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Imap_Forms |
| mysql |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql> quit
Bye
NB代理使用端口4040
而不是3306
。
从mysql-proxy服务器测试mysql-proxy管理界面:
mysql -u root -p -h 127.0.0.1 -P 4041
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.20-agent MySQL Enterprise Agent
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select * from proxy_connections;
+------+--------+-------+------+
| id | type | state | db |
+------+--------+-------+------+
| 0 | server | 0 | |
| 1 | proxy | 0 | |
| 2 | server | 10 | |
+------+--------+-------+------+
3 rows in set (0.00 sec)
mysql>quit
bye
任务完成! 现在读:
http://dev.mysql.com/tech-resources/articles/proxy-gettingstarted.html
http://forge.mysql.com/wiki/MySQL_Proxy
http://www.oreillynet.com/pub/a/databases/2007/07/12/getting-started-with-mysql-proxy.html?page=1