一、MaxScale作用
MaxScale是maridb開發的一個MySQL資料中間件,配置好MySQL的主從複制架構後,希望實作讀寫分離,把讀操作分散到從伺服器中,并且對多個伺服器實作負載均衡。
MaxScale是插件式結構,允許使用者開發适合自己的插件。
讀寫分離和負載均衡是MySQL叢集的基礎需求,基礎架構:
<a href="http://s1.51cto.com/wyfs02/M00/89/D1/wKioL1geuIOAIu54AAE3z9zfFT8243.png-wh_500x0-wm_3-wmp_4-s_3313801083.png" target="_blank"></a>
二、MaxScale的基礎組成
MaxScale 目前提供的插件功能分為5類:
提供了登入認證功能,MaxScale 會讀取并緩存資料庫中 user 表中的資訊,當有連接配接進來時,先從緩存資訊中進行驗證,如果沒有此使用者,會從後端資料庫中更新資訊,再次進行驗證
包括用戶端連接配接協定,和連接配接資料庫的協定
決定如何把用戶端的請求轉發給後端資料庫伺服器,讀寫分離和負載均衡的功能就是由這個子產品實作的
對各個資料庫伺服器進行監控,例如發現某個資料庫伺服器響應很慢,那麼就不向其轉發請求了
提供簡單的資料庫防火牆功能,可以對SQL進行過濾和容錯
三、配置和測試過程:
基礎要求:
master:192.168.1.107
slave:192.168.1.108
slave:192.168.1.114
db verson:yum rpm
MaxScale version:RPM包
1、安裝資料庫、主從搭建(略)
主庫搭建好後添加MS使用者和Maxscale的路由使用者、監控賬号
grant replication slave, replication client on *.* to rep@'%' identified by 'root123';
grant replication slave, replication client on *.* to maxscale_monitor@'%' identified by 'root123';
grant select,show databases on *.* to maxscale@'%' identified by 'root123';
2、MaxScale:
安裝maxscale看了網絡上的各種文檔,隻能呵呵....
這裡提供maxscale的RPM的包,安裝簡單,直接yum -y install +包名即可
b、yum - y install
c、修改maxscale配置檔案(詳細見maxscale.cnf)
3、主從讀寫分離驗證:
1、在主節點通過3306登陸DB,添加表 添加資料:
CREATE TABLE `write_read` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
insert into write_read values(87);
同時也在其他2個SLAVE插入86 88的資料
2、通過mysql指令行通路maxscale所在節點的讀寫分離listener 4006端口
/usr/local/mysql/bin/mysql -udlan -proot123 -hip -P4006
MySQL [maxscale]> select * from write_read;
+------+
| id |
| 87 |
| 86 |
select 語句在其中的一個從庫上了
3、同時操作INSERT語句
MySQL [maxscale]> insert into write_read values(90)
-> ;
Query OK, 1 row affected (0.04 sec)
server1:
| 90 |
3 rows in set (0.00 sec)
###通過登陸3個DB的3306比較結果集:
MariaDB [maxscale]> select * from write_read;
+------+
| id |
| 87 |
| 90 |
2 rows in set (0.01 sec)
server2:
| 86 |
3 rows in set (0.00 sec)
server3:
| 88 |
####maxcale實作了讀寫分離效果,若開始一個事物的話,maxscale就會自動路由到master上,普通的查詢還是在SLAVE上
4、驗證當一台slave出現故障後,檢視MAXSCALE的狀态,及恢複後狀态
注意使用yum 安裝是在/var/log/maxscale2.log,log_info=1logdir=/tmp/ ###通過開啟 log_info 級别,可以看到 MaxScale 的路由日志
運作正常情況下:
[root@haproxy_2 ~]# maxadmin -pmariadb list servers;
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
server1 | 192.168.1.108 | 3306 | 0 | Master, Running
server2 | 192.168.1.107 | 3306 | 0 | Slave, Running
server3 | 192.168.1.114 | 3306 | 0 | Slave, Running
停止server2 的複制:
server2 | 192.168.1.107 | 3306 | 0 | Running
狀态改變了。。。。
檢視日志:
2016-09-15 04:10:08 notice : Server changed state: server2[192.168.1.107:3306]: lost_slave. [Slave, Running] -> [Running]
再次登陸maxcale的4006檢視用戶端查詢結果:
MySQL [maxscale]> select * from write_read;
MySQL [maxscale]> select @@hostname;
+------------+
| @@hostname |
| mycat |
由此可見maxcale功能,在SLAVE故障後自動排除不再向其轉發請求
##恢複server2。。。。
-------------------+-----------------+-------+-------------+-------------------
5、全部的SLAVE故障情況:
server1 | 192.168.1.108 | 3306 | 0 | Running
server2 | 192.168.1.107 | 3306 | 0 | Running
server3 | 192.168.1.114 | 3306 | 0 | Running
全部GG了。。。。
說明SLAVE全部失效後,會導緻MASTER也無法識别,整個服務失效,但實際還有個M還活着,還能提供服務,需要在monitor子產品裡添加一個重要參數
detect_stale_master=true
server1 | 192.168.1.108 | 3306 | 0 | Master, Stale Status, Running
server3 | 192.168.1.114 | 3306 | 0 | Running
.
####
通過定義的telnet登陸方式與maxadmin的登陸方式一樣:
telnet 127.0.0.1 4442 預設使用者名密碼admin/mariadb
MaxScale> add user max password
Account max for remote (network) usage has been successfully added.
MaxScale> show users
Enabled Linux accounts (secure) :
Created network accounts (insecure): max
就可以通過telnet 使用新的使用者登入
檢視更多的指令show --help
list threads.......
檢視服務狀态資訊show server servername
MaxScale> show server server1
Server 0x1536ed0 (server1)
Server: 192.168.1.108
Status: Master, Stale Status, Running
Protocol: MySQLBackend
Port: 3306
Server Version: 10.1.16-MariaDB
Node Id: 28703306
Master Id: -1
Slave Ids:
Repl Depth: -1
Number of connections: 1
Current no. of conns: 0
Current no. of operations: 0
如何實作單點故障,解決資源切換,需要配合MHA實作
本文轉自 DBAspace 51CTO部落格,原文連結:http://blog.51cto.com/dbaspace/1871274