共计 4562 个字符,预计需要花费 12 分钟才能阅读完成。
MySQL 读写分离配置
环境:CentOS7.2 MySQL5.7
场景描述:
数据库 Master 主服务器:192.168.206.100
数据库 Slave 从服务器:192.168.206.200
MySQL-Proxy 调度服务器:192.168.206.210
以下操作,均是在 192.168.206.210 即 MySQL-Proxy 调度服务器 上进行的。
1. 检查系统所需软件包
安装之前需要配置 EPEL YUM 源
wget https://mirrors.ustc.edu.cn/epel//7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
rpm -ivh epel-release-7-11.noarch.rpm
yum clean all
yum update
yum install -y gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig* libevent* glib*
2. 编译安装 lua
MySQL-Proxy 的读写分离主要是通过 rw-splitting.lua 脚本实现的,因此需要安装 lua
lua 可通过以下方式获得
从 http://www.lua.org/download.html 下载源码包
从 rpm.pbone.net 搜索相关的 rpm 包
download.Fedora.RedHat.com/pub/fedora/epel/5/i386/lua-5.1.4-4.el5.i386.rpm
download.fedora.redhat.com/pub/fedora/epel/5/x86_64/lua-5.1.4-4.el5.x86_64.rpm
这里我们建议采用源码包进行安装
cd /opt/install
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zvfx lua-5.1.4.tar.gz
cd lua-5.1.4
make linux
make install
mkdir /usr/lib/pkgconfig/
cp /opt/install/lua-5.1.4/etc/lua.pc /usr/lib/pkgconfig/
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig
注意的问题
编译的时候,遇到的问题是,缺少依赖包 ** readline**, 然后 readline 又依赖 ncurses, 所以要先安装着两个软件
yum install -y readline-devel ncurses-devel
3. 安装配置 MySQL-Proxy
下载 mysql-proxy
下载:http://dev.mysql.com/downloads/mysql-proxy/
wget https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
tar zxvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
mv zxvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy
** 配置 mysql-proxy,创建主配置文件 **
cd /usr/local/mysql-proxy
mkdir lua #创建脚本存放目录
mkdir logs #创建日志目录
cp share/doc/mysql-proxy/rw-splitting.lua ./lua #复制读写分离配置文件
vi /etc/mysql-proxy.cnf #创建配置文件
[mysql-proxy]
user=root #运行 mysql-proxy 用户
admin-username=proxyuser #主从 mysql 共有的用户
admin-password=123456 #用户的密码
proxy-address=192.168.206.210:4040 #mysql-proxy 运行 ip 和端口,不加端口,默认 4040
proxy-read-only-backend-addresses=192.168.206.200 #指定后端从 slave 读取数据
proxy-backend-addresses=192.168.206.100 #指定后端主 master 写入数据
proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua #指定读写分离配置文件位置
admin-lua-script=/usr/local/mysql-proxy/lua/admin.lua #指定管理脚本
log-file=/var/log/mysql-proxy.log #日志位置
log-level=info #定义 log 日志级别
daemon=true# 以守护进程方式运行
keepalive=true #mysql-proxy 崩溃时,尝试重启
这里就有一个坑了
配置文件里面的注释要全部删掉,不然可能会引起一些字符不能识别的错误。
这还不是最坑的,最坑的是:即使删掉注释,去除多余的空白字符,仍然可能会报如下错误:
2018-09-21 06:39:40: (critical) Key file contains key“daemon”which has a value that cannot be interpreted.
或者:
2018-09-21 06:52:22: (critical) Key file contains key“keepalive”which has a value that cannot be interpreted.
出现以上问题的原因是 daemon=true,keepalive=true 现在不这样写了,要改为:
daemon=1
keepalive=1
给配置文件执行权限
chmod 660 /etc/mysql-porxy.cnf
配置 admin.lua 文件
在 /etc/mysql-proxy.cnf 配置文件中,还差 /usr/local/mysql-proxy/lua/admin.lua 的管理文件,实际现在还没有创建的。所以,现在需要编辑创建 admin.lua 文件。mysql-proxy-0.8.5 的这个版本,我找到了下面的 admin.lua 脚本,对这个版本才是有效的:
vim /usr/local/mysql-proxy/lua/admin.lua
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
** 修改读写分离配置文件 **
vim /usr/local/mysql-proxy/lua/rw-splitting.luaif not proxy.global.config.rwsplit
proxy.global.config.rwsplit = {
min_idle_connections = 1, #默认超过 4 个连接数时,才开始读写分离,改为 1
max_idle_connections = 1, #默认 8,改为 1
is_debug = false
}
end
启动 mysql-proxy
/usr/local/mysql-proxy/bin/mysql-proxy –defaults-file=/etc/mysql-proxy.cnf
netstat -tupln | grep 4000 #已经启动 killall -9 mysql-proxy #关闭 mysql-proxy
: