數據庫讀寫分離

實現 MySQL 的讀寫分離主要可以通過以下幾種方式:

  1. 一主多從架構

    • 設置一個主數據庫(Master)來處理寫操作(如 INSERT、UPDATE、DELETE)。

    • 設置多個從數據庫(Slave)來處理讀操作(如 SELECT)。

    • 主數據庫通過復制功能(如 MySQL 的主從復制)將數據變更同步到從數據庫上。

  2. 使用中間件

    • 中間件如 MyCat、ProxySQL、MaxScale 等,它們位于應用程序和數據庫服務器之間,負責處理讀寫請求的路由。

    • 中間件根據配置的策略(如基于 SQL 語句的類型、用戶、時間等)將讀請求發送到從數據庫,將寫請求發送到主數據庫。

    • 中間件通常還提供負載均衡、故障轉移等功能。

  3. 數據庫自帶功能

    • 一些數據庫管理系統(如 MySQL 的 Group Replication 或 MariaDB 的 Galera Cluster)提供了內置的讀寫分離和故障轉移功能。

    • 這些功能通常更加穩定可靠,但可能受到數據庫管理系統版本的限制。

以下是實現讀寫分離的一般步驟:

  1. 配置主從復制

    • 在主數據庫上配置二進制日志(binary logging)和服務器 ID。

    • 在從數據庫上配置主數據庫的 IP 地址、端口、用戶名、密碼等信息,并啟動復制進程。

    • 驗證從數據庫是否成功從主數據庫復制數據。

  2. 配置中間件(如果使用):

    • 根據所選的中間件進行配置,包括數據庫連接信息、讀寫分離策略、負載均衡策略等。

    • 啟動中間件服務,并驗證其是否正常工作。

  3. 修改應用程序(如果使用基于應用程序的讀寫分離):

    • 修改應用程序的代碼,使其根據業務需求將讀請求發送到從數據庫,將寫請求發送到主數據庫。

    • 這可能需要修改數據庫連接字符串、添加路由邏輯等。

  4. 監控和日志

    • 對讀寫分離架構進行持續的監控,包括主從數據庫的同步狀態、性能指標等。

    • 記錄相關的日志信息,以便在出現問題時能夠快速定位和解決問題。

環境

Redhat 9.2

192.168.200.133 mysql-proxy

192.168.200.128 master

192.168.200.129 slave

