Simple database load balancing with MySQL Proxy

MySQL的Proxytransparently passes information between a client and aMySQL的服务器。代理可以审核在两个方向上的信息流,并在必要时改变它,这可能是为了保护MySQL服务器免受恶意查询或改变信息接收的客户实际上不更改对数据库有用。代理还可以做负载的MySQL服务器之间的平衡,并通过引导进行流量优化选择statements to read-only slave servers, which enhances MySQL scalability by allowing you to add more servers for read operations.

在许多Linux软件包管理MySQL的代理包可以在名称mysql的代理被发现。在CentOSthe package is available from theEPEL repository。EPEL provides many additional packages that are not available from the main CentOS repository. If you don't have the EPEL repository installed, in CentOS 6 you can install it with the commandrpm -ivh http://ftp-stud.hs-esslingen.de/pub/epel/6/i386/epel-release-6-8.noarch.rpm。一旦你添加EPEL软件库,你可以用以下命令来安装MySQL代理百胜安装mysql代理,然后确保它启动和运行的命令与系统一起自动停止关于chkconfig的MySQL的代理

组态

Unfortunately, MySQL Proxy and its CentOS package are not well documented. It requires some ingenuity to configure it and get started. Here are some tips to aid you.

The configuration file for MySQL Proxy is /etc/sysconfig/mysql-proxy, as you can confirm with the commandrpm -qc mysql-proxy,where the argumentqstands for query andC为配置文件。当你不能确定包的配置文件,您可以随时使用CentOS上此命令。

在side the /etc/sysconfig/mysql-proxy file you can set the following options:

  • ADMIN_USER – the user for the proxy's admin interface. You can leave the default admin user.
  • ADMIN_PASSWORD - 在明文管理用户的密码。为更好的安全性更改默认密码。
  • ADMIN_LUA_SCRIPT - 中的Lua编程语言中的管理脚本。如果没有这个脚本管理界面无法正常工作。您可以保留默认值。
  • PROXY_USER – the system user under which the proxy will work. By default it is mysql-proxy, and it's safe to leave it as is.
  • PROXY_OPTIONS - 代理选项,如日志记录级别,插件和Lua脚本加载。

The most important configuration directive is the PROXY_OPTIONS. A good example for it looks like:

PROXY_OPTIONS="--daemon --log-level=info --log-use-syslog --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.1.102:3306 --proxy-read-only-backend-addresses=192.168.1.105:3306 --proxy-lua-script=http://www.openlogic.com/usr/lib/mysql-proxy/lua/proxy/rw-splitting.lua"

With these settings, logging is set to the info level (--log-level=info)通过系统的syslog(--log-use-syslog), which means all system messages from the proxy go to the file /var/log/messages.

Two plugins are to be used – proxy (--plugins=proxy),其提供核心代理功能,和管理员(--plugins=admin), which gives users an admin interface with useful information about the back-end servers, as we will see later.

The backend servers are specified – one read/write (--proxy-backend-addresses=192.168.1.102:3306)和一个只用于阅读,也就是只有SELECT语句(--proxy-read-only-backend-addresses=192.168.1.105:3306)。The read-only servers should be replicated from the master read/write server. You can specify more read and write servers according to your MySQL replication design, and all queries will be evenly distributed using a round-robin algorithm. This is useful for load balancing and failover because the proxy will not forward queries to a failed server.

The last setting is a Lua script for splitting queries into reads and writes (--proxy-LUA脚本= HTTP://www.openlogic.com/usr/lib/mysql-proxy/lua/proxy/rw-splitting.lua)。这是对MySQL代理的最有用的功能之一。它允许卸载主的MySQL服务器和转发SELECT语句优化换读取从属服务器。

这Lua的默认脚本不包含在EPEL包。为了得到它,你必须下载正式MySQL代理包。From the download options choose the generic Linux archive, which is currently called mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz. Once you extract this package you can find the rw-splitting.lua script in the newly extracted directory mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy/. (Say that three times fast.) Copy the script from there to /usr/lib/mysql-proxy/lua/proxy/ on the proxy server.

新创建的目录中包含了许多其它例子Lua脚本,你可以玩,并没有充分了解Lua语言甚至使用。大部分脚本的情况下,他们的名字暗示他们的目的。例如,auditing.lua脚本用于审计和教程查询time.lua给你查询的时间。

监控

一旦你完成设置,你可以用以下命令启动MySQL代理mysql proxy startCentOS。你应该在/var/log/messages文件d see output indicating a successful start, such as:

1月14日21时54分08秒的mysql服务器2代理:2013年1月14日21时54分08秒:(消息)MySQL的代理0.8.2开始1月14日21时54分08秒的mysql服务器2代理:2013年1月14日21时54分08秒:(消息)在端口代理听音:4040年1月14 21点54分08秒的MySQL服务器2代理:2013年1月14日21时54分08秒:(消息)加入读/写后端:192.168.1.102:3306年1月14 21点54分08秒的MySQL服务器2代理:2013年1月14日21时54分08秒:(消息)加入只读后端:192.168.1.105:3306

To test the proxy you need to设置MySQL的复制first. Once you have replication working you can import a sample database, such as the

你已经通过代理一些活动后,您可以查看其状态,并开始监测。要做到这一点,使用管理界面,这是由服务器的端口4041上的MySQL客户端访问如果你的MySQL代理有192.168.1.201的IP地址,例如,您可以连接到其管理界面用命令MySQL的主机= 192.168.1.201 --port = 4041 -u管理-p secr3t_pass 。管理员登录ID和密码是在/ etc / SYSCONFIG / MySQL的代理指定的人。

管理界面是简单的,并且通常(取决于lua的管理脚本)只允许命令选择* FROM backends;。On a properly working MySQL Proxy this command should give output such as:

+ ------------- + -------------------- + ------- + ------+ ------ + ------------------- + |backend_ndx |地址|状态|键入|UUID |connected_clients |+ ------------- + -------------------- + ------- + ------+ ------ + ------------------- + |1 |192.168.1.102:3306 | up | rw | NULL | 0 | | 2 | 192.168.1.105:3306 | up | ro | NULL | 0 | +-------------+--------------------+-------+------+------+-------------------+

The above table shows the addresses of the servers, their state, type – read/write (rw) or read-only (ro) –uuid和数字连接的客户端。

您也可以包含在官方档案中的Lua脚本的其余部分发挥。为了测试一个新的脚本,只需将它复制到MySQL代理服务器上的/ usr / lib中/ MySQL的代理/ LUA /代理/目录下,并将其包含在PROXY_OPTIONS指令。

MySQL代理是一个简单而强大的工具。虽然它提供了一些挑战,今天在微薄的文档和粗略的易用性方面,它是在不断发展和演出不断完善。

Join the Network World communities onFacebookandLinkedInto comment on topics that are top of mind.

版权所有©2013Raybet2

IT Salary Survey:结果是