內容預覽:
1、MaxScale簡介
2、MaxScale安裝
3、MaxScale配置
4、MaxScale簡單測試
5、MaxScale Debug Interface和CLI
6、MaxScale調試
7、可用資源
(文中圖片普遍偏小,請點擊看高清大圖)
1、MaxScale簡介
項目地址:https://github.com/skysql/MaxScale/
MaxScale是由http://www.skysql.com/開發的一款MySQL數據庫中間件。
-
MaxScale是什么(摘自Github/MaxScale):
The SkySQL MaxScale is an intelligent proxy that allows forwarding of database statements to one or more database servers using complex rules, a semantic understanding of the database statements and the roles of the various servers within the backend cluster of databases. MaxScale is designed to provide load balancing and high availability functionality transparantly to the applications. In addition it provides a highly scalable and flexibile architecture, with plugin components to support different protocols and routing decissions. MaxScale is implemented in C and makes entensive use of the asynchronous I/O capabilities of the Linux operating system. The epoll system is used to provide the event driven framework for the input and output via sockets.
簡單的說就是:MaxScale是一個支持高可用,負載均衡,具有良好的可擴展性,高性能的基於事件驅動的同時具有代理和管理功能的中間件。
-
Architecture:
如圖所示,MaxScale由以下幾個模塊組成:
Protocol,Router,Monitor,Authentication,Filter and logging.
MaxScale運行在clinet與server之間,起到一個”Exchange”的作用.
需要知道的是:client並不會直接與server建立連接,而是通過MaxScale來做連接創建和數據交換,MaxScale接收client的請求,經過(或不經過)Filter處理當前的Request,然后通過Router將client發送的request發送到后端server,server返回的數據經過(或不經過)處理之后再返回給client。MaxScale同時維護client請求的連接和到server的連接。
Client與Server、MaxScale之間的關系如圖:
1)Protocol:分為client protocol和back-end protocol.
client目前盡支持MySQL以及MariaDB,
back-end目前支持以下幾種類型的后端集群:
MySQL Replication
MariaDB Replication
Galera Cluster on both MySQL and MariaDB
MySQL Server with NDB storage engine
2)Router:Connection-based Routing和Statement-based Routing.
Connection-based Routing:client連接到maxscale之后,會為該client維護一個路由,同一個連接會始終通過這條路由,並且不檢查當前連接下的產生 的不同的請求,該路由一旦創建便會一直存在直到客戶端連接斷開。適合讀請求和寫請求使用不同的連接的場景。
Statement-based Routing:對同一個連接產生的不同的請求動態的路由到一個或多個后端的數據庫做處理。此類型路由可使得應用程序端操作非常簡單,讀寫分離以及負載均衡均對應用透明。
當MaxScale發現一個正在被客戶端使用的連接在連接到后端server時發生錯誤導致連接失敗的時候,MaxScale會自動重建一個到后端server的新的連接用來代替失效的連接,使得讓客戶端無感。
3)Authentication:
MaxScale 用從后端服務器獲取到的user表作為客戶端授權的依據,為了能驗證用戶連接,MaxScale從后端數據庫中讀取mysql.user表信息,並將此用 戶信息緩存在MaxScale端,當用戶連接進來會根據緩存的信息判斷是否通過驗證,如果當前緩存無此用戶,會從后端數據庫中更新用戶信息,並作驗證。
4)Filter & Logging:
Filter可比作database-level firewall,它可用於對某些特殊statement進行過濾或者Rewrite。適用於簡單類型的語句容錯,以及語句的自動轉換。
Rewrite:
Fault Tolerance:
5)Monitor:
用於對后端數據庫池的實時監控,目前只支持MySQL master/slave replication和Galera cluster。
2、MaxScale安裝
-
安裝准備工作
測試環境准備:
CentOS 6.5 x86_64:172.16.238.128 Master MySQL-5.6.19
CentOS 6.5 x86_64:172.16.238.129 Slave MySQL-5.6.19
CentOS 6.5 x86_64:172.16.238.130 Slave MySQL-5.6.19
CentOS 6.5 x86_64:172.16.238.131 Slave MySQL-5.6.19
CentOS 6.5 x86_64:172.16.238.132 MaxScale
配置好主從,並開啟半同步。
安裝包下載地址:
Binary package:https://downloads.skysql.com/files/SkySQL/MaxScale
Source package:http://github.com/skysql/MaxScale
-
從binary包安裝
1)解壓:
tar -zxvf maxscale.1.0.0-beta.tar.gz
2)將解壓的文件夾拷貝到安裝目錄即可:
mv maxscale-1.0.0-beta /usr/local/maxscale
3)maxscale程序啟動需要讀取兩個環境變量MAXSCALE_HOME和LD_LIBRARY_PATH
export MAXSCALE_HOME=/usr/local/maxscale
export LD_LIBRARY_PATH=/usr/local/maxscale/lib
如果需要永久生效還需將這兩行添加到profile或者.bashrc
4)拷貝MySQL配置文件 my.cnf 到 $MAXSCALE_HOME/mysql/.
cp /etc/my.cnf /usr/local/maxscale/mysql/my.cnf
5)拷貝默認配置文件
cd /usr/local/maxscale/etc/
cp MaxScale_template.cnf MaxScale.cnf
默認配置使用了3000,3001,3002三個mysql監聽端口,根據實際情況修改即可。
6)啟動maxscale
MaxScale默認會根據$MAXSCALE_HOME變量從$MAXSCALE_HOME/etc目錄中查找配置文件MaxScale.cnf。
如果變量$MAXSCALE_HOME沒有設置,則查找/etc/MaxScale.cnf文件。
以下三種啟動方式均可:
export MAXSCALE_HOME=/usr/local/maxscale
/usr/local/maxscale/bin/maxscale
或者
/usr/local/maxscale/bin/maxscale -c /usr/local/maxscale/
或者
/usr/local/maxscale/bin/maxscale -f /usr/local/maxscale/etc/MaxScale.cnf
-
從源碼安裝
作者原文:https://groups.google.com/forum/#!topic/maxscale/1NA_0Ni58X4
下文根據實際情況有所修改:
1)獲取源碼原文:
我不用git工具下載,而是直接從Github獲取源碼zip包
wget https://github.com/skysql/MaxScale/archive/master.zip -O MaxScale-src-1.0.zip
2)配置MariaDB安裝源(由於只用於編譯maxscale故不用安裝MariaDB-server)
vi /etc/yum.repos.d/mariadb.repo
寫入 :
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
保存退出。
3)安裝MariaDB-devel
(請直接安裝mariadb-devel,沒必要嘗試mysql-devel,因為,我嘗試過了,原因,你試試就知道)
yum install MariaDB-devel
如果網絡不給力就直接下載rpm包(比如我)
wget https://downloads.skysql.com/files/MariaDB/yum/5.5/centos6-amd64/rpms/MariaDB-5.5.38-centos6-x86_64-devel.rpm
如果安裝過程中發現提示沖突,那是因為已有mysql相關的包,請直接卸載即可rpm -e 或者yum remove。
4)安裝編譯MaxScale所需必要依賴:
yum -y install make gcc gcc-c++ ncurses-devel bison glibc-devel openssl-devel libaio-devel telnet
5)修改源碼包中的配置文件
unzip MaxScale-src-1.0.zip
cd /root/MaxScale-master/
vi 修改根目錄下 build_gateway.inc 文件中以下幾項
ROOT_PATH := $(HOME)/MaxScale-master (此處需注意,$HOME指的是當前用戶環境變量HOME,我用root運行,解壓后文件夾為/root/MaxScale-master,如果是其他位置直接用絕對路徑)
INC_PATH := /usr/include
MYSQL_ROOT := $(INC_PATH)/mysql
EMBEDDED_LIB := /usr/lib64 (mariadb-devel安裝后libmysqld.a在這個目錄下)
ERRMSG := /usr/share/mysql/english
保存退出
6)開始編譯
make depend
make all
make DEST=/usr/local/maxscale/ install
7)拷貝啟動腳本和配置文件
cp etc/init.d/maxscale /etc/init.d/
cp etc/MaxScale_template.cnf /usr/local/maxscale/etc/MaxScale.cnf
8)maxscale程序啟動需要讀取兩個環境變量MAXSCALE_HOME和LD_LIBRARY_PATH
export MAXSCALE_HOME=/usr/local/maxscale
export LD_LIBRARY_PATH=/usr/local/maxscale/lib
如果需要永久生效還需將這兩行添加到profile或者.bashrc
9)這時啟動maxscale和binary安裝方式類似,也可以使用service maxscale start啟動
可以任選一種方式安裝使用,安裝完成后各個文件位置如下圖:
需要注意的就是etc目錄下的passwd文件是在debug interface創建用戶之后自動生成的。
3、MaxScale配置和啟動
針對各項配置詳細解讀請看安裝包里面的文檔MaxScale Configuration And Usage Scenarios.pdf
配置文件主要結構如下圖所示:
1)詳細配置如下:
配置文件為/usr/local/maxscale/etc/MaxScale.cnf
grep -v "^#" /usr/local/maxscale/etc/MaxScale.cnf | grep -v "^$" [maxscale] threads=1
[MySQL Monitor] type=monitor module=mysqlmon servers=server1,server2,server3,server4 user=maxmonuser passwd=C891A19D7EFAF3B59AF7948E5AE3B998
[RW Split Router] type=service router=readwritesplit servers=server1,server2,server3,server4 max_slave_replication_lag=5 user=maxuser passwd=A71E58147EBD73986D3D074E80F00DB9
[Read Connection Router] type=service router=readconnroute router_options=slave servers=server1,server2,server3,server3 user=maxuser passwd=A71E58147EBD73986D3D074E80F00DB9
[HTTPD Router] type=service router=testroute servers=server1,server2,server3,server4
[Debug Interface] type=service router=debugcli servers=server1
[CLI] type=service router=cli
[RW Split Listener] type=listener service=RW Split Router protocol=MySQLClient port=4006 enable_root_user=1
[Read Connection Listener] type=listener service=Read Connection Router protocol=MySQLClient port=4008 enable_root_user=1
[Debug Listener] type=listener service=Debug Interface protocol=telnetd port=4442 address=127.0.0.1
[HTTPD Listener] type=listener service=HTTPD Router protocol=HTTPD port=6444
[CLI Listener] type=listener service=CLI protocol=maxscaled address=127.0.0.1 port=6603
[server1] type=server address=172.16.238.128 port=3306 protocol=MySQLBackend
[server2] type=server address=172.16.238.129 port=3306 protocol=MySQLBackend
[server3] type=server address=172.16.238.130 port=3306 protocol=MySQLBackend
[server4] type=server address=172.16.238.131 port=3306 protocol=MySQLBackend
2)配置里面用到的user需要在后端的各個mysql server里面建立用戶並授權。
create user 'maxuser'@'172.16.238.132' identified by 'maxpwd'; grant SELECT on mysql.user to 'maxuser'@'172.16.238.132';
maxmonuser用於監控后端服務器狀態
create user 'maxmonuser'@'172.16.238.132' identified by 'maxmonpwd'; grant REPLICATION SLAVE on *.* to 'maxmonuser'@'172.16.238.132'; grant REPLICATION CLIENT on *.* to 'maxmonuser'@'172.16.238.132';
另 外新建一個maxscale用於測試,此用戶不在配置文件中,且盡量不用root用戶,因為maxscale默認不允許root連接后端數據庫,需要在管 理界面enable root “some service” 才能開放root登錄,目前maxscale還不支持類似172.16.238.%以%作模糊匹配的主機地址,會報錯
create user 'maxscale'@'%' identified by 'maxscalepass'; grant ALL PRIVILEGES on *.* to 'maxscale'@'172.16.238.132';
為了使得配置文件中不以明文方式存放密碼,maxscale自帶了加密工具用於獲取加密后的字符串,maxscale在讀取加密字符串之后會自動解密。
進入/usr/local/maxscale/bin/目錄
./maxkeys $MAXSCALE_HOME/etc/.secrets
加密maxuser密碼 ./maxpasswd maxpwd A71E58147EBD73986D3D074E80F00DB9
加密maxmonuser密碼 ./maxpasswd maxmonpwd C891A19D7EFAF3B59AF7948E5AE3B998
至此,MaxScale配置完成,現在啟動MaxScale。
3)啟動MaxScale
[root@db132 bin]# /usr/local/maxscale/bin/maxscale
SkySQL MaxScale 0.7.0 Mon Jun 30 23:03:54 2014
——————————————————
Info : MaxScale will be run in a daemon process.
See the log from the following log files :
Error log : /usr/local/maxscale/log/skygw_err1.log
Message log : /usr/local/maxscale/log/skygw_msg1.log
Trace log : /usr/local/maxscale/log/skygw_trace1.log
Debug log : /usr/local/maxscale/log/skygw_debug1.log
Listening MySQL connections at 0.0.0.0:4006
Listening MySQL connections at 0.0.0.0:4008
Listening http connections at 0.0.0.0:6444
Listening telnet connections at 127.0.0.1:4442
或直接啟動服務
[root@db132 ~]# service maxscale start
Starting MaxScale: maxscale (pid 54649) is running… [ OK ]
啟動服務后,觀察啟動日志:
1.0beta版還會加載libmaxscaled.so模塊。
4、MaxScale簡單測試
1)連接測試,通過maxscale監聽的端口4006或者4008連接后端數據庫,默認會route到master
[root@db132 bin]# mysql -umaxscale -pmaxscalepass -h172.16.238.132 -P4006
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6385
Server version: 5.5.35-MariaDB MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
准備工作:
創建一個庫d,並簡單的表t,並在每個server中開啟general log。
2)測試查詢
mysqlslap -umaxscale -pmaxscalepass -h172.16.238.132 -P4006 \ --create-schema=d --query=select.sql \ --concurrency=6 --number-of-queries 1 --iterations=1 --debug-info
查詢語句: cat select.sql select * from t; select * from t; select * from t;
執行結果:
可以看到查詢被均勻的路由到三個從庫。
3)測試插入
mysqlslap -umaxscale -pmaxscalepass -h172.16.238.132 -P4006 \ --create-schema=d --query=insert.sql \ --concurrency=1 --number-of-queries 4 --iterations=1 --debug-info
cat insert.sql insert into t values(20,'x'); insert into t values(30,'xx'); insert into t values(40,'xxx'); insert into t values(50,'xxxx');
執行結果:
可以看到,主庫接收了所有寫操作,並且全部同步到了從庫,從庫可看到更新操作記錄。
4)測試更新
mysqlslap -umaxscale -pmaxscalepass -h172.16.238.132 -P4006 \ --create-schema=d --query=update.sql \ --concurrency=1 --number-of-queries 1 --iterations=1 --debug-info
cat update.sql update t set str='yy' where id=2;
執行結果:
同樣,更新操作的操作記錄跟插入類似,主庫上執行,然后同步到從庫。
5)測試事務
[root@db129 mysql]# mysql -umaxscale -pmaxscalepass -h172.16.238.132 -P4006
測試兩次事務操作,一次提交,一次不提交。
mysql> begin; mysql> select * from t where id=1 for update; mysql> update t set str='zz' where id=1; mysql> \q
mysql> begin; mysql> select * from t where id=1 for update; mysql> update t set str='zz' where id=1; mysql> commit; mysql> \q
執行結果:
可以看到,第一次開啟事務,執行操作,不提交,在主庫上看到更新操作記錄,從庫無記錄,第二次開始事務並提交,從庫會更新生效的一條記錄。
讀寫測試基本完成,目前未發現任何問題,還需在物理機上測試看看高並發下的處理能力。
maxscale對每個后端server作定期檢查,檢查的時間間隔由參數monitor_interval控制,默認10000ms。
MaxScale會根據語句類型作路由,在trace日志中可以看到如下記錄:
5、MaxScale Debug Interface 和 CLI
前面的配置中可以看到,定義了一個Debug Interface和一個CLI service,MaxScale可以通過telnet連接到Debug Interface進行一系列的管理操作,同時也可以通過CLI做相關操作。
-
Debug Interface
1)使用telnet登錄管理界面:
#telnet 127.0.0.1 4442
默認用戶名是admin,密碼是skysql
可以使用add user username password 覆蓋默認的用戶名密碼
使用show users可以看到當前的所有用戶
2)控制monitor
monitor可以定義多個,如果需要停用monitor可以使用shutdown命令。
show,shutdown,restart monitor
3)控制service
maxscale可以同時開啟多個Router並監聽端口,如果需要關閉其中某個類型的router,可以使用shutdown命令,重新啟用使用restart命令。(下圖重啟失敗,應該是bug,嘗試多次)
show,shutdown,restart service
4)控制server
如果需要臨時對某個主機進行維護,可以設置server為maintenance狀態,maxscale將不會再派發請求到該server,維護完成后,清楚此狀態即可。
show,set,clear server
5)控制log
enable啟用
disable停用
MaxScale> enable log debug
MaxScale> enable log trace
6)停機維護
對於service down的mysql服務器,可以手動設置維護模式,以不影響其他server,但是mascale會不停的探測該server是否重新運行。
server down之后master會發出如下兩種提示,設置maintenance之后err信息將停止。
#msg Jul 8 23:06:02 db132 MaxScale[53927]: Backend server 172.16.238.131:3306 state : NO STATUS
#err Jul 8 23:06:12 db132 MaxScale[53927]: Error : Monitor was unable to connect to server 172.16.238.131:3306 : "Can't connect to MySQL server on '172.16.238.131' (111)"
其他命令可自由發揮。
-
CLI(1.0beta)
CLI模式即可以在shell中直接使用maxadmin工具查看maxscale的狀態,需要配置CLI 以及 CLI Listener兩個Section。
可能你要問這個工具跟debug interface有什么區別?
區別就在於跟交互式的debug interface相比,maxadmin可以用於監控任何一個需要監控的對象,servers,service,dcbs,sessions,users等等。你可以把要檢查的內容列出來像寫shell腳本那樣寫入腳本中一起執行。
6、MaxScale調試模式
maxscale支持debug模式
#gdb maxscale
(gdb) run --d
此時ctl+c即可進入調試模式,可以調用一些function。
call printAllServers()
call printAllSessions()
call printServer(xxx)和call printSession(xxx)
結果跟debug interface和maxadmin輸出結果一樣。
MaxScale里面還有一個很重要的概念,那就是DCB(Descriptor Control Block )
一 個DCB(Descriptor Control Block)表示一個在MaxScale內部的連接的狀態,每個來自client的連接、到后端server的連接、以及每個listener都會被分配 一個DCB,這些連接的狀態統計由這些DCB來完成。每個DCB並不會有特定的名字用於查詢,而是直接使用具有唯一性的內存地址。
一個DCB的生命周期:
同樣可以通過Debug Interface查看DCB相關信息:
壓測的時候可以看到DCB狀態變化:
7、可用資源
幾個非常直觀的介紹:
http://www.slideshare.net/izoratti/140116-max-scale-for-effectivemysql
http://www.slideshare.net/skysql/maxscale-the-pluggibale-router-mariadb-roadshow-2014-paris
http://www.slideshare.net/skysql/maxscale-the-pluggable-router-36239961
http://files.meetup.com/107604/MaxScale_Overview_DBA_Hangout20140122.pdf
https://mariadb.com/resources/maxscale(Demo)
較老版本的一個安裝配置文檔:
http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-1
http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-2
http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-3
http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-4
Google Group:https://groups.google.com/forum/#!forum/maxscale
Bug Report:http://bugs.skysql.com/enter_bug.cgi?product=Maxscale
以及安裝包里自帶的非常有用的幾個Document:
Documentation/
├── Debug And Diagnostic Support.pdf
├── filters
│ ├── QLA Filter.pdf
│ ├── Regex Filter.pdf
│ ├── Tee Filter.pdf
│ └── Top Filter.pdf
├── history
│ ├── MaxScale 0.5 Release Notes.pdf
│ ├── MaxScale 0.6 Release Notes.pdf
│ └── MaxScale 0.7 Release Notes.pdf
├── internal
│ └── DCB states.pdf
├── MaxAdmin The MaxScale Administration And Monitoring Client.pdf
├── MaxScale 1.0beta Release Notes.pdf
├── MaxScale Configuration And Usage Scenarios.pdf
└── MaxScale HA with Corosync and Pacemaker.pdf
Coming Soon:
-
MaxScale結合MHA
-
MaxScale結合Pacemaker+Corosync
-
更詳細的測試(Filter)
-
壓力測試
以上內容完全基於本人理解得來,如有理解有誤的地方還請指出,以便及時改正。
部分bug已通過Google Group反饋給該軟件的作者,當前的各個版本,包括最新版1.0beta版仍存在很多運行不順暢的地方,所以遇到問題,要從多方面去考慮,去排查,如果能確定是bug,直接反饋就可以了。
本文出自 “張躍的部落格” 博客,請務必保留此出處http://mrcto.blog.51cto.com/1923168/1437287