步驟
1、修改主機名,關閉防火墻
[root@localhost ~]# hostnamectl  hostname  master
[root@localhost ~]# bash
[root@master ~]# systemctl  stop firewalld.service 
[root@master ~]# systemctl  disable firewalld.service 
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@master ~]# setenforce  0
[root@master ~]#[root@localhost ~]# hostnamectl  hostname  slave
[root@localhost ~]# bash
[root@slave ~]# systemctl  stop  firewalld.service 
[root@slave ~]# systemctl  disable  firewalld.service 
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@slave ~]# setenforce 0
[root@slave ~]# [root@admin ~]# hostnamectl  hostname mysql_proxy
[root@admin ~]# bash
[root@mysqlproxy ~]# systemctl  stop firewalld.service 
[root@mysqlproxy ~]# systemctl  disable firewalld.service 
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@mysqlproxy ~]# setenforce  0
[root@mysqlproxy ~]# 
2、安裝數據庫軟件
[root@master ~]# yum -y install  mariadb*
[root@master ~]# systemctl  restart  mariadb.service 
[root@master ~]# systemctl  enable  mariadb.service 
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@master ~]# mysql_secure_installation [root@slave ~]# yum -y install  mariadb*
[root@slave ~]# systemctl  restart  mariadb.service 
[root@slave ~]# systemctl  enable  mariadb.service 
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@slave ~]# 
[root@slave ~]# mysql_secure_installation 
3、對兩臺機器做主從同步
#master配置
[root@master ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
log_bin=mysql_bin     //添加
binlog_ignore_db=mysql    //添加
server_id=200			//添加
[root@master ~]# systemctl  restart  mariadb.service #slave
[root@slave ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
log_bin=mysql_bin      //添加如下
binlog_ignore_db=mysql
server_id=201
[root@slave ~]# systemctl  restart  mariadb.service 
?4、創建登錄用戶并授權
[root@master ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.16-MariaDB-log 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)]> create user 'slave'@'%' identified by '1';
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> grant replication slave on  *.* to 'slave'@'%' identified by '1';
Query OK, 0 rows affected (0.001 sec)
5、在slave上開啟同步
MariaDB [(none)]> change master to master_host='192.168.200.128',master_user='slave',master_password='1';
Query OK, 0 rows affected (0.007 sec)MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.200.128Master_User: slaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql_bin.000001Read_Master_Log_Pos: 659Relay_Log_File: mariadb-relay-bin.000002Relay_Log_Pos: 958Relay_Master_Log_File: mysql_bin.000001Slave_IO_Running: YesSlave_SQL_Running: Yes
......
6、給mysql-proxy主機安裝lua解析器

安裝解析器和mariadb

[root@mysqlproxy ~]# yum -y install  mariadb*
[root@mysqlproxy ~]# systemctl  restart  mariadb.service 
[root@mysqlproxy ~]# mysql_secure_installation 
[root@mysqlproxy ~]# yum -y install lua*
#下載Mysql-proxy軟件?具
[root@mysqlproxy ~]#  wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@mysqlproxy ~]# ls
公共  模板  視頻  圖片  文檔  下載  音樂  桌面  anaconda-ks.cfg  mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@mysqlproxy ~]# 
#解壓
[root@mysqlproxy ~]# tar -xvf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
[root@mysqlproxy ~]# cd /usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/
[root@mysqlproxy mysql-proxy-0.8.5-linux-el6-x86-64bit]# ls
bin  include  lib  libexec  licenses  share
[root@mysqlproxy mysql-proxy-0.8.5-linux-el6-x86-64bit]# 
#配置環境變量
[root@mysqlproxy ~]# export PATH=$PATH:$HOME/bin:/usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/bin
7、增加配置文件
[root@mysqlproxy ~]# vim /usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/mysql_proxy.conf 
[mysql-proxy]plugins=proxy   //代理插件proxy-address=192.168.200.133:4040   // 定義 MySQL Proxy 監聽的地址和端口,客戶端應該連接到這個地址和端口來訪問 MySQL 服務proxy-backend-addresses=192.168.200.129:3306 // 主服務器地址進行寫操作proxy-read-only-backend-addresses=192.168.200.128:3306  //從服務器地址進行讀操作proxy-lua-script=/usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit//share/doc/mysql- proxy/rw-splitting.lualog-file=/usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/mysql-proxy.loglog-level=debugkeepalive=true   //保持連接daemon=true#修改權限[root@mysqlproxy ~]# chmod 660 /usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/mysql_proxy.conf 
[root@mysqlproxy ~]# 
8、啟動mysql_proxy服務

啟動后修改讀寫分離配置文件

[root@mysqlproxy ~]# mysql-proxy --defaults-file=/usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/mysql_proxy.conf 
[root@mysqlproxy ~]# ss -anltp | grep mysql-proxy
LISTEN 0      128    192.168.200.133:4040      0.0.0.0:*    users:(("mysql-proxy",pid=84175,fd=10))
[root@mysqlproxy ~]# 
[root@mysqlproxy ~]# vim /usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/share/doc/mysql-proxy/rw-splitting.lua 38 if not proxy.global.config.rwsplit then39         proxy.global.config.rwsplit = {40                 min_idle_connections = 1,   //1,只要有?個連接,就進?讀寫分離。41                 max_idle_connections = 8,42 43                 is_debug = false44         }45 end
[root@mysqlproxy ~]# pkill mysql-proxy   //終止進程
[root@mysqlproxy ~]# mysql-proxy --defaults-file=/usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit/mysql_proxy.conf 
[root@mysqlproxy ~]# 
9、主從服務器為mysqlproxy數據庫賬戶授權
[root@master ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.5.16-MariaDB-log 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 *.* TO 'mysqlproxy'@'%' IDENTIFIED BY '1';
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> [root@slave ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 10.5.16-MariaDB-log 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 *.* TO 'mysqlproxy'@'%' IDENTIFIED BY '1';
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> 
10、測試
[root@mysqlproxy ~]# mysql -umysqlproxy -p -P 4040 -h 192.168.200.133
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.5.16-MariaDB-log 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)]> 

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/14536.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/14536.shtml
英文地址,請注明出處:http://en.pswp.cn/web/14536.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

