溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于maxscale的讀寫分離部署筆記

發(fā)布時間:2020-06-01 04:52:00 來源:網(wǎng)絡 閱讀:25089 作者:我的二狗呢 欄目:MySQL數(shù)據(jù)庫



使用maxscale搭建的讀寫分離架構,后期還可以再結合MHA做master的故障轉移,這樣業(yè)務層面上不需要做任何的改動即可。


基于maxscale的讀寫分離部署筆記

基于maxscale的讀寫分離部署筆記

基于maxscale的讀寫分離部署筆記



基于connect方式的不要使用。從庫延遲他還會繼續(xù)分發(fā)請求過去,暫時不適合生產(chǎn)使用。

 

 

 

 

 

 

實驗演示:

目前的主從結構:

node93     10.1.20.93 master

node94  10.1.20.94 slave

node95  10.1.20.95 slave

node96  10.1.20.96 maxscale

 

 

先在master主庫上創(chuàng)建相關的賬戶:

在開始配置之前,需要在 master中為MaxScale 創(chuàng)建兩個用戶,用于監(jiān)控模塊和路由模塊。

 

創(chuàng)建監(jiān)控用戶,用于[MySQL Monitor]段的配置中:

> create database maxscale_schema ;    # maxscale監(jiān)控用的心跳信息會寫到這個庫里面

> create user scalemon@'%' identified by"111111";

> grant replication slave, replication client on*.* to scalemon@'%';

> grant all on maxscale_schema.* to scalemon@'%';

 

創(chuàng)建路由用戶,用于[Read-Write Service]段的配置中:

> create user maxscale@'%' identified by"111111";

> grant select on mysql.* to maxscale@'%';

 

 

maxscale 部署:

rpm -ivh maxscale-2.0.5-1.rhel.6.x86_64.rpm

 

主要生成文件如下:

/etc/maxscale.cnf

/etc/maxscale.cnf.template

/usr/bin/cdc.py

/usr/bin/cdc_kafka_producer.py

/usr/bin/cdc_last_transaction.py

/usr/bin/cdc_users.py

/usr/bin/maxadmin

/usr/bin/maxavrocheck

/usr/bin/maxbinlogcheck

/usr/bin/maxkeys

/usr/bin/maxpasswd

/usr/bin/maxscale

/var

/var/lib

/var/lib/maxscale

 

 

 

 

創(chuàng)建秘鑰文件:

[root@maxscale /root ]# maxkeys /var/lib/maxscale

生成加密后的密碼:

[root@maxscale /root ]# maxpasswd/var/lib/maxscale/.secrets 123456

