寫在前面
- 分享一些MySQL(MariaDB)集群主從結(jié)構(gòu)數(shù)據(jù)讀寫分離的筆記,關(guān)于讀寫分離:
- 一如果對于讀密集型應(yīng)用,可以容忍從庫異步復(fù)制延遲導(dǎo)致的臟數(shù)據(jù),讀寫分離是一種不錯的負(fù)載均衡方式
- 如果對于臟數(shù)據(jù)零容忍,不建議這樣搞,出了故障還需要考慮這個因素,不太方便定位問題
- 二是讀寫分離需要做體量評估,不能為了讀寫分離去讀寫分離,系統(tǒng)負(fù)載正常,完全沒必要,如果擴(kuò)了資源還是頻繁的sql timeout,讀寫分離是解決方法之一
- 博文偏實(shí)戰(zhàn),內(nèi)容涉及: 為什么需要負(fù)載均衡?MaxScale配置主從集群的讀寫分離
- 食用方式:了解linux,MySQL
- 理解不足小伙伴幫忙指正
「 只要足夠開心,煩惱就追不上哦 ^_^ 」
一、為什么需要負(fù)載均衡?
需要負(fù)載均衡的理由:
- 「可擴(kuò)展性」:負(fù)載均衡對某些擴(kuò)展策略有所幫助,比如流量控制,例如數(shù)據(jù)庫讀寫流量分離時從備庫讀數(shù)據(jù),降低主庫讀的工作負(fù)載,提升寫的負(fù)載能力,在比如大型的Web應(yīng)用,對于搜索類請求需要最小化延遲,可以負(fù)載到最近的數(shù)據(jù)中心,對于上傳來講需要最大化吞吐量,需要負(fù)載到帶寬沒有占滿的鏈路,即使跳的遠(yuǎn)一點(diǎn)。
- 「高效性」:負(fù)載均衡有助于更有效地使用資源,控制流量被路由到何處。如果服務(wù)器處理能力各不相同,這就尤為重要:你可以把更多的工作分配給性能更好的機(jī)器。
- 「高可用性」: 一個靈活的負(fù)載均衡解決方案能夠使用時刻保持可用的服務(wù)器。
- 「匿名性」: 客戶端無須知道是否存在負(fù)載均衡設(shè)置。負(fù)載均衡器給客戶端看到的只是一個代理一個虛擬服務(wù)器。
- 「一致性」: 如果應(yīng)用是有狀態(tài)的(數(shù)據(jù)庫事務(wù),網(wǎng)站會話等),那么負(fù)載均衡器就應(yīng)將相關(guān)的查詢指向同一個服務(wù)器,以防止?fàn)顟B(tài)丟失。應(yīng)用無須去跟蹤到底連接的是哪個服務(wù)器。

