mysql数据分离(mysql-proxy配置)
1、安装MySQL
2、安装Lua
# wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
# tar xzf lua-5.1.4.tar.gz
# cd lua-5.1.4
# make
# make linux INSTALL_TOP= /usr/local/lua
# make install
3、安装glib
# wget http://ftp.gnome.org/pub/gnome/sources/glib/2.28/glib-2.28.4.tar.bz2
# tar jxf glib-2.28.4.tar.bz2
# cd glib-2.28.4
# ./configure --prefix=/opt/module/glib2
# make && make install
# echo "/opt/module/glib2/lib" >> /etc/ld.so.conf
# ldconfig
4、安装pkg-config
# wget http://pkg-config.freedesktop.org/releases/pkgconfig-0.18.tar.gz
# tar xzf pkgconfig-0.18.tar.gz
# cd pkgconfig-0.18
# ./configure && make && make install
5、安装libevent
# wget http://monkey.org/%7Eprovos/libevent-2.0.10-stable.tar.gz
# tar zxf libevent-2.0.10-stable.tar.gz
# cd libevent-2.0.10-stable
# ./configure --prefix=/opt/module/libevent
# make; make install
6、安装mysql-proxy
# wget http://dev.mysql.com/get/Downloads/MySQL-Proxy/mysql-proxy-0.8.1.tar.gz/from/http://mysql.he.net/
# tar xzf mysql-proxy-0.8.1.tar.gz
# cd mysql-proxy-0.8.1
# export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm"
export PKG_CONFIG_PATH="/usr/lib/pkgconfig/"
export GLIB_CFLAGS="-I/opt/module/glib2/include/glib-2.0 -I/opt/module/glib2/lib/glib-2.0/include" GLIB_LIBS="-L/opt/module/glib2/lib -lglib-2.0"
export GMODULE_CFLAGS="-I/opt/module/glib2/include" GMODULE_LIBS="-L/opt/module/glib2/lib"
export GTHREAD_CFLAGS="-I/opt/module/glib2/include"
export GTHREAD_LIBS="-L/opt/module/glib2/lib"
export CPPFLAGS="$CPPFLAGS -I/opt/module/libevent/include"
export CFLAGS="$CFLAGS -I/opt/module/libevent/include"
export LDFLAGS="$LDFLAGS -L/opt/module/libevent/lib -lm"
export LIBS="/opt/module/glib2/lib/libgthread-2.0.so /opt/module/glib2/lib/libgmodule-2.0.so"
# ./configure --with-mysql="/opt/module/mysql" --prefix="/opt/module/mysql-proxy"
查看是否安装成功:
# /opt/module/mysql-proxy/bin/mysql-proxy --help-all | less
配置:
# vi /etc/mysql-proxy.cnf
===================
[mysql-proxy]
admin-username = mysqlproxyadm 主从数据库都需要的数据库用户
admin-password = 123456
daemon = true
keepalive = true
log-file = /opt/module/mysql-proxy/log
log-level = debug
proxy-backend-addresses = 192.168.1.223:3306 主
proxy-read-only-backend-address = 192.168.1.222:3306 从
proxy-read-only-backend-address = 192.168.1.224:3306 从
proxy-lua-script = /opt/module/mysql-proxy/rw-splitting.lua
admin-lua-script = /opt/module/mysql-proxy/lib/mysql-proxy/lua/admin.lua
===================
# chmod 0660 /etc/mysql-proxy.cnf
制作启动脚本:
# vi /etc/init.d/mysql-proxy
===================
#!/bin/sh
export LUA_PATH=/opt/module/mysql-proxy/?.lua
mode=$1;
if [ -z $mode ] ; then
mode="start"
fi
case $mode in
start)
/opt/module/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf>/opt/module/mysql-proxy/log &
;;
stop)
killall -9 mysql-proxy
;;
*)
echo "Usage: $0 (start|stop)"
exit 1
;;
esac
exit 0;
=====================
# chmod +x /etc/init.d/mysql-proxy
编写mysql-proxy实现读写分离的脚本
# vi /opt/module/mysql-proxy/rw-splitting.lua
============
发送所有的非事务性select到一个从数据库
if is_in_transaction==0 and packet:byte() == proxy.COM_QUERY and packet:sub(2, 7)=="SELECT" then
local max_conns=-1
local max_conns_ndx=0
for i=1, #proxy.servers do
local s=proxy.servers[i] 需要选择一个拥有空闲连接的从数据库
if s.type==proxy.BACKEND_TYPE_RO and s.idling_connections>0 then
if max_conns==-1 or s.connected_clients<MAX_CONNS max_conns_ndx="i" if 至此,找到了一个拥有空闲连接的从数据库 end max_conns="s.connected_clients" then>0 then
proxy.connection.backend_ndx=max_conns_ndx
end
else
发送到主数据库
end
return proxy.PROXY_SEND_QUERY
============
启动mysql-proxy
# /etc/init.d/mysql-proxy start
2、安装Lua
# wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
# tar xzf lua-5.1.4.tar.gz
# cd lua-5.1.4
# make
# make linux INSTALL_TOP= /usr/local/lua
# make install
3、安装glib
# wget http://ftp.gnome.org/pub/gnome/sources/glib/2.28/glib-2.28.4.tar.bz2
# tar jxf glib-2.28.4.tar.bz2
# cd glib-2.28.4
# ./configure --prefix=/opt/module/glib2
# make && make install
# echo "/opt/module/glib2/lib" >> /etc/ld.so.conf
# ldconfig
4、安装pkg-config
# wget http://pkg-config.freedesktop.org/releases/pkgconfig-0.18.tar.gz
# tar xzf pkgconfig-0.18.tar.gz
# cd pkgconfig-0.18
# ./configure && make && make install
5、安装libevent
# wget http://monkey.org/%7Eprovos/libevent-2.0.10-stable.tar.gz
# tar zxf libevent-2.0.10-stable.tar.gz
# cd libevent-2.0.10-stable
# ./configure --prefix=/opt/module/libevent
# make; make install
6、安装mysql-proxy
# wget http://dev.mysql.com/get/Downloads/MySQL-Proxy/mysql-proxy-0.8.1.tar.gz/from/http://mysql.he.net/
# tar xzf mysql-proxy-0.8.1.tar.gz
# cd mysql-proxy-0.8.1
# export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm"
export PKG_CONFIG_PATH="/usr/lib/pkgconfig/"
export GLIB_CFLAGS="-I/opt/module/glib2/include/glib-2.0 -I/opt/module/glib2/lib/glib-2.0/include" GLIB_LIBS="-L/opt/module/glib2/lib -lglib-2.0"
export GMODULE_CFLAGS="-I/opt/module/glib2/include" GMODULE_LIBS="-L/opt/module/glib2/lib"
export GTHREAD_CFLAGS="-I/opt/module/glib2/include"
export GTHREAD_LIBS="-L/opt/module/glib2/lib"
export CPPFLAGS="$CPPFLAGS -I/opt/module/libevent/include"
export CFLAGS="$CFLAGS -I/opt/module/libevent/include"
export LDFLAGS="$LDFLAGS -L/opt/module/libevent/lib -lm"
export LIBS="/opt/module/glib2/lib/libgthread-2.0.so /opt/module/glib2/lib/libgmodule-2.0.so"
# ./configure --with-mysql="/opt/module/mysql" --prefix="/opt/module/mysql-proxy"
查看是否安装成功:
# /opt/module/mysql-proxy/bin/mysql-proxy --help-all | less
配置:
# vi /etc/mysql-proxy.cnf
===================
[mysql-proxy]
admin-username = mysqlproxyadm 主从数据库都需要的数据库用户
admin-password = 123456
daemon = true
keepalive = true
log-file = /opt/module/mysql-proxy/log
log-level = debug
proxy-backend-addresses = 192.168.1.223:3306 主
proxy-read-only-backend-address = 192.168.1.222:3306 从
proxy-read-only-backend-address = 192.168.1.224:3306 从
proxy-lua-script = /opt/module/mysql-proxy/rw-splitting.lua
admin-lua-script = /opt/module/mysql-proxy/lib/mysql-proxy/lua/admin.lua
===================
# chmod 0660 /etc/mysql-proxy.cnf
制作启动脚本:
# vi /etc/init.d/mysql-proxy
===================
#!/bin/sh
export LUA_PATH=/opt/module/mysql-proxy/?.lua
mode=$1;
if [ -z $mode ] ; then
mode="start"
fi
case $mode in
start)
/opt/module/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf>/opt/module/mysql-proxy/log &
;;
stop)
killall -9 mysql-proxy
;;
*)
echo "Usage: $0 (start|stop)"
exit 1
;;
esac
exit 0;
=====================
# chmod +x /etc/init.d/mysql-proxy
编写mysql-proxy实现读写分离的脚本
# vi /opt/module/mysql-proxy/rw-splitting.lua
============
发送所有的非事务性select到一个从数据库
if is_in_transaction==0 and packet:byte() == proxy.COM_QUERY and packet:sub(2, 7)=="SELECT" then
local max_conns=-1
local max_conns_ndx=0
for i=1, #proxy.servers do
local s=proxy.servers[i] 需要选择一个拥有空闲连接的从数据库
if s.type==proxy.BACKEND_TYPE_RO and s.idling_connections>0 then
if max_conns==-1 or s.connected_clients<MAX_CONNS max_conns_ndx="i" if 至此,找到了一个拥有空闲连接的从数据库 end max_conns="s.connected_clients" then>0 then
proxy.connection.backend_ndx=max_conns_ndx
end
else
发送到主数据库
end
return proxy.PROXY_SEND_QUERY
============
启动mysql-proxy
# /etc/init.d/mysql-proxy start
注意:copy虚拟机的虚拟硬盘例如219.vdi,重命名为218.vdi,此时用virtualbox来创建一个虚拟主机,直接连接这个硬盘使用,会提示uuid重复的提示,这是用virtualbox的内部命令可以解决:
C:\Documents and Settings\Administrator>"c:\Program Files\Oracle\VirtualBox"\VBoxManage internalcommands setvdiuuid E:\HardDisks\217.vdi
相关推荐
在这个配置中,`proxy-address` 设置了代理监听的 IP 和端口,`proxy-lua-script` 指定了包含读写分离逻辑的 Lua 脚本路径。`rw-splitting` 部分定义了主服务器的信息,而 `slave1` 和 `slave2` 定义了从服务器的...
例如,在提供的文件中,使用的是`mysql-proxy-0.6.0-linux-rhas4-x86.tar.gz`。 - 解压下载的文件到 `/usr/local/proxymysql` 目录下。 - 将解压后的目录重命名为 `/usr/local/proxymysql`,然后进入该目录的`sbin...
- 配置 `conf/server.yaml` 文件,设置连接MySQL的配置信息,包括数据源、分片规则、端口等。 - 如果需要,配置 `conf/sharding-rule.yaml` 或 `conf/master-slave-rule.yaml` 文件,定义具体的分片规则。 #### ...
--proxy-lua-script=/usr/local/mysql-proxy/sha ``` 其中,`--proxy-backend-addresses` 指定的是主数据库的地址和端口,而 `--proxy-read-only-backend-addresses` 指定的是从数据库的信息。 5. **启动mysql-...
在"mysql-proxy-0.8.5-linux-glibc2.3-x86-32bit"这个压缩包中,包含的是MySQL Proxy的32位版本,适用于基于Linux且采用glibc 2.3库的32位操作系统。安装和配置过程如下: 1. 解压下载的压缩包。 2. 执行编译和安装...
./mysql-proxy --proxy-read-only-backend-addresses=192.168.1.140:3306 --proxy-backend-addresses=192.168.1.14:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua --...
MySQL主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy/Amoeba)来提升数据库的并发负载能力 这样的方案来进行部署与实施的
docker_compose搭建shardingSphereProxyMysql主从读写分离
- **下载与解压**:下载`mysql-proxy-0.8.3-win32-x86.zip`,解压后包含可执行文件`mysql-proxy.exe`,以及相关的配置文件和Lua脚本库。 - **启动Proxy**:通过命令行运行`mysql-proxy.exe`,并指定配置参数,如...
- `proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua`:指定读写分离脚本的位置。 4. **启动MySQL-Proxy**: 启动MySQL-Proxy服务,并验证其是否正常运行。 ```bash /usr/local/mysql-proxy/...
在上述配置中,`--proxy-read-only-backend-addresses`参数指定了两个只读副本的地址,当客户端发起读请求时,MySQL Proxy会将这些请求转发到这些副本。 负载均衡则是通过智能地分配请求到不同的服务器来防止某一台...
确保下载对应操作系统的版本,如文中提到的`mysql-proxy-0.8.5-linux-glibc2.3-x86-32bit`,解压后移动到适当的目录。 2. 创建必要的目录,例如lua脚本目录和日志目录,并将示例的读写分离配置文件`rw-splitting....
Sharding-Proxy 作为数据库的代理层,部署在应用与数据库之间,承担了数据分片、读写分离、分布式事务等核心功能,极大地提升了数据库的处理能力和系统的可扩展性。 **1. 数据分片** 数据分片是 Sharding-Proxy 的...
- `--proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua`:指定 Lua 脚本,该脚本用于实现读写分离功能。 3. **启动 MySQL Proxy**:通过 `/etc/init.d/mysql-proxy` 脚本启动或停止 MySQL Proxy。...
公司运维手册,mysql-proxy读写分离,废话不多说,上干货!
shardingsphere-sharding-proxy,代理数据库,提供分库分表,读写分离功能,对mysql客户端与DBA友好
4. 安装 mysql-proxy:将下载的 mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz 解压到 /opt/mysql-proxy 目录下,设置环境变量:export PATH=$PATH:/opt/mysql-proxy/mysql-proxy-0.8.5/bin 读写分离配置: 1. ...