您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
mysql-proxy.0.8.3实现MySQL-5.6读写分离
发布时间:2019-06-16 18:14:34编辑:雪饮阅读()
上次实现了mysql5.6的主从复制及半同步。这次在上次的基础上继续实现mysql5.6的读写分离。
实现读写分离的原理是在主从复制的基础上再加上一个节点,该节点与master和slave没有直接关系,仅仅做为类似负载均衡的前端负载均衡器一样的作用,过滤用户sql语句,当是查询操作时候自动用slave服务器给用户返回结果,但是写入操作是就以master服务器给用户返回结果。
使用mysql-proxy0.8.3配置读写分离的代理服务器
解压安装
软件包:mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit.tar.gz
[root@localhost src]# ln -sv /usr/local/src/mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit /usr/local/mysql-proxy
`/usr/local/mysql-proxy' -> `/usr/local/src/mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit'
在master上准备一个用于proxy连接管理的代理账号
该账号也同时是对使用proxy的用户提供的,如使用此代理账号在对mysql性能要求比较高的应用中。
mysql> grant all on *.* to 'root'@'192.168.2.%' identified by 'xy220807';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
准备代理管理的lua脚本
[root@localhost src]# cat /usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua
--[[ $%BEGINLICENSE%$
Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; version 2 of the
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
$%ENDLICENSE%$ --]]
function set_error(errmsg)
proxy.response = {
type = proxy.MYSQLD_PACKET_ERR,
errmsg = errmsg or "error"
}
end
function read_query(packet)
if packet:byte() ~= proxy.COM_QUERY then
set_error("[admin] we only handle text-based queries (COM_QUERY)")
return proxy.PROXY_SEND_RESULT
end
local query = packet:sub(2)
local rows = { }
local fields = { }
if query:lower() == "select * from backends" then
fields = {
{ name = "backend_ndx",
type = proxy.MYSQL_TYPE_LONG },
{ name = "address",
type = proxy.MYSQL_TYPE_STRING },
{ name = "state",
type = proxy.MYSQL_TYPE_STRING },
{ name = "type",
type = proxy.MYSQL_TYPE_STRING },
{ name = "uuid",
type = proxy.MYSQL_TYPE_STRING },
{ name = "connected_clients",
type = proxy.MYSQL_TYPE_LONG },
}
for i = 1, #proxy.global.backends do
local states = {
"unknown",
"up",
"down"
}
local types = {
"unknown",
"rw",
"ro"
}
local b = proxy.global.backends[i]
rows[#rows + 1] = {
i,
b.dst.name, -- configured backend address
states[b.state + 1], -- the C-id is pushed down starting at 0
types[b.type + 1], -- the C-id is pushed down starting at 0
b.uuid, -- the MySQL Server's UUID if it is managed
b.connected_clients -- currently connected clients
}
end
elseif query:lower() == "select * from help" then
fields = {
{ name = "command",
type = proxy.MYSQL_TYPE_STRING },
{ name = "description",
type = proxy.MYSQL_TYPE_STRING },
}
rows[#rows + 1] = { "SELECT * FROM help", "shows this help" }
rows[#rows + 1] = { "SELECT * FROM backends", "lists the backends and their state" }
else
set_error("use 'SELECT * FROM help' to see the supported commands")
return proxy.PROXY_SEND_RESULT
end
proxy.response = {
type = proxy.MYSQLD_PACKET_OK,
resultset = {
fields = fields,
rows = rows
}
}
return proxy.PROXY_SEND_RESULT
end
启动代理
[root@localhost src]# /usr/local/mysql-proxy/bin/mysql-proxy --daemon --log-level=debug --log-file=/var/log/mysql-proxy.log --plugins='proxy' --proxy-backend-addresses='192.168.2.155' --proxy-read-only-backend-addresses='192.168.2.216' --proxy-lua-script='/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua' --plugins=admin --admin-username='admin' --admin-password='admin' --admin-lua-script='/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua'
参数解析:
--log-file:指定代理日志路径
--proxy-backend-addresses:指定master服务器
--proxy-read-only-backend-addresses:指定slave服务器
--proxy-lua-script:指定读写分离lua脚本
--admin-lua-script:指定代理管理所用lua脚本
--plugins=admin --admin-username='admin' --admin-password='admin' 配置代理管理的用户名和密码
管理代理服务器
登录代理服务器
这里另外再找一个服务器安装上mysql客户端,然后登录mysql代理服务器如
[root@localhost ~]# mysql -h 192.168.2.122 -u admin --port=4041 -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
查看该代理服务器所代理的主从集群情况
MySQL [(none)]> select * from backends;
+-------------+--------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+--------------------+---------+------+------+-------------------+
| 1 | 192.168.2.155:3306 | unknown | rw | NULL | 0 |
| 2 | 192.168.2.216:3306 | unknown | ro | NULL | 0 |
+-------------+--------------------+---------+------+------+-------------------+
2 rows in set (0.01 sec)
解决unknown问题
其实unknown是因为当前没有任何sql经过该代理服务器执行过,所以就没有初始化状态。
那么我们再开一会话通过该代理服务器创建一个数据库
[root@localhost ~]# mysql -h 192.168.2.122 -u root --port=4040 -e 'create database xy220807' -p;
Enter password:
注意:这里所用到的账号就是上面我们再master中所提供的给该代理服务器可用的mysql账号,另外这里的端口默认是4040,代理登录端口是4041,别搞混了,这里的4040将来是给真实用户用的,以后可以修改为3306
此时我们重新查看主从集群情况
MySQL [(none)]> select * from backends;
+-------------+--------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+--------------------+---------+------+------+-------------------+
| 1 | 192.168.2.155:3306 | up | rw | NULL | 0 |
| 2 | 192.168.2.216:3306 | unknown | ro | NULL | 0 |
+-------------+--------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
发现现在仅剩下一个从服务器的状态没有初始化
那么我们如法炮制,我们同样开一个新会话并执行一个查询操作
[root@localhost ~]# mysql -h 192.168.2.122 -u root --port=4040 -e 'use mysql;select user,host from user' -p;
Enter password:
+----------+-----------------------+
| user | host |
+----------+-----------------------+
| root | 127.0.0.1 |
| repluser | 192.168.2.% |
| root | 192.168.2.% |
| root | ::1 |
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+----------+-----------------------+
我们再来查看主从集群状态
MySQL [(none)]> select * from backends;
+-------------+--------------------+-------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+--------------------+-------+------+------+-------------------+
| 1 | 192.168.2.155:3306 | up | rw | NULL | 0 |
| 2 | 192.168.2.216:3306 | up | ro | NULL | 0 |
+-------------+--------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)
发现这里这次都有状态值了,注意查询的时候这个状态值有延迟(也可能是缓存),多查询几次后或多等一会儿重新查看集群状态才会把状态更新,我查看代理日志后更倾向于是延迟问题,有一个sync的日志,应该是同步状态数据还在进行中所以状态值就没有直接改变。
至此读写分离就搞定了。
启动命令精简(加入系统服务)
上面我们发现代理启动命令太冗长了,这可不行,每次使用都挺麻烦。
先杀死之前的启动进程
killall mysql-proxy
配置环境变量
[root@localhost src]# cat /etc/profile.d/mysql-proxy.sh
export PATH=$PATH:/usr/local/mysql-proxy/bin
[root@localhost src]# . /etc/profile.d/mysql-proxy.sh
mysql-proxy服务脚本文件
[root@localhost src]# cat /etc/rc.d/init.d/mysql-proxy
#!/bin/bash
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
#
# chkconfig: - 78 30
# processname: mysql-proxy
# description: mysql-proxy is a proxy daemon for mysql
# Source function library.
. /etc/rc.d/init.d/functions
prog="/usr/local/mysql-proxy/bin/mysql-proxy"
# Source networking configuration.
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
fi
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
# Set default mysql-proxy configuration.
ADMIN_USER="admin"
ADMIN_PASSWD="admin"
ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
PROXY_OPTIONS="--daemon"
PROXY_PID=/var/run/mysql-proxy.pid
PROXY_USER="mysql-proxy"
# Source mysql-proxy configuration.
if [ -f /etc/sysconfig/mysql-proxy ]; then
. /etc/sysconfig/mysql-proxy
fi
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon $prog $PROXY_OPTIONS --pid-file=$PROXY_PID --proxy-address="$PROXY_ADDRESS" --user=$PROXY_USER --admin-username="$ADMIN_USER" --admin-lua-script="$ADMIN_LUA_SCRIPT" --admin-password="$ADMIN_PASSWORD"
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
touch /var/lock/subsys/mysql-proxy
fi
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $PROXY_PID -d 3 $prog
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/mysql-proxy
rm -f $PROXY_PID
fi
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart|try-restart)
if status -p $PROXY_PIDFILE $prog >&/dev/null; then
stop
start
fi
;;
status)
status -p $PROXY_PID $prog
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|condrestart|try-restart}"
RETVAL=1
;;
esac
exit $RETVAL
[root@localhost src]# chmod +x /etc/rc.d/init.d/mysql-proxy
[root@localhost src]# chkconfig --add mysql-proxy
服务脚本配置文件
[root@localhost src]# cat /etc/sysconfig/mysql-proxy
# Options for mysql-proxy
ADMIN_USER="admin"
ADMIN_PASSWORD="admin"
ADMIN_ADDRESS=""
ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
PROXY_ADDRESS="0.0.0.0:3306"
PROXY_USER="root"
PROXY_OPTIONS="--daemon --log-level=info --log-file=/var/log/mysql-proxy.log --plugins=proxy --proxy-backend-addresses=192.168.2.155:3306 --proxy-read-only-backend-addresses=192.168.2.216:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua --plugins=admin --admin-username=admin --admin-password=admin --admin-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
注意脚本中PROXY_USER为上面maste给分配的用户,PROXY_ADDRESS则是对外部用户访问提供的3306,让用户感觉就和在真实数据库上一样。
服务启动
那么接下来服务启动就可以直接使用sysv风格了
[root@localhost src]# service mysql-proxy start
启动后代理登录端口地址不变,而对外访问即操作真实集群的端口变成3306了
关键字词:mysql5.6,读写分离,proxy