MySQLProxy實(shí)現(xiàn)讀寫分離的實(shí)戰(zhàn)舉例

下文主要給大家?guī)?lái)MySQL Proxy實(shí)現(xiàn)讀寫分離的實(shí)戰(zhàn)舉例,希望這些內(nèi)容能夠帶給大家實(shí)際用處,這也是我編輯MySQL Proxy實(shí)現(xiàn)讀寫分離的實(shí)戰(zhàn)舉例這篇文章的主要目的。好了,廢話不多說(shuō),大家直接看下文吧。

10年積累的做網(wǎng)站、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有清豐免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

       規(guī)劃:
主mysql服務(wù)器:192.168.1.21
從mysql服務(wù)器: 192.168.1.22
mysql讀寫分離器:192.168.1.23

1、讀寫分離服務(wù)器上解壓安裝包,并添加對(duì)應(yīng)用戶,并編輯啟動(dòng)腳本;
# tar xf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local/
# cd /usr/local/
# ln -sv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit mysql-proxy
# cd mysql-proxy
# useradd mysql-proxy
# 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-proxy
# 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.1.21:3306 --proxy-read-only-backend-addresses=192.168.1.22:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"

2、編輯admin.lua腳本文件,將其保存至/usr/local/mysql-proxy/share/doc/mysql-proxy/中;
# 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
# service mysql-proxy start

3、測(cè)試;
3.1、管理功能測(cè)試:
# yum -y install mysql
# mysql -uadmin -padmin -h292.168.1.23 --port=4041
mysql> SELECT * FROM backends;

3.2、主服務(wù)器上創(chuàng)建一個(gè)復(fù)制賬號(hào);
mysql> GRANT ALL ON *.* TO 'admin'@'192.168.1.23' IDENTIFIED BY 'admin';
mysql> FLUSH PRIVILEGES;

3.3、在mysql-proxy上使用主服務(wù)器的賬號(hào)登錄;
# mysql -u admin -p -h 192.168.1.23
此時(shí)即可登錄到主mysql服務(wù)器;
mysql> CREATE DATABASE mydb;
此時(shí)創(chuàng)建數(shù)據(jù)庫(kù)應(yīng)該僅發(fā)送給主mysql服務(wù)器,可使用tcpdump的方式獲取數(shù)據(jù)流向,創(chuàng)建數(shù)據(jù)庫(kù)完成后mysql-proxy與主mysql服務(wù)器已建立聯(lián)系,因而在mysql-proxy上使用SELECT * FROM backends即可查看服務(wù)器狀態(tài);

對(duì)于以上關(guān)于MySQL Proxy實(shí)現(xiàn)讀寫分離的實(shí)戰(zhàn)舉例,大家是不是覺得非常有幫助。如果需要了解更多內(nèi)容,請(qǐng)繼續(xù)關(guān)注我們的行業(yè)資訊,相信你會(huì)喜歡上這些內(nèi)容的。

網(wǎng)站欄目:MySQLProxy實(shí)現(xiàn)讀寫分離的實(shí)戰(zhàn)舉例
當(dāng)前網(wǎng)址:http://muchs.cn/article4/pihioe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)Google、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作