一、安裝mysql-proxyhtml
實現讀寫分離是有lua腳本實現的,如今mysql-proxy裏面已經集成,無需再安裝mysql
下載:http://dev.mysql.com/downloads/mysql-proxy/linux
tar
zxvf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.
tar
.gz
mv
mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit
/usr/local/mysql-proxy
cd
/usr/local/mysql-proxy
mkdir
lua
#建立腳本存放目錄
mkdir
logs
#建立日誌目錄
cp
share
/doc/mysql-proxy/rw-splitting
.lua .
/lua
#複製讀寫分離配置文件
cp
share
/doc/mysql-proxy/admin-sql
.lua .
/lua
#複製管理腳本
vi
/etc/mysql-proxy
.cnf
#建立配置文件
[mysql-proxy] ## 第二次和第三次配置的什麼,漏掉這一行列,找好久的錯誤。
user=root
#運行mysql-proxy用戶
admin-username=proxy
#主從mysql共有的用戶
admin-password=123.com
#用戶的密碼
proxy-address=192.168.0.204:4040
# 若是不寫端口號,默認端口號3306,與Mysql的端口號衝突,因此會報錯。*************************!!!!
proxy-
read
-only-backend-addresses=192.168.0.203
#指定後端從slave讀取數據
proxy-backend-addresses=192.168.0.202
#指定後端主master寫入數據
proxy-lua-script=
/usr/local/mysql-proxy/lua/rw-splitting
.lua
#指定讀寫分離配置文件位置
admin-lua-script=
/usr/local/mysql-proxy/lua/admin-sql
.lua
#指定管理腳本
log-
file
=
/usr/local/mysql-proxy/logs/mysql-proxy
.log
#日誌位置
log-level=info
#定義log日誌級別,由高到低分別有(error|warning|info|message|debug)
daemon=
true
#以守護進程方式運行
keepalive=
true
#mysql-proxy崩潰時,嘗試重啓
保存退出!
chmod 660 /etc/mysql-proxy.cnf
vi
/usr/local/mysql-proxy/lua/rw-splitting
.lua
if
not proxy.global.config.rwsplit
then
proxy.global.config.rwsplit = {
min_idle_connections = 1,
#默認超過4個鏈接數時,纔開始讀寫分離,改成1
max_idle_connections = 1,
#默認8,改成1
is_debug =
false
}
end
/usr/local/mysql-proxy/bin/mysql-proxy
--defaults-
file
=
/etc/mysql-proxy
.cnf
netstat
-tupln |
grep
4040
#已經啓動
tcp 0 0 192.168.0.204:4040 0.0.0.0:* LISTEN 1264
/mysql-proxy
關閉mysql-proxy使用:killall -9 mysql-proxy
mysql> grant all on *.* to
'proxy'
@
'192.168.0.204'
identified by
'123.com'
;
mysql> create table user (number INT(10),name VARCHAR(255));
mysql> insert into user
values(01,
'zhangsan'
);
mysql> insert into user values(02,
'lisi'
);
參考文獻:http://lizhenliang.blog.51cto.com/7876557/1305083sql
參考文獻2:http://www.cnblogs.com/luckcs/articles/2543607.html數據庫