USB數據恢復軟件:輕松找回U盤重要數據!

USB數據丟失的原因 USB數據丟失有一些常見原因,了解這些原因有利于恢復數據。 文件意外刪除病毒攻擊軟件錯誤未安全彈出USB設備格式化USB設備 順便一提,如果你通過快捷鍵“Ctrl D”刪除了數據,那你可以從回收站中還原它們。如果你永久刪除…

Isaac Sim仿真平臺學習(1)認識Isaac Sim

0.前言 上一個教程中我們下載好了Isaac Sim,這一章我們將來簡單了解一下Isaac Sim平臺。 isaac Sim仿真平臺安裝-CSDN博客 1.Isaac Sim是啥? What Is Isaac Sim? — Omniverse IsaacSim latest documentation Isaac Sim是NVDIA Omniverse平臺的機器…

【編譯原理復習筆記】屬性文法

屬性文法 也稱為屬性翻譯文法,由 Knuth 提出,以上下文無關文法為基礎 (1)為每個文法符號(終結符與非終結符)配備相關的屬性,代表與該文法符號相關的信息 (2)屬性文法對于…

【LSTM】基于Matlab的LSTM模型建模(代碼)

訓練目標:用LSTM訓練數據 數據:隨時間遞增,患者患病的概率(橫坐標1個單位代表1個時間單位) 以下代碼可直接運行 clc clear close all warning off % 關閉報警信息 %% 1.數據操作 % 1.1.導入數據&#x…

數據鏈路層協議——以太網協議

