利用mysql-proxy进行mysql数据库的读写分离

实验系统:CentOS 6.6_x86_64

实验前提:防火墙和selinux都关闭

实验说明:本实验共有4台主机,IP分配如拓扑

实验软件:mariadb-10.0.20  mysql-proxy-0.8.5-linux-el6-x86-64bit

下载地址:http://pan.baidu.com/s/1i3F5Pop

实验拓扑:

    

一、准备工作:

  1.将主机名称改为如下所示:

    

  2.将hosts文件添加如下内容:

    

  3.master、slave1和slave2安装mariadb:

tar xf mariadb-10.0.20-linux-x86_64.tar.gz  -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.0.20-linux-x86_64 mysql
useradd -r mysql
mkdir -pv /mydata/data
chown -R mysql.mysql /mydata/data/
cd mysql/
chown -R root.mysql .
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

二、配置主从复制

  1.master上配置my.cnf:

[mysqld]
server-id       = 1
datadir = /mydata/data
log-bin = /mydata/data/master-bin
binlog_format = ROW
sync_binlog = 1                        //确保每次事务提交之前都能将二进制日志同步磁盘上

  2.slave1上配置my.cnf:

[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
server-id       = 2
datadir = /mydata/data
relay_log = /mydata/data/relay-log
read_only = 1
sync_master_info = 1                   //及时同步master文件
sync_relay_log = 1                     //及时同步relay-log文件
sync_relay_log_info = 1                //及时同步relay-log-info文件

  3.slave2上配置my.cnf:

[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
server-id       = 3
datadir = /mydata/data
relay_log = /mydata/data/relay-log
read_only = 1
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1

  4.在master上创建复制用户:

service mysqld start
/usr/local/mysql/bin/mysql
------------------------------------------->
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘slave‘@‘192.168.19.%‘ IDENTIFIED BY ‘123456‘;
FLUSH PRIVILEGES;

  5.在master上查看二进制日志位置:

SHOW MASTER LOGS;

    

  6.两台slave上操作:

CHANGE MASTER TO MASTER_HOST=‘master‘,MASTER_USER=‘slave‘,MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘master-bin.000001‘,MASTER_LOG_POS=637;
START SLAVE;
SHOW SLAVE STATUS\G

    

三、安装mysql-proxy

  1.此实验中19.79为mysql-proxy服务器,所以软件安装在此主机上:

tar xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy
useradd -r mysql-proxy

  2.提供服务脚本:

vim /etc/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<---------------------------------------------------------chmod +x /etc/init.d/mysql-proxychkconfig --add mysql-proxy

  3.为服务脚本提供配置文件:

vim /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=""
PROXY_USER="mysql-proxy"
PROXY_OPTIONS="--daemon --log-level=info --log-use-syslog --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.19.66:3306 --proxy-read-only-backend-addresses=192.168.19.74:3306
 --proxy-read-only-backend-addresses=192.168.19.76:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"

//--daemon:以守护进程模式启动mysql-proxy//--proxy-backend-addresses:后端可读写的mysql服务器的地址和端口//--proxy-read-only-backend-addresses:后端只读mysql服务器的地址和端口//--proxy-lua-script:完成mysql代理功能的Lua脚本

  4.提供admin.lua文件:

vim /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

  5.为了使实验结果更明显,编辑rw-splitting.lua文件中的其中2个数值:

vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
--------------------------------------------------------------------------->
if not proxy.global.config.rwsplit then
        proxy.global.config.rwsplit = {
                min_idle_connections = 1,     //默认为4
                max_idle_connections = 1,    //默认为8

                is_debug = false
        }
end          //mysql-proxy会检测客户端连接,当连接没有超过min_idle_connections预设值时, 不会进行读写分离, 即查询操作会发生到Master上。

5.启动mysql-proxy:

service mysql-proxy start
ss -tnlp                      //查看端口

    

  6.连接测试:

yum -y install mysql                               //如果没有mysql客户端的话执行此步
mysql -uadmin -padmin -h192.168.19.79 --port=4041
------------------------------------------------------------>
SELECT * FROM backends;
+-------------+--------------------+---------+------+------+-------------------+
| backend_ndx | address            | state   | type | uuid | connected_clients |
+-------------+--------------------+---------+------+------+-------------------+
|           1 | 192.168.19.66:3306 | unknown | rw   | NULL |                 0 |
|           2 | 192.168.19.74:3306 | unknown | ro   | NULL |                 0 |
|           3 | 192.168.19.76:3306 | unknown | ro   | NULL |                 0 |
+-------------+--------------------+---------+------+------+-------------------+

四、读写分离测试:

  1.在master上创建测试用户:

GRANT ALL ON *.* TO ‘jason‘@‘192.168.19.%‘ IDENTIFIED BY ‘123456‘;
FLUSH PRIVILEGES;

  2.分别在三台mariadb服务器上抓包:

    master:

tcpdump -i eth0 -nn -XX ip dst 192.168.19.66 and tcp dst port 3306      //目标是19.66并且端口是3306

    slave1:

tcpdump -i eth0 -nn -XX ip dst 192.168.19.74 and tcp dst port 3306

    slave2:

tcpdump -i eth0 -nn -XX ip dst 192.168.19.76 and tcp dst port 3306

  3.mysql-proxy上进行数据库操作:

mysql -ujason -p123456 -h192.168.19.79
------------------------------------------------->
CREATE DATABASE hello;USE mysql;SELECT * FROM user;             //可以用额外的主机多执行几次

    在master上的抓包信息:

    

    在slave上的抓包信息:

    

    

  4.查看状态,在proxy上操作,可以看到状态全部为up:

mysql -uadmin -padmin -h192.168.19.79 --port=4041
------------------------------------------------------------->
SELECT * FROM backends;
+-------------+--------------------+-------+------+------+-------------------+
| backend_ndx | address            | state | type | uuid | connected_clients |
+-------------+--------------------+-------+------+------+-------------------+
|           1 | 192.168.19.66:3306 | up    | rw   | NULL |                 0 |
|           2 | 192.168.19.74:3306 | up    | ro   | NULL |                 0 |
|           3 | 192.168.19.76:3306 | up    | ro   | NULL |                 0 |
+-------------+--------------------+-------+------+------+-------------------+

五、拓展实验

  1.在proxy上安装httpd和php:

yum install httpd php php-mysql
service httpd start

  2.让httpd可以支持index.php首页,然后放入wordpress页面文件,创建wordpress数据库并安装:

    

  3.安装完后修改wordpress的配置文件,将master地址改为proxy的:

vim /var/www/html/wp-config.php

    

  4.访问测试并抓包:

    master:

    

    slave:

    

    

    至此,读写分离实验演示完毕,谢谢!如有问题,请联系我,QQ:82800452

时间: 2024-09-30 06:45:00

利用mysql-proxy进行mysql数据库的读写分离的相关文章

利用oneproxy部署mysql数据库的读写分离

实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验说明:本实验共有4台主机,IP分配如拓扑 实验软件:mariadb-10.0.20 oneproxy-rhel5-linux64-v5.6-ga 下载地址:http://pan.baidu.com/s/1jGpL2o2 实验拓扑: 注意:本实验是之前mysql-proxy实验的拓展,因此大部分环境及设定是相同的,其中一.二步这里不再重复,具体请参考:利用mysql-proxy进行mysql数据库的读写分离

mysql数据库的读写分离

一.      熟悉mysql读写分离及mysql存储引擎的区别. Ndb集群存储引擎.myisam.innodb Myisam为mysql安装后默认的存储引擎,不支持事务,行级锁和外键约束的功能:ndb为mysql集群存储引擎与innodb都支持事物,行级锁和外键约束.使用show engines;查看mysql支持的存储引擎. 读写分离: i.         环境: 应用程序client Database proxy Database集群(两台以上mysql机器) ii.         

MySQL数据库之读写分离

一.概述: MySQL数据库主从结构配置以后,正常情况下数据库的所有读写操作全部都在主数据库上面,从数据库仅仅作为数据备份使用,显然无法有效的使用服务器资源,那么实现读写分离的需求就不可避免. 二.拓扑图说明: 如上图所示,本文要实现的是读MySQL数据库的写入操作(增删改)等在Master服务器(192.168.4.10)上面实现,而对MySQL数据库的读取操作(查询)等在Slave服务器(192.168.4.20)上面完成. 如果在程序员编程时创建两个数据库连接Connection,在程序中

MySQL深入10-利用Ameoba实现读写分离

本篇博文主要介绍利用Ameoba实现MySQL的读写分离功能,而MySQL官方的mysql-proxy需要自己编写复杂的lua脚本才能实现同样的功能,故个人还是倾向使用amoeba哦: =================================================================== 1 简介 2 准备 2.1 时间同步 2.2 配置MySQL主从复制架构 3 ameoba安装配置 3.1 安装配置JDK 3.2 安装ameoba 3.3 配置ameoba 3

MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践

Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中,由单台Mysql作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面. 因此,一般来说都是通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力 这样的方案来进行部署与实施的. 如下图所示: 下面是我在实际工作过程中所整理的笔记,在此分享出来,以供大家参考. 一.MySQL的安装与

Mysql一主多从和读写分离配置简记

Mysql一主多从和读写分离配置简记 标签: mysql数据库服务器class数据库servermanager 2012-05-30 16:44 14981人阅读 评论(1) 收藏 举报  分类: 数据库 版权声明:本文为博主原创文章,未经博主允许不得转载. 近期开发的系统中使用MySql作为数据库,由于数据涉及到Money,所以不得不慎重.同时,用户对最大访问量也提出了要求.为了避免Mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离.在此简做纪要,以备日后所用! 一.配置主

MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 转载

http://heylinux.com/archives/1004.html MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中,由单台Mysql作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面. 因此,一般来说都是通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy

linux下mysql基于mycat做主从复制和读写分离之基础篇

Linux下mysql基于mycat实现主从复制和读写分离1.基础设施 两台虚拟机:172.20.79.232(主) 172.20.79.233(从) 1.1软件设施 mysql5.6.39 , mycat1.6-RELEASE jdk1.7及其以上版本2.实现步骤一(mycat实现读写分离) 1.首先在两台服务器安装mysql 1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rp

docker部署django项目、mysql主从搭建、django实现读写分离

目录 docker部署django项目 1.1 基于python基础镜像 将本地django项目打包并发到远程服务器上 将服务器上的.zip django项目解压 部署的具体流程 1.2 基于dockerfile 2 mysql主从搭建 3 django实现读写分离 我们使用python镜像源构建的出的容器,是一个纯净的debian系统下的python环境,在容器内部要想安装vim.nginx等,就不能使用yum install ...了,需要使用apt-get源.首先更新apt-get源,使用

基于 EntityFramework 的数据库主从读写分离服务插件

基于 EntityFramework 的数据库主从读写分离服务插件 1.       版本信息和源码 1.1 版本信息 v1.0 beta(2015-04-02),基于 EF 6.1 开发,支持 EF 6.1 之后的所有 EF6 版本. 1.2 开放源码地址 https://github.com/cjw0511/NDF.Infrastructure 关于该 EF 数据库主从读写分离服务核心源码位于文件夹: src\ NDF.Data.EntityFramework\MasterSlaves 文件