您好,登錄后才能下訂單哦!
本文主要給大家介紹mysql-proxy如何實現數據庫讀寫分離,希望可以給大家補充和更新些知識,如有其它問題需要了解的可以持續在億速云行業資訊里面關注我的更新文章的。
機器:192.168.21.139
完成實戰之前的必要條件:主從復制環境搭建好。
MySQL Proxy是一個處于你的Client端和MySQL server端之間的簡單程序,它可以監測、分析或改變它們的通信。它使用靈活,沒有限制,常見的用途包括:負載平衡,故障、查詢分析,查詢過濾和修改等等。
MySQL Proxy就是這么一個中間層代理,簡單的說,MySQL Proxy就是一個連接池,負責將前臺應用的連接請求轉發給后臺的數據庫,并且通過使用lua腳本,可以實現復雜的連接控制和過濾,從而實現讀寫分離和負載平衡。對于應用來說,MySQLProxy是完全透明的,應用則只需要連接到MySQL Proxy的監聽端口即可。當然,這樣proxy機器可能成為單點失效,但完全可以使用多個proxy機器做為冗余,在應用云服務器的連接池配置中配置到多個proxy的連接參數即可。
MySQL Proxy更強大的一項功能是實現“讀寫分離”,基本原理是讓主數據庫處理事務性查詢,讓從庫處理SELECT查詢。數據庫復制被用來把事務性查詢導致的變更同步到集群中的從庫。
主機名 | IP地址 | 系統版本 |
mysql-proxy | 192.168.21.139 | CentOS release 6.4 (Final) 2.6.32-431.el6.x86_64 |
mysql-master | 192.168.21.135 | CentOS release 6.4 (Final) 2.6.32-431.el6.x86_64 |
mysql-slave | 192.168.21.135 | CentOS release 6.4 (Final) 2.6.32-431.el6.x86_64 |
yum -y install gcc gcc-c++autoconf libevent-devel pkgconfig libtool mysql-devel
[root@zyl tools]# wget http://ftp.gnome.org/pub/gnome/sources/glib/2.22/glib-2.22.5.tar.gz
tar xf glib-2.22.5.tar.gz
cd glib-2.22.5
./configure--prefix=/usr/local/glib-2.22.5
make && make install
echo $?
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar xf lua-5.1.4.tar.gz
cd lua-5.1.4
sed -i's#^INSTALL_TOP=.*#INSTALL_TOP= /usr/local/lua-5.1.4#gi' ./Makefile
sed -i 's#^CFLAGS=.*#CFLAGS= -02-fPIC -Wall $(MYCFLAGS)#gi' ./src/Makefile
make linux install
echo $?
tar xf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
cdmysql-proxy-0.8.5-linux-glibc2.3-x86-64bit
cd ..
cp -amysql-proxy-0.8.5-linux-glibc2.3-x86-64bit mysql-proxy
mv mysql-proxy /application/
cd /application/
ls
pkill mysql
netstat -lnput|grep 330
cd mysql
cd ../mysql-proxy/
ls
mkdir conf
mkdir logs
ls
cd ./conf/
ls
#添加到PATH環境變量
echo 'exportPATH=$PATH:/application/mysql-proxy/bin/' >>/etc/profile
tail -1 /etc/profile
source /etc/profile
which mysql-proxy
/application/mysql-proxy/bin/mysql-proxy--help
vim ./mysql-proxy.cnf
#############################################
簡單一個配置(192.168.21.135:單機多實例)
[mysql-proxy]
plugins=admin,proxy
admin-username=admin
admin-password=admin
admin-lua-script=/application/mysql-proxy/lib/mysql-proxy/lua/admin.lua
proxy-backend-addresses=192.168.21.135:3306 èmaster
proxy-read-only-backend-addresses=192.168.21.135:3308èslave
proxy-lua-script=/application/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
log-level=debug 日志的級別
keepalive=true 在mysql-proxy崩潰時嘗試重啟之;
daemon=true 以守護進程模式啟動mysql-proxy;
log-file=/application/mysql-proxy/logs/proxy.log
##########################
chmod 0660 mysql-proxy.cnf
啟動:
/application/mysql-proxy/bin/mysql-proxy--defaults-file=/application/mysql-proxy/conf/mysql-proxy.cnf
cat logs/proxy.log
[root@zyl tools]# netstat -lnpt|grep mysql-proxy
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 4261/mysql-proxy
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 4261/mysql-proxy
4040是proxy端口;4041是admin端口,也就是管理。
mysqlgrant all on *.* to 'zyl'@'192.168.21.139' identified by '888666';
mysql> flush privileges
##########################
多開幾個終端,測試連接:
[root@zyl -master ]# mysql -u zyl -p -h 192.168.21.139 --port 4040
[root@zyl mysql-proxy]# mysql-uadmin -P4041 -h 192.168.21.139 -padmin
mysql> select * from help;
+------------------------+------------------------------------+
| command | description |
+------------------------+------------------------------------+
| SELECT * FROM help | shows this help |
| SELECT * FROM backends | liststhe backends and their state |
+------------------------+------------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM backends;
+-------------+---------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+---------------------+---------+------+------+-------------------+
| 1 | 192.168.21.135:3306 | unknown | rw |NULL | 0 |
| 2 | 192.168.21.135:3308 | unknown | ro | NULL | 0 |
+-------------+---------------------+---------+------+------+-------------------+
最終結果:
mysql> select * frombackends;
+-------------+-------------------+-------+------+------+-------------------+
| backend_ndx | address | state | type | uuid |connected_clients |
+-------------+-------------------+-------+------+------+-------------------+
| 1 |192.168.21.135:3306 | up | rw | NULL | 0 |
| 2 |192.168.21.135:3308 | up | ro | NULL | 0 |
+-------------+-------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)
##出現兩個up,那么分離成功了。
1.
[root@zyl lua-5.1.4]# make linuxinstall
cd src && make linux
make[1]: Entering directory`/home/tools/lua-5.1.4/src'
make allMYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory-lncurses"
make[2]: Entering directory`/home/tools/lua-5.1.4/src'
gcc -O2 -Wall -fPIC -DLUA_USE_LINUX -c -o lua.o lua.c
在包含自 lua.h:16 的文件中,
從 lua.c:15:
luaconf.h:275:31: 錯誤:readline/readline.h:沒有那個文件或目錄
luaconf.h:276:30: 錯誤:readline/history.h:沒有那個文件或目錄
lua.c: 在函數‘pushline’中:
lua.c:182: 警告:隱式聲明函數‘readline’
lua.c:182: 警告:賦值時將整數賦給指針,未作類型轉換
lua.c: 在函數‘loadline’中:
lua.c:210: 警告:隱式聲明函數‘add_history’
make[2]: *** [lua.o] 錯誤 1
make[2]: Leaving directory`/home/tools/lua-5.1.4/src'
make[1]: *** [linux] 錯誤 2
make[1]: Leaving directory`/home/tools/lua-5.1.4/src'
make: *** [linux] 錯誤 2
解決:
yum installlibtermcap-devel ncurses-devel libevent-devel readline-devel
-------------------------------------------------------------------------------------------
2.
[root@zyl tools]# mysql -u zyl -p -h 192.168.21.139 --port 4040
ERROR 1105 (HY000): #07000MySQLProxy Lua script failed to load. Check the error log.
解決:
[root@zyl lua]# pkillmysql-proxy
[root@zyl lua]# cp./mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy/rw-splitting.lua/opt/mysql-proxy/lib/mysql-proxy/lua/
[root@zyl lua]# vim/opt/mysql-proxy/init.d/mysql-proxy
更改腳本:
檢查這個腳本:--proxy-lua-script=/opt/mysql-proxy/lib/mysql-proxy/lua/rw-splitting.lua"
-------------------------------------------------------------------------------------------
3.
[root@zyl lua]# mysql -u zyl -p -h 192.168.21.139 --port 4040
注:因為rw-splitting.lua腳本默認有4個鏈接才啟用分離;所以多開啟幾個終端;多測試幾下;你也可以去修改里面的相關值;
改rw-splitting.lua讀寫分離腳本如下參數:
4.編譯glib時報錯
configure: error:
*** You must have either havegettext support in your C library, or use the
*** GNU gettext library.(http://www.gnu.org/software/gettext/gettext.html
解決:
[root@zyl glib-2.22.5]# yum install -y gettext
-------------------------------------------------------------------------------------------
5.
[root@zyl conf]#/application/mysql-proxy/bin/mysql-proxy--defaults-file=/application/mysql-proxy/conf/mysql-proxy.cnf
2016-09-02 22:28:37: (critical)mysql-proxy-cli.c:326: loading config from'/application/mysql-proxy/conf/mysql-proxy.cnf' failed: permissions of/application/mysql-proxy/conf/mysql-proxy.cnf aren't secure (0660 or stricter required)
2016-09-02 22:28:37: (message)Initiating shutdown, requested from mysql-proxy-cli.c:328
2016-09-02 22:28:37: (message) shuttingdown normally, exit code is: 1
解決:
[root@zyl mysql-proxy]# chmod0660 mysql-proxy.cnf
看了以上關于mysql-proxy如何實現數據庫讀寫分離,希望能給大家在實際運用中帶來一定的幫助。本文由于篇幅有限,難免會有不足和需要補充的地方,如有需要更加專業的解答,可在官網聯系我們的24小時售前售后,隨時幫您解答問題的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。