上面劃掉的這2步驟我們用不到,直接在配置文件/etc/maxscale.cnf里寫上明文密碼就行了。(這里踩了坑,配置這個參數(shù),導致后面maxscale起來后,無法連接到其他庫提示access denied

 

 

vim/etc/security/limits.conf

    * softnofile 65535 

    * hardnofile 65535

 

vim/etc/sysctl.conf :

    fs.file-max=655350

    net.ipv4.ip_local_port_range= 1025 65000

    net.ipv4.tcp_tw_reuse= 1

 

修改完內(nèi)和參數(shù)后,需要重啟下服務器。

 

修改配置文件:

cat /etc/maxscale.cnf

[maxscale]

threads=auto

 

ms_timestamp=1             #timestamp精度 

syslog=1                   #將日志寫入到syslog   

maxlog=1                   #將日志寫入到maxscale的日志文件中 

log_to_shm=0               #不將日志寫入到共享緩存中,開啟debug模式時可打開加快速度 

log_warning=1              #記錄告警信息 

log_notice=1               #記錄notice 

log_info=1                 #記錄info 

log_debug=0                #不打開debug模式 

log_augmentation=1         #日志遞增 

 

# Server definitions

#

# Set the address of the server to the network

# address of a MySQL server.

#

 

# 需要把masterslave地址都配上,maxscale會自動分辨出哪個是masterslave

[server1]

type=server

address=10.1.20.93

port=3306

protocol=MySQLBackend

 

[server2]

type=server

address=10.1.20.94

port=3306

protocol=MySQLBackend

 

[server3]

type=server

address=10.1.20.95

port=3306

protocol=MySQLBackend

 

# Monitor for the servers

#

# This will keep MaxScale aware of the state of theservers.

# MySQL Monitor documentation:

# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Monitors/MySQL-Monitor.md

 

[MySQL Monitor]

type=monitor

module=mysqlmon

servers=server1,server2,server3    # 這里要把全部server都寫上,以便maxscale去監(jiān)測

user=scalemon 

passwd=111111  

monitor_interval=10000    # 每隔10s檢查一次

detect_replication_lag=true     # 檢查復制延遲的情況

detect_stale_master=true       # 當所有的slave都不可用時,select查詢請求會轉發(fā)到master。

 

# Service definitions

#

# Service Definition for a read-only service and

# a read/write splitting service.

#

 

# ReadConnRoute documentation:

#https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadConnRoute.md

 

由于我們使用了 [Read-Write Service],可以刪除另一個服務[Read-Only Service],注釋掉下面整塊兒內(nèi)容即可。

# 需要把masterslave地址都配上

#[Read-Only Service]

#type=service

#router=readconnroute

#servers=server1,server2,server3

#user=maxscale           # 讀寫分離的賬戶和密碼

#passwd=111111           # 讀寫分離的賬戶和密碼

#router_options=slave

 

# ReadWriteSplit documentation:

# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadWriteSplit.md

 

# 配置的讀寫分離,需要把masterslave地址都配上

[Read-Write Service]

type=service

router=readwritesplit

servers=server1,server2,server3

user=maxscale        # 讀寫分離的賬戶和密碼

passwd=111111       # 讀寫分離的賬戶和密碼

 

max_slave_connections=100%         # 所有的slave提供select查詢服務

max_slave_replication_lag = 5      # slave超時5秒,就把請求轉發(fā)到其他slave

use_sql_variables_in = all         #

 

# This service enables the use of the MaxAdmininterface

# MaxScale administration guide:

#https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Reference/MaxAdmin.md

 

[MaxAdmin Service]

type=service

router=cli

 

# Listener definitions for the services

#

# These listeners represent the ports the

# services will listen on.

#

 

#[Read-Only Listener]

#type=listener

#service=Read-Only Service

#protocol=MySQLClient

#port=4008

 

[Read-Write Listener]

type=listener

service=Read-Write Service

protocol=MySQLClient

port=4006

 

 

[MaxAdmin Listener]

type=listener

service=MaxAdmin Service

protocol=maxscaled

socket=default

 

 

啟動maxscale

maxscale -f/etc/maxscale.cnf

 

ss -lnt 可以看到4006端口啟動了。

可以使用之前的業(yè)務賬號連接到maxscale4006端口上,例如:

mysql -utest -ptest -P 4006 -h 10.1.20.96 

基于maxscale的讀寫分離部署筆記


注意begin;select @@hostname;commit;這種的select會在主庫上執(zhí)行。此外,執(zhí)行存儲過程或者函數(shù)時候也是會自動在主庫去執(zhí)行的。

 

執(zhí)行SQL  > begin;select@@hostname; commit; insert into t2 select 3; select @@hostname; 對應的在/var/log/maxscale/maxscale.log記錄如下:

基于maxscale的讀寫分離部署筆記

很明顯的可以看:開啟事務、插入等操作會被轉發(fā)到主庫去處理。而單純的select則會被轉發(fā)到某個從庫去處理。

 

 

maxscale不能對master進行故障切換,可以配合使用MHA來進行。MHA的故障切換后,maxscale可以自動識別哪臺機器是master。然后自動將求發(fā)送到新的主從結構中。

 

 

maxscale的延遲檢測:

pt-heartbeat的原理類似。maxscale會對masterslavereplication_heartbeat表的master_timestamp時間戳進行對比,相減得出差異。這個差異就是MySQL主從同步的延遲值。

 

select * frommaxscale_schema.replication_heartbeat;

基于maxscale的讀寫分離部署筆記



從庫故障或延遲過大會被自動剔除:

我們可以在192.168.2.12stop slave; 稍等片刻,再執(zhí)行查詢操作的話,會發(fā)現(xiàn)請求不會被轉發(fā)到192.168.2.12mysql上,maxscale里面自動將這個機器踢下線了。如下圖:

基于maxscale的讀寫分離部署筆記


192.168.2.12 出現(xiàn)延遲,則在maxscale日志顯示如下:基于maxscale的讀寫分離部署筆記


192.168.2.12 3306端口不通(可能mysqld掛掉或者服務器宕機),則在maxscale日志顯示如下:

基于maxscale的讀寫分離部署筆記




其他后補充進筆記的,管理maxscale的一些命令

maxadmin -S /tmp/maxadmin.sock 

基于maxscale的讀寫分離部署筆記

基于maxscale的讀寫分離部署筆記

基于maxscale的讀寫分離部署筆記

基于maxscale的讀寫分離部署筆記

基于maxscale的讀寫分離部署筆記




向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI