日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

寫在前面

 

  • 分享一些MySQL(MariaDB)集群主從結構數據讀寫分離的筆記,關于讀寫分離:
  • 一如果對于讀密集型應用,可以容忍從庫異步復制延遲導致的臟數據,讀寫分離是一種不錯的負載均衡方式
  • 如果對于臟數據零容忍,不建議這樣搞,出了故障還需要考慮這個因素,不太方便定位問題
  • 二是讀寫分離需要做體量評估,不能為了讀寫分離去讀寫分離,系統負載正常,完全沒必要,如果擴了資源還是頻繁的sql timeout,讀寫分離是解決方法之一
  • 博文偏實戰,內容涉及: 為什么需要負載均衡?MaxScale配置主從集群的讀寫分離
  • 食用方式:了解linux,MySQL
  • 理解不足小伙伴幫忙指正

 

「 只要足夠開心,煩惱就追不上哦 ^_^ 」

一、為什么需要負載均衡?

需要負載均衡的理由:

 

  • 「可擴展性」:負載均衡對某些擴展策略有所幫助,比如流量控制,例如數據庫讀寫流量分離時從備庫讀數據,降低主庫讀的工作負載,提升寫的負載能力,在比如大型的Web應用,對于搜索類請求需要最小化延遲,可以負載到最近的數據中心,對于上傳來講需要最大化吞吐量,需要負載到帶寬沒有占滿的鏈路,即使跳的遠一點。
  • 「高效性」:負載均衡有助于更有效地使用資源,控制流量被路由到何處。如果服務器處理能力各不相同,這就尤為重要:你可以把更多的工作分配給性能更好的機器。
  • 「高可用性」: 一個靈活的負載均衡解決方案能夠使用時刻保持可用的服務器。
  • 「匿名性」: 客戶端無須知道是否存在負載均衡設置。負載均衡器給客戶端看到的只是一個代理一個虛擬服務器。
  • 「一致性」: 如果應用是有狀態的(數據庫事務,網站會話等),那么負載均衡器就應將相關的查詢指向同一個服務器,以防止狀態丟失。應用無須去跟蹤到底連接的是哪個服務器。

 


 

從集群角度考慮,MySQL做主備集群復制如果只用作備份,有些浪費,和負載均衡結合使用一種相輔相成的作用。

所以MySQL的負載均衡架構通常和數據分片及復制緊密相關。我們今天要講的讀/寫分離策略即屬于負載均衡的一個實現。

有些負載均衡技術本身能夠實現這一點,有些需要應用自己知道哪些節點是可讀的或可寫的。

客戶端讀寫分離

常見的讀寫分離一種是通過客戶端去區分讀寫,比如上面那個圖,寫在主庫,讀通過負載均衡到多個從庫。

在應用層面粗粒度通過配置不同數據源分離讀寫實現,同時還需要考慮從庫異步復制導致的臟數據問題,需要監控延遲復制來決策讀寫的分配??梢钥紤]在編碼層次基于查詢,版本,請求時間戳,會話等做一些讀寫策略,不能容忍臟數據的讀可以放到寫節點

從庫的負載可以通過DNS負載、LVS+Keepalived、硬件負載均衡器F5、TCP代理(HAproxy,Nginx)、或者在應用中管理從庫負載均衡。比如做簡單的數據源池做線性負載等。

服務端讀寫分離

另一種是通過在服務端去區分,通過MySQL Proxy的方式實現。客戶端的請求都到MySQL Proxy上,如果客戶端要執行查詢數據的請求,則交給從服務器來處理;如果客戶端要對數據進行增、刪、改的操作,則交給主服務器來處理;

MySQL Proxy相關的工具有很多,有自帶的mysql-proxy插件,也有MyCat等中間件,今天和小伙伴分享通過MaxScale來實現的讀寫分離,不管使用那種方式,個人覺得如果對于臟數據零容忍的應用更多的應該在硬件資源上面考慮,并且大多數的讀寫分離解決方案都需要監控延遲復制來決策讀寫的分配。做的不好,總感覺有點不靠譜...

二、配置主從集群的讀寫分離


 

MariaDB MaxScale是MariaDB企業服務器、MariaDB ColumnStore和MariaDB Xpand的高級數據庫代理,為它們提供企業高可用性、可伸縮性、安全和集成服務,同時抽象出底層數據庫基礎設施,以簡化應用程序開發和數據庫管理。

官方地址:https://mariadb.com/

讀寫分離工作原理

由 MaxScale面向客戶端提供服務,收到SQL寫請求時,交給master 服務器處理,收到SQL讀請求時,交給slave服務器處理,這里我們已經搭建好一個主從結構的MySQL集群,關于集群搭建小伙伴可以看我之前的文章,有詳細教程,所以這里只需要安裝MaxScale,然后配置啟動測試就OK


 

 

  • MaxScale代理服務:192.168.26.152
  • 主庫:192.168.26.153(寫)
  • 從庫:192.168.26.154(讀)
  • 客戶端: 192.168.26.155
安裝 MaxScale

 

可以參考官方文檔: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準備中... ################################# [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準備中... ################################# [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準備中... ################################# [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準備中... ################################# [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]# 定義線程的個數,一個線程對應一個CPU核心數,線程數量要小于等于CPU核心數threads=auto# Server definitions# Set the address of the server to the network# address of a MariaDB server.# 指定要代理的數據庫服務器信息[server1]type=serveraddress=192.168.26.153 #需要數據庫服務器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/# 定義要監控的數據庫服務器[MariaDB-Monitor]type=monitormodule=mariadbmonservers=server1,server2#指定監控用戶maxscalemon,用于登錄后端服務器,檢查服務器的運行狀態和主從狀態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/#定義只讀服務器配置,需要注釋掉,我們配置的是讀寫分離#[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/#定義讀寫分離服務器配置[Read-Write-Service]type=servicerouter=readwritesplitservers=server1,server2 ##指定做讀寫分離服務的數據庫服務器user=maxscaleroutepassword=liruilong##客戶端通過用戶名和密碼向數據庫發送請求,先訪問maxscale代理服務器,再由代理服務器將請求轉發##給后端數據庫服務器;maxscale代理服務器是通過路由用戶連接后端服務器,檢測客戶端的用戶名和密碼在##后端數據庫中是否存在# 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# 定義讀寫分離服務使用的端口號[Read-Write-Listener]type=listenerservice=Read-Write-Serviceprotocol=MariaDBClientport=4006┌──[[email protected]]-[~]└─$創建監控用戶和路由用戶

創建監控用戶maxscalemon,用于登錄后端服務器,檢查服務器的狀態

grant replication slave,replication client on *.* to maxscalemon@"%" identified by "liruilong";

 

  • replication slave 能夠同步數據,查看從服務器上slave的狀態;
  • replication client 可以獲取數據庫服務的狀態(數據庫服務是否允許,主從是否正常)
MariaDB [(none)]> grant replication slave,replication client on *.* to maxscalemon@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)

 

創建路由用戶maxscalerouter,檢測客戶端的用戶名和密碼在后端數據庫中是否存在

 

  • 只是檢查用戶是否存在,所以此用戶只需對mysql庫下表有查詢權限即可
MariaDB [(none)]> grant select on mysql.* to maxscaleroute@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)

 

在mysql庫的user表中,查看監控用戶和路由用戶

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表中,查看同步過去監控用戶和路由用戶

┌──[[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服務

┌──[[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

查看日志,配置文件有個報錯,解決下

┌──[[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]]-[~]└─$檢查服務器配置

使用maxctrl list servers和maxctrl show server命令查看配置的服務器對象。

獲取服務器對象的完整列表:

 

  • 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]]-[~]└─$

 

對于每一個服務器對象,查看配置:

┌──[[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檢查監控配置

使用maxctrl list monitors和maxctrl show monitor命令查看已配置的監視器。

獲取監控器的完整列表:

┌──[[email protected]]-[~]└─$maxctrl list monitors┌─────────────────┬─────────┬──────────────────┐│ Monitor │ State │ Servers │├─────────────────┼─────────┼──────────────────┤│ MariaDB-Monitor │ Running │ server1, server2 │└─────────────────┴─────────┴──────────────────┘

對于每個監控,查看監控配置:

┌──[[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]]-[~]└─$檢查服務配置

使用maxctrl list services和maxctrl show service命令查看已配置的路由服務。

獲取路由服務的完整列表:

┌──[[email protected]]-[~]└─$maxctrl list services┌────────────────────┬────────────────┬─────────────┬───────────────────┬──────────────────┐│ Service │ Router │ Connections │ Total Connections │ Targets │├────────────────────┼────────────────┼─────────────┼───────────────────┼──────────────────┤│ Read-Write-Service │ readwritesplit │ 0 │ 0 │ server1, server2 │└────────────────────┴────────────────┴─────────────┴───────────────────┴──────────────────┘

查看詳細信息

┌──[[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]]-[~]└─$檢查讀寫分離

創建一個普通用戶用于測試,并且授權數據庫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]]-[~]└─$

代理機器上查看路由端口

┌──[[email protected]]-[~]└─$ maxctrl list listeners┌─────────────────────┬──────┬──────┬─────────┬────────────────────┐│ Name │ Port │ Host │ State │ Service │├─────────────────────┼──────┼──────┼─────────┼────────────────────┤│ Read-Write-Listener │ 4006 │ :: │ Running │ Read-Write-Service │└─────────────────────┴──────┴──────┴─────────┴────────────────────┘┌──[[email protected]]-[~]└─$

客戶機連接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 |

客戶機連接MaxScale代理,通過代理插入一條數據

┌──[[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]]-[~]└─$

可以正常讀取,下面在從(讀)庫插入一條數據

┌──[[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 |

主庫沒有數據,但是通過代理讀到了數據,說明讀是通過從讀取。

┌──[[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 |

通過代理插入數據,主從庫數據同時存在,說明寫是在主庫,然后同步給從庫

┌──[[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/

分享到:
標簽:讀寫 分離
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定