1. 數據鏈路層 網絡層用于將數據從一臺主機發送到另一臺主機。傳輸層用于將數據可靠的從一臺主機發送到另一臺主機。(網絡層沒有保證可靠性的策略,傳輸過程中可能會出現各種意外,例如:丟包,網絡擁塞等。通過傳輸層可以…

跨域問題的4種解決方案

文章導讀 前言 跨域問題指的是在Web開發中,由于瀏覽器的同源策略限制,當一個網頁嘗試訪問與它不同源(協議、域名或端口不同)的資源時,可能會遇到安全限制導致無法正常訪問的問題。這種策略旨在防止惡意網站讀取或修改其…

yarn的基本命令和用法

Yarn通過并行安裝、離線模式、確定性安裝以及更好的依賴解析算法,為開發者提供了更快、更穩定、更安全的包管理體驗。它保留了npm的大部分功能,并在此基礎上做了大量優化,下面我們就來詳述Yarn的核心命令和實用技巧。📚 安裝Yarn…

【MySQL精通之路】InnoDB(7)-鎖和事務模型(2)-事務模型

主博客: 【MySQL精通之路】InnoDB(7)-鎖和事務模型-CSDN博客 上一篇: 【MySQL精通之路】InnoDB(7)-鎖和事務模型(1)-鎖-CSDN博客 下一篇: 目錄 1.事務隔離級別 2.1 可重復讀 2.2 讀已提交 2.3 讀取未提交 2.4 序列化讀 2.自動提交、…

訂餐系統總結、

應用層: SpringBoot:快速構建Spring項目,采用“約定大于配置”的思想,簡化Spring項目的配置開發。 SpringMvc:Spring框架的一個模塊,springmvc和spring無需通過中間整合層進行整合,可以無縫集成。 Sprin…

完整的數據可視化方法集

在當前的大數據時代,了解如何可視化數據是UI/UX設計師技能的重要組成部分。如今,幾乎所有的公司都需要良好的數據可視化作為確定業務方向和決策的參考。數據的可視化結果越好,用戶的決策就越科學。 1、什么是數據可視化 數據可視化是將信息…

張量 t-product 積(matlab代碼)

參考文獻:Tensor Robust Principal Component Analysis with a New Tensor Nuclear Norm 首先是文章2.3節中 t-product 的定義: 塊循環矩陣: 參考知乎博主的例子及代碼:(t-product與t-QR分解,另一篇傅里葉對…

HTML5 設備訪問及輸入輸出設備交互

目錄 設備訪問輸入設備交互輸出設備交互設備訪問 設備信息訪問 navigator.userAgent:獲取瀏覽器的用戶代理字符串,從中可以解析出設備類型、操作系統、瀏覽器版本等信息。 const userAgent = navigator.userAgent; console.log(userAgent); // 輸出類似 "Mozilla/5.0…

算法(Algorithm)

算法(Algorithm)是指解題方案的準確而完整的描述,是一系列解決問題的清晰指令,代表著用系統的方法描述解決問題的策略機制。也就是說,算法能夠對一定規范的輸入,在有限時間內獲得所要求的輸出。算法應該具有…

【python004】miniforge可行替代方案實戰總結(最近更新中)

1.熟悉、梳理、總結項目研發實戰中的miniforge日常使用中的問題。隨著版本更新,做了一些變動,如商業化限制,取消一些語法等。 2.歡迎點贊、關注、批評、指正,互三走起來,小手動起來!

vue通過for循環生成input框后雙向綁定失效問題

有些時候頁面上有太多的表單元素&#xff0c;一個個的寫太過繁瑣&#xff0c;拿 input 框舉例&#xff0c;眾多的 input 框&#xff0c;無非就是輸入框前的說明和 input 框的 name 屬性不一樣 <el-form :inline"true" :model"formInline" size"mi…

01-05.Vue自定義過濾器

目錄 前言過濾器的概念過濾器的基本使用給過濾器添加多個參數 前言 我們接著上一篇文章01-04.Vue的使用示例&#xff1a;列表功能 來講。 下一篇文章 02-Vue實例的生命周期函數 過濾器的概念 概念&#xff1a;Vue.js 允許我們自定義過濾器&#xff0c;可被用作一些常見的文本…

軟件模塊的耦合

軟件模塊的耦合 耦合是指軟件模塊之間的依賴程度&#xff0c;耦合越低&#xff0c;模塊之間的獨立性越高&#xff0c;軟件的可維護性、可重用性也越高。下面是幾種常見的耦合類型的概念&#xff1a; 數據耦合&#xff08;Data Coupling&#xff09;&#xff1a; 當一個模塊通…

Python ? 使用代碼解決今天中午吃什么的重大生存問題

1. 環境安裝 安裝Python代碼環境參考文檔 2. 代碼塊 import random# 準備一下你想吃的東西 hot ["蘭州拉面", "爆肚面", "黃燜雞", "麻辣香鍋", "米線", "麻食", "羊肉泡饃", "肚絲/羊血湯&qu…

doxygen 1.11.0 使用詳解(九)——包含公式

目錄 Doxygen allows you to put LATEX formulas in the output (this works only for the HTML, LATEX and RTF output. To be able to include formulas (as images) in the HTML and RTF documentation, you will also need to have the following tools installed latex: …

定時監測服務器磁盤是否超過閾值,超過就刪除docker 鏡像

達到指定百分比 刪除鏡像腳本 df -h 查找到 內存占用信息 &#xff0c;得到的 文件系統名稱是 overlay的&#xff0c;Use% 達到70就進行刪除docker 鏡像 #!/bin/bash# 設置磁盤使用閾值 THRESHOLD70# 獲取 overlay 文件系統的磁盤使用百分比 DISK_USAGES$(df -h | grep overl…