從集群角度考慮,MySQL做主備集群復(fù)制如果只用作備份,有些浪費(fèi),和負(fù)載均衡結(jié)合使用一種相輔相成的作用。
所以MySQL的負(fù)載均衡架構(gòu)通常和數(shù)據(jù)分片及復(fù)制緊密相關(guān)。我們今天要講的讀/寫分離策略即屬于負(fù)載均衡的一個實(shí)現(xiàn)。
有些負(fù)載均衡技術(shù)本身能夠?qū)崿F(xiàn)這一點(diǎn),有些需要應(yīng)用自己知道哪些節(jié)點(diǎn)是可讀的或可寫的。
客戶端讀寫分離
常見的讀寫分離一種是通過客戶端去區(qū)分讀寫,比如上面那個圖,寫在主庫,讀通過負(fù)載均衡到多個從庫。
在應(yīng)用層面粗粒度通過配置不同數(shù)據(jù)源分離讀寫實(shí)現(xiàn),同時還需要考慮從庫異步復(fù)制導(dǎo)致的臟數(shù)據(jù)問題,需要監(jiān)控延遲復(fù)制來決策讀寫的分配。可以考慮在編碼層次基于查詢,版本,請求時間戳,會話等做一些讀寫策略,不能容忍臟數(shù)據(jù)的讀可以放到寫節(jié)點(diǎn)
從庫的負(fù)載可以通過DNS負(fù)載、LVS+Keepalived、硬件負(fù)載均衡器F5、TCP代理(HAproxy,Nginx)、或者在應(yīng)用中管理從庫負(fù)載均衡。比如做簡單的數(shù)據(jù)源池做線性負(fù)載等。
服務(wù)端讀寫分離
另一種是通過在服務(wù)端去區(qū)分,通過MySQL Proxy的方式實(shí)現(xiàn)。客戶端的請求都到MySQL Proxy上,如果客戶端要執(zhí)行查詢數(shù)據(jù)的請求,則交給從服務(wù)器來處理;如果客戶端要對數(shù)據(jù)進(jìn)行增、刪、改的操作,則交給主服務(wù)器來處理;
MySQL Proxy相關(guān)的工具有很多,有自帶的mysql-proxy插件,也有MyCat等中間件,今天和小伙伴分享通過MaxScale來實(shí)現(xiàn)的讀寫分離,不管使用那種方式,個人覺得如果對于臟數(shù)據(jù)零容忍的應(yīng)用更多的應(yīng)該在硬件資源上面考慮,并且大多數(shù)的讀寫分離解決方案都需要監(jiān)控延遲復(fù)制來決策讀寫的分配。做的不好,總感覺有點(diǎn)不靠譜...
二、配置主從集群的讀寫分離

MariaDB MaxScale是MariaDB企業(yè)服務(wù)器、MariaDB ColumnStore和MariaDB Xpand的高級數(shù)據(jù)庫代理,為它們提供企業(yè)高可用性、可伸縮性、安全和集成服務(wù),同時抽象出底層數(shù)據(jù)庫基礎(chǔ)設(shè)施,以簡化應(yīng)用程序開發(fā)和數(shù)據(jù)庫管理。
官方地址:https://mariadb.com/
讀寫分離工作原理
由 MaxScale面向客戶端提供服務(wù),收到SQL寫請求時,交給master 服務(wù)器處理,收到SQL讀請求時,交給slave服務(wù)器處理,這里我們已經(jīng)搭建好一個主從結(jié)構(gòu)的MySQL集群,關(guān)于集群搭建小伙伴可以看我之前的文章,有詳細(xì)教程,所以這里只需要安裝MaxScale,然后配置啟動測試就OK

