广告
返回顶部
首页 > 资讯 > 数据库 >mysql-proxy实现读写分离脚本
  • 372
分享到

mysql-proxy实现读写分离脚本

2024-04-02 19:04:59 372人浏览 泡泡鱼
摘要

将以下脚本保存为/etc/init.d/Mysql-proxy ,赋予权限chmod 755 /etc/init.d/mysql-proxy#!/bin/bash # # mysql-proxy&

  1. 将以下脚本保存为/etc/init.d/Mysql-proxy ,赋予权限

    chmod 755 /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



2.为服务脚本提供配置文件/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"


#注意最后一行,需要根据实际生产场景进行修改,比如;

PROXY_OPTIONS="--daemon --log-level=info --log-file=/var/log/mysql-proxy.log --plugins=proxy

 --plugins=admin --proxy-backend-addresses=192.168.141.101:3306 --proxy-read-only-backend-ad

dresses=192.168.141.102:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy

/rw-splitting.lua"


3.复制如下内容建立admin.lua文件,将其保存至/usr/local/mysql-proxy/share/doc/mysql-proxy/目录中:

--[[ $%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




您可能感兴趣的文档:

--结束END--

本文标题: mysql-proxy实现读写分离脚本

本文链接: https://www.lsjlt.com/news/35097.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mysql-proxy实现读写分离脚本
    将以下脚本保存为/etc/init.d/mysql-proxy ,赋予权限chmod 755 /etc/init.d/mysql-proxy#!/bin/bash # # mysql-proxy&...
    99+
    2022-10-18
  • 使用mysql-proxy实现读写分离
    mysql-proxy实现MySQL读写分离 使用二进制格式的mysql-proxy的安装配置 mysql-proxy本身并不能进行读写分离,要实现读写分离要依赖于lua,所以要先查看是否安装了lua,...
    99+
    2022-10-18
  • mysql 基于mysql-proxy实现读写分离
    mysql 读写分离 读写分离首先不推荐使用,很多业务场景也没有必要,不仅增加技术复杂度,而且可能会导致读到落后的数据,建议优化数据库,推荐使用keepalive+mysql双主复制的方案 然后mysql-proxy是官方推出的中间件,不仅...
    99+
    2015-08-29
    mysql 基于mysql-proxy实现读写分离 数据库入门 数据库基础教程 数据库 mysql
  • 安装mysql-proxy实现mysql读写分离
    一. 环境描述操作系统版本:centos 7.2主服务器:192.168.0.72  node2从服务器:192.168.0.73  node3调度服务器MySQL-Proxy...
    99+
    2022-10-18
  • 使用mysql-proxy实现mysql的读写分离
    TEST ...
    99+
    2022-10-18
  • 企业 - mysql 读写分离(proxy)
                     &...
    99+
    2022-10-18
  • MySQL读写分离amoeba&mysql-proxy
    ----主从同步介绍 refencen    https://www.cnblogs.com/lin3615/p/5684891.html 1. 读写分离方式 ...
    99+
    2022-10-18
  • 关系型数据库之mysql-proxy实现读写分离
    简要:  mysql-proxy作为mysql测试项目,可以实现读写分离架构,具有开发能力的公司通过二次开发的方式去完善bug应用在生产环境中,下面我们通过案例使用mysql-proxy实现读写分...
    99+
    2022-10-18
  • 如何进行Mysql中的mysql-proxy读写分离
    本篇文章为大家展示了如何进行Mysql中的mysql-proxy读写分离,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。以配置文件的方式启动[root@host-19...
    99+
    2022-10-19
  • mysql+mycat实现读写分离
    centos7 master slave mycat1.6 client 192.168.41.10 192.168.41.11 192.168.41.12 192.168.41.13 ...
    99+
    2022-10-18
  • MySQL Router实现MySQL的读写分离
    MySQL Router实现MySQL的读写分离 https://www.cnblogs.com/f-ck-need-u/p/9276639.html https://www.cnblogs.com...
    99+
    2022-10-18
  • MyCAT实现MySQL的读写分离
    在MySQL中间件出现之前,对于MySQL主从集群,如果要实现其读写分离,一般是在程序端实现,这样就带来一个问题,即数据库和程序的耦合度太高,如果我数据库的地址发生改变了,那么我程序端也要进行相应的修改,如...
    99+
    2022-10-18
  • 怎么实现mysql读写分离
    这篇文章主要介绍“怎么实现mysql读写分离”,在日常操作中,相信很多人在怎么实现mysql读写分离问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么实现mysql读写分离”...
    99+
    2022-10-19
  • 使用MySQL-Proxy读写分离时需要注意什么
    这篇文章主要介绍“使用MySQL-Proxy读写分离时需要注意什么”,在日常操作中,相信很多人在使用MySQL-Proxy读写分离时需要注意什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,...
    99+
    2022-10-18
  • MySQL使用ProxySQL实现读写分离
    1 ProxySQL简介: ProxySQL是一个高性能的MySQL中间件,拥有强大的规则引擎。官方文档:https://github.com/sysown/proxysql/wiki/下载地址:https...
    99+
    2022-10-18
  • 使用Maxscale实现mysql读写分离
    MaxScale 是 MariaDB 的产品之一,可以很方便的实现读写分离方案;并且提供了读写分离的负载均衡和高可用性保障。一、安装配置前提:先配置好mysql的主从,Maxscale会自动的根据...
    99+
    2022-10-18
  • MySQL中如何实现读写分离
    MySQL中如何实现读写分离,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一,创建Master数据库的配置文件vi master...
    99+
    2022-10-18
  • php+mysql如何实现读写分离
    这篇“php+mysql如何实现读写分离”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“php+mysql如何实现读写分离”文...
    99+
    2023-07-05
  • MySQL中间件proxysql实现MySQL读写分离
    1. Mysql实现读写分离的方式 mysql 实现读写分离的方式有以下几种: 程序修改mysql操作,直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配,需要开发人员协助。 amoeba,直接实现读写分离和负载...
    99+
    2021-12-16
    MySQL中间件proxysql实现MySQL读写分离 数据库入门 数据库基础教程 数据库 mysql
  • mysql中Oneproxy如何实现读写分离
    这篇文章将为大家详细讲解有关mysql中Oneproxy如何实现读写分离,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。基本架构: 写请求全部定向到主库,数据通过日志异步复...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作