標籤:maxscale
MaxScale 是 MariaDB 的產品之一,可以很方便的實現讀寫分離方案;並且提供了讀寫分離的負載平衡和高可用性保障。
一、安裝配置
前提:先配置好mysql的主從,Maxscale會根據主從複製資訊來判斷Master和slave
註:不能配置mysql互為主從,如果互為主從,兩台都會被認為slave,在這種情況下,寫請求會被拒絕,只接受讀請求。
1.1.安裝
官網下載對應的rpm包,
wget https://downloads.mariadb.com/MaxScale/2.0.3/centos/6server/x86_64/maxscale-2.0.3-1.centos.6.x86_64.rpmrpm -ivh maxscale-2.0.3-1.centos.6.x86_64.rpm
組建組態檔案 /etc/maxscale.conf,設定檔由多個配置模組組成
vi /etc/maxscale.conf#全域配置:[maxscale]threads=autolog_info=1log_notice=1log_warning=1#後端mysql定義[server1]type=serveraddress=192.168.10.1port=3306protocol=MySQLBackend[server2]type=serveraddress=192.168.10.2port=3306protocol=MySQLBackend#監控配置[MySQL Monitor] type=monitormodule=mysqlmon #監控模組使用mysqlmonservers=server1,server2user=admin #admin監控後端mysql的複製狀況,必須具有REPLICATION CLIENT許可權 passwd=123456monitor_interval=10000script=/opt/mysql_monitor.sh #定義事件觸發指令碼執行events=master_down #當master down時,執行上面的指令碼[Read-Write Service] type=servicerouter=readwritesplit #讀寫分離路由模式servers=server1,server2user=maxscale #該使用者從後端mysql擷取使用者資訊,對用戶端進行身分識別驗證,必須具有mysql.user table的select許可權 passwd=maxscalemax_slave_connections=100% max_slave_replication_lag=3600000 #最大允許slave的資料落後master 3600秒,仍然可用connection_timeout=300 router_options=master_failure_mode=error_on_write #允許master down掉,slave仍然可讀#router_options=master_accept_reads=true #允許master接受讀請求[Read-Write Listener] type=listenerservice=Read-Write Serviceprotocol=MySQLClientport=3306#管理服務配置 [MaxAdmin Service]type=servicerouter=cli#管理服務監聽[MaxAdmin Listener]type=listenerservice=MaxAdmin Serviceprotocol=maxscaledsocket=default
二、管理
Maxscale提供了maxadmin命令用於查看管理
[[email protected] ~]# maxadmin list -Unknown or missing option for the list command. Valid sub-commands are: clients List all the client connections to MaxScale dcbs List all the DCBs active within MaxScale filters List all the filters defined within MaxScale listeners List all the listeners defined within MaxScale modules List all currently loaded modules monitors List all monitors services List all the services defined within MaxScale servers List all the servers defined within MaxScale sessions List all the active sessions within MaxScale threads List the status of the polling threads in MaxScale
[[email protected] ~]# maxadmin list serversServers.-------------------+-----------------+-------+-------------+--------------------Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+--------------------server1 | 192.168.10.1 | 3306 | 0 | Master, Runningserver2 | 192.168.10.2 | 3306 | 0 | Slave, Running-------------------+-----------------+-------+-------------+--------------------
三、高可用性
Maxscale預設提供讀寫分離和讀的高可用性,要實現寫的高可用性,可以使用兩種途徑:
1.需要使用Multi-MasterMonitor監控模組,不同於上文使用的mysqlmon模組,該模組是通過read_only參數來選舉Master和Slave,結合指令碼可以實現在Master 宕機的時候,取消slave的read_only屬性,這樣slave就會變成Master
2.使用高可用軟體MMM
使用Maxscale實現mysql讀寫分離