- MaxScale代理服務(wù):192.168.26.152
- 主庫:192.168.26.153(寫)
- 從庫:192.168.26.154(讀)
- 客戶端: 192.168.26.155
可以參考官方文檔:https://mariadb.com/docs/deploy/topologies/primary-replica/enterprise-server-10-6/install-mxs/
安裝包下載: https://downloads.mariadb.com/files/MaxScale
需要的依賴包下載:https://rpmfind.NET/linux/rpm2html/search.php
依賴安裝┌──[[email protected]]-[~]└─$yum -y install libatomic軟件包 libatomic-4.8.5-44.el7.x86_64 已安裝并且是最新版本無須任何處理┌──[[email protected]]-[~]└─$wget https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/trousers-0.3.14-2.el7.x86_64.rpm--2022-09-29 20:13:22-- https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/trousers-0.3.14-2.el7.x86_64.rpm┌──[[email protected]]-[~]└─$rpm -ivh trousers-0.3.14-2.el7.x86_64.rpm準(zhǔn)備中... ################################# [100%]正在升級/安裝...1:trousers-0.3.14-2.el7 ################################# [100%]┌──[[email protected]]-[~]└─$┌──[[email protected]]-[~]└─$wget https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/nettle-2.7.1-8.el7.x86_64.rpm--2022-09-29 20:14:52-- https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/nettle-2.7.1-8.el7.x86_64.rpm┌──[[email protected]]-[~]└─$rpm -ivh nettle-2.7.1-8.el7.x86_64.rpm準(zhǔn)備中... ################################# [100%]正在升級/安裝...1:nettle-2.7.1-8.el7 ################################# [100%]┌──[[email protected]]-[~]└─$┌──[[email protected]]-[~]└─$wget https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/gnutls-3.3.29-9.el7_6.x86_64.rpm--2022-09-29 20:15:50-- https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/gnutls-3.3.29-9.el7_6.x86_64.rpm┌──[[email protected]]-[~]└─$rpm -ivh gnutls-3.3.29-9.el7_6.x86_64.rpm準(zhǔn)備中... ################################# [100%]正在升級/安裝...1:gnutls-3.3.29-9.el7_6 ################################# [100%]┌──[[email protected]]-[~]└─$
安裝完依賴包安裝 maxscale
安裝 MaxScale┌──[[email protected]]-[~]└─$rpm -ivh maxscale-2.5.21-1.rhel.7.x86_64.rpm警告:maxscale-2.5.21-1.rhel.7.x86_64.rpm: 頭V4 RSA/SHA512 Signature, 密鑰 ID e3c94f49: NOKEY準(zhǔn)備中... ################################# [100%]正在升級/安裝...1:maxscale-2.5.21-1.rhel.7 ################################# [100%]Created symlink from /etc/systemd/system/multi-user.target.wants/maxscale.service to /usr/lib/systemd/system/maxscale.service.┌──[[email protected]]-[~]└─$配置 MaxScale┌──[[email protected]]-[~]└─$cp /etc/maxscale.cnf /etc/maxscale.cnf.bak┌──[[email protected]]-[~]└─$vim /etc/maxscale.cnf
備份配置文件,然后修改
┌──[[email protected]]-[~]└─$cat /etc/maxscale.cnf# MaxScale documentation:# https://mariadb.com/kb/en/mariadb-maxscale-25/# Global parameters# Complete list of configuration options:# https://mariadb.com/kb/en/mariadb-maxscale-25-mariadb-maxscale-configuration-guide/[maxscale]# 定義線程的個數(shù),一個線程對應(yīng)一個CPU核心數(shù),線程數(shù)量要小于等于CPU核心數(shù)threads=auto# Server definitions# Set the address of the server to the network# address of a MariaDB server.# 指定要代理的數(shù)據(jù)庫服務(wù)器信息[server1]type=serveraddress=192.168.26.153 #需要數(shù)據(jù)庫服務(wù)器IPport=3306protocol=MariaDBBackend[server2]type=serveraddress=192.168.26.154port=3306protocol=MariaDBBackend# Monitor for the servers# This will keep MaxScale aware of the state of the servers.# MariaDB Monitor documentation:# https://mariadb.com/kb/en/maxscale-25-monitors/# 定義要監(jiān)控的數(shù)據(jù)庫服務(wù)器[MariaDB-Monitor]type=monitormodule=mariadbmonservers=server1,server2#指定監(jiān)控用戶maxscalemon,用于登錄后端服務(wù)器,檢查服務(wù)器的運(yùn)行狀態(tài)和主從狀態(tài)user=maxscalemonpassword=liruilongmonitor_interval=10000# Service definitions# Service Definition for a read-only service and# a read/write splitting service.# ReadConnRoute documentation:# https://mariadb.com/kb/en/mariadb-maxscale-25-readconnroute/#定義只讀服務(wù)器配置,需要注釋掉,我們配置的是讀寫分離#[Read-Only-Service]#type=service#router=readconnroute#servers=server1#user=myuser#password=mypwd#router_options=slave# ReadWriteSplit documentation:# https://mariadb.com/kb/en/mariadb-maxscale-25-readwritesplit/#定義讀寫分離服務(wù)器配置[Read-Write-Service]type=servicerouter=readwritesplitservers=server1,server2 ##指定做讀寫分離服務(wù)的數(shù)據(jù)庫服務(wù)器user=maxscaleroutepassword=liruilong##客戶端通過用戶名和密碼向數(shù)據(jù)庫發(fā)送請求,先訪問maxscale代理服務(wù)器,再由代理服務(wù)器將請求轉(zhuǎn)發(fā)##給后端數(shù)據(jù)庫服務(wù)器;maxscale代理服務(wù)器是通過路由用戶連接后端服務(wù)器,檢測客戶端的用戶名和密碼在##后端數(shù)據(jù)庫中是否存在# 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=MariaDBClient#port=4008# 定義讀寫分離服務(wù)使用的端口號[Read-Write-Listener]type=listenerservice=Read-Write-Serviceprotocol=MariaDBClientport=4006┌──[[email protected]]-[~]└─$創(chuàng)建監(jiān)控用戶和路由用戶
創(chuàng)建監(jiān)控用戶maxscalemon,用于登錄后端服務(wù)器,檢查服務(wù)器的狀態(tài)
grant replication slave,replication client on *.* to maxscalemon@"%" identified by "liruilong";
- replication slave 能夠同步數(shù)據(jù),查看從服務(wù)器上slave的狀態(tài);
- replication client 可以獲取數(shù)據(jù)庫服務(wù)的狀態(tài)(數(shù)據(jù)庫服務(wù)是否允許,主從是否正常)
MariaDB [(none)]> grant replication slave,replication client on *.* to maxscalemon@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)
創(chuàng)建路由用戶maxscalerouter,檢測客戶端的用戶名和密碼在后端數(shù)據(jù)庫中是否存在
- 只是檢查用戶是否存在,所以此用戶只需對mysql庫下表有查詢權(quán)限即可
MariaDB [(none)]> grant select on mysql.* to maxscaleroute@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)
在mysql庫的user表中,查看監(jiān)控用戶和路由用戶
MariaDB [(none)]> select user,host from mysql.user;| user | host || maxscalemon | % || maxscaleroute | % || repluser | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms153.liruilongs.github.io |7 rows in set (0.00 sec)MariaDB [(none)]>
在154從庫的mysql庫的user表中,查看同步過去監(jiān)控用戶和路由用戶
┌──[[email protected]]-[~]└─$mysql -uroot -pliruilong -e'select user,host from mysql.user;'| user | host || maxscalemon | % || maxscaleroute | % || tom | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms154.liruilongs.github.io |┌──[[email protected]]-[~]└─$
啟動MaxScale服務(wù)
┌──[[email protected]]-[~]└─$maxscale -f /etc/maxscale.cnf -U maxscale┌──[[email protected]]-[~]└─$netstat -ntulp | grep maxscaletcp 0 0 127.0.0.1:8989 0.0.0.0:* LISTEN 1169/maxscaletcp6 0 0 :::4006 :::* LISTEN 1169/maxscale┌──[[email protected]]-[~]└─$ps -C maxscalePID TTY TIME CMD1169 ? 00:00:00 maxscale
查看日志,配置文件有個報(bào)錯,解決下
┌──[[email protected]]-[~]└─$tail -n 5 /var/log/maxscale/maxscale.log2022-09-29 22:38:24 error : Monitor timed out when connecting to server server2[192.168.26.26.154:3306] : 'Unknown MySQL server host '192.168.26.26.154' (-2)'2022-09-29 22:38:24 notice : [mariadbmon] Selecting new master server.2022-09-29 22:38:24 warning: [mariadbmon] No running master candidates detected and no master currently set. Accepting a non-running server as master.2022-09-29 22:38:24 notice : [mariadbmon] Setting 'server1' as master.2022-09-29 22:39:15 warning: [mariadbmon] The current master server 'server1' is no longer valid because it has been down over 5 (failcount) monitor updates and it does not have any running slaves, but there is no valid alternative to swap to.┌──[[email protected]]-[~]└─$kill -9 1169┌──[[email protected]]-[~]└─$vim /etc/maxscale.cnf┌──[[email protected]]-[~]└─$maxscale -f /etc/maxscale.cnf -U maxscale┌──[[email protected]]-[~]└─$測試 MaxScale檢查全局配置
使用maxctrl show maxscale命令查看全局maxscale配置。
┌──[[email protected]]-[~]└─$maxctrl show maxscale┌──────────────┬───────────────────────────────────────────────────────┐│ Version │ 2.5.21 │├──────────────┼───────────────────────────────────────────────────────┤│ Commit │ eb659891d7b507958f3c5f100d1ebe5f0f68afaf │├──────────────┼───────────────────────────────────────────────────────┤│ Started At │ Sun, 09 Oct 2022 14:50:14 GMT │├──────────────┼───────────────────────────────────────────────────────┤│ Activated At │ Sun, 09 Oct 2022 14:50:14 GMT │├──────────────┼───────────────────────────────────────────────────────┤│ Uptime │ 43 │├──────────────┼───────────────────────────────────────────────────────┤│ Parameters │ { ││ │ "admin_auth": true, ││ │ "admin_enabled": true, ││ │ "admin_gui": true, ││ │ "admin_host": "127.0.0.1", ││ │ "admin_log_auth_failures": true, ││ │ "writeq_low_water": 8192 ││ │ } │└──────────────┴───────────────────────────────────────────────────────┘┌──[[email protected]]-[~]└─$檢查服務(wù)器配置
使用maxctrl list servers和maxctrl show server命令查看配置的服務(wù)器對象。
獲取服務(wù)器對象的完整列表:
- server1 :192.168.26.153:3306 ││ Master, Running
- server2 :192.168.26.154:3306 ││ Slave, Running
┌──[[email protected]]-[~]└─$maxctrl list servers┌─────────┬────────────────┬──────┬─────────────┬─────────────────┬──────┐│ Server │ Address │ Port │ Connections │ State │ GTID │├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┤│ server1 │ 192.168.26.153 │ 3306 │ 0 │ Master, Running │ │├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┤│ server2 │ 192.168.26.154 │ 3306 │ 0 │ Slave, Running │ │└─────────┴────────────────┴──────┴─────────────┴─────────────────┴──────┘┌──[[email protected]]-[~]└─$
對于每一個服務(wù)器對象,查看配置:
┌──[[email protected]]-[~]└─$maxctrl show server server1┌─────────────────────┬───────────────────────────────────────────┐│ Server │ server1 │├─────────────────────┼───────────────────────────────────────────┤│ Address │ 192.168.26.153 │├─────────────────────┼───────────────────────────────────────────┤│ Port │ 3306 │├─────────────────────┼───────────────────────────────────────────┤│ State │ Master, Running │├─────────────────────┼───────────────────────────────────────────┤│ Version │ 5.5.68-MariaDB │├─────────────────────┼───────────────────────────────────────────┤│ Last Event │ master_up │├─────────────────────┼───────────────────────────────────────────┤│ Triggered At │ Sun, 09 Oct 2022 14:50:14 GMT │├─────────────────────┼───────────────────────────────────────────┤│ Services │ Read-Write-Service │├─────────────────────┼───────────────────────────────────────────┤│ Monitors │ MariaDB-Monitor │├─────────────────────┼───────────────────────────────────────────┤│ Master ID │ -1 │├─────────────────────┼───────────────────────────────────────────┤│ Node ID │ 153 │├─────────────────────┼───────────────────────────────────────────┤│ Slave Server IDs │ │├─────────────────────┼───────────────────────────────────────────┤│ Current Connections │ 0 │├─────────────────────┼───────────────────────────────────────────┤│ Total Connections │ 0 │├─────────────────────┼───────────────────────────────────────────┤│ Max Connections │ 0 │├─────────────────────┼───────────────────────────────────────────┤│ Statistics │ { ││ │ "active_operations": 0, ││ │ "total_connections": 0 ││ │ } │├─────────────────────┼───────────────────────────────────────────┤│ Parameters │ { ││ │ "address": "192.168.26.153", ││ │ "ssl_version": "MAX" ││ │ } │└─────────────────────┴───────────────────────────────────────────┘┌──[[email protected]]-[~]└─$maxctrl show server server2檢查監(jiān)控配置
使用maxctrl list monitors和maxctrl show monitor命令查看已配置的監(jiān)視器。
獲取監(jiān)控器的完整列表:
┌──[[email protected]]-[~]└─$maxctrl list monitors┌─────────────────┬─────────┬──────────────────┐│ Monitor │ State │ Servers │├─────────────────┼─────────┼──────────────────┤│ MariaDB-Monitor │ Running │ server1, server2 │└─────────────────┴─────────┴──────────────────┘
對于每個監(jiān)控,查看監(jiān)控配置:
┌──[[email protected]]-[~]└─$maxctrl show monitor MariaDB-Monitor┌─────────────────────┬──────────────────────────────────────────────────────┐│ Monitor │ MariaDB-Monitor │├─────────────────────┼──────────────────────────────────────────────────────┤│ Module │ mariadbmon │├─────────────────────┼──────────────────────────────────────────────────────┤│ State │ Running │├─────────────────────┼──────────────────────────────────────────────────────┤│ Servers │ server1 ││ │ server2 │├─────────────────────┼──────────────────────────────────────────────────────┤│ Parameters │ { ││ │ "assume_unique_hostnames": true, ││ │ "slave_conditions": "none", ││ │ "switchover_on_low_disk_space": false, ││ │ "switchover_timeout": 90, ││ │ "user": "maxscalemon", ││ │ "verify_master_failure": true ││ │ } │├─────────────────────┼──────────────────────────────────────────────────────┤│ Monitor Diagnostics │ { ││ │ "master": "server1", ││ │ "master_gtid_domain_id": null, ││ │ "primary": null, ││ │ "server_info": [ ││ │ { ││ │ "gtid_binlog_pos": null, ││ │ "last_sql_error": "", ││ │ "master_host": "192.168.26.153", ││ │ "master_port": 3306, ││ │ "master_server_id": 153, ││ │ "seconds_behind_master": 0, ││ │ "slave_io_running": "Yes", ││ │ "slave_sql_running": "Yes" ││ │ } ││ │ ] ││ │ } ││ │ ], ││ │ "state": "Idle" ││ │ } │└─────────────────────┴──────────────────────────────────────────────────────┘┌──[[email protected]]-[~]└─$檢查服務(wù)配置
使用maxctrl list services和maxctrl show service命令查看已配置的路由服務(wù)。
獲取路由服務(wù)的完整列表:
┌──[[email protected]]-[~]└─$maxctrl list services┌────────────────────┬────────────────┬─────────────┬───────────────────┬──────────────────┐│ Service │ Router │ Connections │ Total Connections │ Targets │├────────────────────┼────────────────┼─────────────┼───────────────────┼──────────────────┤│ Read-Write-Service │ readwritesplit │ 0 │ 0 │ server1, server2 │└────────────────────┴────────────────┴─────────────┴───────────────────┴──────────────────┘
查看詳細(xì)信息
┌──[[email protected]]-[~]└─$maxctrl show services┌─────────────────────┬─────────────────────────────────────────────────────────────┐│ Service │ Read-Write-Service │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Router │ readwritesplit │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ State │ Started │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Started At │ Sun Oct 9 22:50:17 2022 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Current Connections │ 0 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Total Connections │ 0 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Max Connections │ 0 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Cluster │ │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Servers │ server1 ││ │ server2 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Services │ │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Filters │ │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Parameters │ { ││ │ "auth_all_servers": false, ││ │ "causal_reads": "false", ││ │ "causal_reads_timeout": 10000, ││ │ "transaction_replay_max_size": "1073741824", ││ │ "transaction_replay_retry_on_deadlock": false, ││ │ "use_sql_variables_in": "all", ││ │ "user": "maxscaleroute", ││ │ "version_string": null ││ │ } │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Router Diagnostics │ { ││ │ "queries": 0, ││ │ "server_query_statistics": [] ││ │ } │└─────────────────────┴─────────────────────────────────────────────────────────────┘┌──[[email protected]]-[~]└─$檢查讀寫分離
創(chuàng)建一個普通用戶用于測試,并且授權(quán)數(shù)據(jù)庫liruilong_db
┌──[[email protected]]-[~]└─$mysql -uroot -pliruilongWelcome to the MariaDB monitor. Commands end with ; or g.Your MariaDB connection id is 50Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.MariaDB [(none)]> grant all on liruilong_db.* to liruilong@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> select user,host from mysql.user;| user | host || liruilong | % || maxscalemon | % || maxscaleroute | % || repluser | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms153.liruilongs.github.io |8 rows in set (0.00 sec)MariaDB [(none)]>
登錄測試
┌──[[email protected]]-[~]└─$mysql -uliruilong -pliruilongWelcome to the MariaDB monitor. Commands end with ; or g.Your MariaDB connection id is 51Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.MariaDB [(none)]> show grants;| Grants for liruilong@% || GRANT USAGE ON *.* TO 'liruilong'@'%' IDENTIFIED BY PASSWORD '*73CA7DD1B0BD11DCA665AB9C635C2188533331B3' || GRANT ALL PRIVILEGES ON `liruilong_db`.* TO 'liruilong'@'%' |2 rows in set (0.00 sec)MariaDB [(none)]>
從庫查看是否同步
┌──[[email protected]]-[~]└─$mysql -uroot -pliruilong -e'select user,host from mysql.user;'| user | host || liruilong | % || maxscalemon | % || maxscaleroute | % || tom | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms154.liruilongs.github.io |┌──[[email protected]]-[~]└─$
代理機(jī)器上查看路由端口
┌──[[email protected]]-[~]└─$ maxctrl list listeners┌─────────────────────┬──────┬──────┬─────────┬────────────────────┐│ Name │ Port │ Host │ State │ Service │├─────────────────────┼──────┼──────┼─────────┼────────────────────┤│ Read-Write-Listener │ 4006 │ :: │ Running │ Read-Write-Service │└─────────────────────┴──────┴──────┴─────────┴────────────────────┘┌──[[email protected]]-[~]└─$
客戶機(jī)連接MaxScale代理,查到登錄用戶測試庫等信息
┌──[[email protected]]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'show grants'| Grants for liruilong@% || GRANT USAGE ON *.* TO 'liruilong'@'%' IDENTIFIED BY PASSWORD '*73CA7DD1B0BD11DCA665AB9C635C2188533331B3' || GRANT ALL PRIVILEGES ON `liruilong_db`.* TO 'liruilong'@'%' |┌──[[email protected]]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'show databases;'| Database || information_schema || liruilong_db || test |┌──[[email protected]]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;show tables;'| Tables_in_liruilong_db || user |
客戶機(jī)連接MaxScale代理,通過代理插入一條數(shù)據(jù)
┌──[[email protected]]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;insert into user values(2,now());'┌──[[email protected]]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 |┌──[[email protected]]-[~]└─$
可以正常讀取,下面在從(讀)庫插入一條數(shù)據(jù)
┌──[[email protected]]-[~]└─$mysql -h192.168.26.154 -uliruilong -pliruilong -e'use liruilong_db;insert into user values(154,now());'┌──[[email protected]]-[~]└─$mysql -h192.168.26.154 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 154 | 2022-09-30 13:32:18 |
主庫沒有數(shù)據(jù),但是通過代理讀到了數(shù)據(jù),說明讀是通過從讀取。
┌──[[email protected]]-[~]└─$mysql -h192.168.26.153 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 |┌──[[email protected]]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 154 | 2022-09-30 13:32:18 |
通過代理插入數(shù)據(jù),主從庫數(shù)據(jù)同時存在,說明寫是在主庫,然后同步給從庫
┌──[[email protected]]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;insert into user values(152,now());'┌──[[email protected]]-[~]└─$mysql -h192.168.26.153 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 152 | 2022-09-30 13:34:26 |┌──[[email protected]]-[~]└─$mysql -h192.168.26.154 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 154 | 2022-09-30 13:32:18 || 152 | 2022-09-30 13:34:26 |┌──[[email protected]]-[~]└─$博文參考
《高性能Mysql》第三版(High Performance MySQL,Third Edition)
https://mariadb.com/docs/deploy/topologies/primary-replica/enterprise-server-10-6/






