實驗系統:CentOS 6.6_x86_64php
實驗前提:防火牆和selinux都關閉html
實驗說明:本實驗共有4臺主機,IP分配如拓撲mysql
實驗軟件:mariadb-10.0.20 mysql-proxy-0.8.5-linux-el6-x86-64bitlinux
下載地址:http://pan.baidu.com/s/1i3F5Popredis
實驗拓撲:sql
1、準備工做:數據庫
1.將主機名稱改成以下所示:vim
2.將hosts文件添加以下內容:後端
3.master、slave1和slave2安裝mariadb:bash
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
2、配置主從複製
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
3、安裝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-proxy
chkconfig --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 | +-------------+--------------------+---------+------+------+-------------------+
4、讀寫分離測試:
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 | +-------------+--------------------+-------+------+------+-------------------+
5、拓展實驗
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