使用Maxscale實(shí)現(xiàn)mysql讀寫分離

MaxScale 是 MariaDB 的產(chǎn)品之一,可以很方便的實(shí)現(xiàn)讀寫分離方案;并且提供了讀寫分離的負(fù)載均衡和高可用性保障。

10年積累的成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有宜賓免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

一、安裝配置

前提:先配置好MySQL的主從,Maxscale會(huì)自動(dòng)的根據(jù)主從復(fù)制信息來判斷Master和slave

注:不能配置mysql互為主從,如果互為主從,兩臺(tái)都會(huì)被認(rèn)為slave,在這種情況下,寫請求會(huì)被拒絕,只接受讀請求。

1.1.安裝

官網(wǎng)下載對應(yīng)的rpm包,

wget  https://downloads.mariadb.com/MaxScale/2.0.3/centos/6server/x86_64/maxscale-2.0.3-1.centos.6.x86_64.rpm
rpm -ivh maxscale-2.0.3-1.centos.6.x86_64.rpm

maxscale的配置文件為:/etc/maxscale.conf,配置文件由多個(gè)配置模塊組成

vi /etc/maxscale.conf

#全局配置:
[maxscale]
threads=auto
log_info=1
log_notice=1
log_warning=1

#后端mysql定義
[server1]
type=server
address=192.168.10.1
port=3306
protocol=MySQLBackend
[server2]
type=server
address=192.168.10.2
port=3306
protocol=MySQLBackend

#監(jiān)控配置
[MySQL Monitor] 
type=monitor
module=mysqlmon      #監(jiān)控模塊默認(rèn)使用mysqlmon,當(dāng)然還有其他可選擇的模塊
servers=server1,server2
user=admin        #admin監(jiān)控后端mysql的復(fù)制狀況,必須具有REPLICATION CLIENT權(quán)限      
passwd=123456
monitor_interval=10000

#觸發(fā)器定義 注意,以下兩句的上下順序不能變,不然無法生效
script=/opt/mysql_monitor.sh  #定義事件觸發(fā)腳本執(zhí)行
events=master_down       #事件觸發(fā)器當(dāng)master down時(shí),執(zhí)行上面的腳本,maxscale內(nèi)置了很
                                 多的events
#讀寫分離service
[Read-Write Service] 
type=service
router=readwritesplit       #讀寫分離路由模式
servers=server1,server2
user=maxscale    #該用戶從后端mysql獲取用戶信息,對客戶端進(jìn)行身份驗(yàn)證,必須具有mysql.user table的select權(quán)限                
passwd=maxscale
max_slave_connections=100%                   
max_slave_replication_lag=3600000   #當(dāng)slave的數(shù)據(jù)落后master小于3600秒時(shí)仍然可用
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=listener
service=Read-Write Service
protocol=MySQLClient
port=3306

#管理服務(wù)配置 
[MaxAdmin Service]
type=service
router=cli

#管理服務(wù)監(jiān)聽
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default

二、管理

Maxscale提供了maxadmin命令用于查看管理

[root@server ~]# 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
[root@server ~]# maxadmin list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.10.1      |  3306 |           0 | Master, Running
server2            | 192.168.10.2     |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------

三、高可用性

Maxscale默認(rèn)只提供讀的高可用性,要實(shí)現(xiàn)寫的高可用性,可以使用兩種途徑:

1.需要使用Multi-MasterMonitor監(jiān)控模塊,不同于上文使用的mysqlmon模塊,該模塊是通過read_only參數(shù)來選舉Master和Slave,結(jié)合腳本可以實(shí)現(xiàn)在Master fail的時(shí)候,取消slave的read_only屬性,將slave提升為Master

2.使用高可用軟件MMM

當(dāng)前題目:使用Maxscale實(shí)現(xiàn)mysql讀寫分離
網(wǎng)址分享:http://bm7419.com/article42/igegec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、軟件開發(fā)、企業(yè)建站、網(wǎng)站策劃、電子商務(wù)、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)