MySQL Galera Cluster部署與使用
MySQL Galera Cluster 是一個高可用、同步復制的數(shù)據庫解決方案,基于 Galera Library 和 MySQL 數(shù)據庫。它設計用于提供高可用性、負載均衡和數(shù)據一致性,特別適合需要高寫入負載和多主節(jié)點支持的場景。
主要特點
同步復制:
- 所有的寫操作(包括插入、更新、刪除)在集群中的所有節(jié)點上都是同步的。這意味著每個節(jié)點上的數(shù)據是完全一致的。
多主節(jié)點(Multi-Master):
- 集群中的每個節(jié)點都是主節(jié)點。所有節(jié)點都可以處理讀寫請求,從而實現(xiàn)負載均衡和高可用性。
自動故障轉移:
- 如果一個節(jié)點發(fā)生故障,其他節(jié)點會自動接管,確保系統(tǒng)持續(xù)可用。
自動節(jié)點加入:
- 新節(jié)點可以自動加入集群,并同步到當前集群狀態(tài)。
數(shù)據一致性:
- 使用基于事務的復制保證數(shù)據一致性,所有事務在所有節(jié)點上以相同的順序執(zhí)行。
容錯性:
- 集群能夠處理網絡分區(qū)和節(jié)點故障,確保集群的健壯性。
組件
Galera Library:
- 提供了同步復制功能和事務一致性。Galera Library 負責復制和應用事務。
MySQL 數(shù)據庫:
- 提供數(shù)據庫管理功能,包括 SQL 支持、查詢優(yōu)化等。
State Snapshot Transfer (SST) 和 Incremental State Transfer (IST):
- SST 用于將新節(jié)點與集群中的現(xiàn)有數(shù)據進行同步,IST 用于在集群中節(jié)點之間進行增量數(shù)據傳輸。
一. 環(huán)境準備
主機名 | IP | 系統(tǒng) | 軟件版本 | 配置信息 |
galera1 | 192.168.226.31 | Rocky_linux9.4 | mysql-wsrep-8.0 galera-26.4.14-1 | 2核4G |
galera2 | 192.168.226.32 | Rocky_linux9.4 | mysql-wsrep-8.0 galera-26.4.14-1 | 2核4G |
galera3 | 192.168.226.33 | Rocky_linux9.4 | mysql-wsrep-8.0 galera-26.4.14-1 | 2核4G |
galera4 | 192.168.226.34 | Rocky_linux9.4 | mysql-wsrep-8.0 galera-26.4.14-1 | 2核4G |
同意關閉防火墻和selinux,進行時間同步。
主機解析:
[root@galera1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.226.32 galera2 192.168.226.33 galera3 192.168.226.34 galera4
[root@galera2 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.226.31 galera1 192.168.226.33 galera3 192.168.226.34 galera4
[root@galera3 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.226.32 galera2 192.168.226.31 galera1 192.168.226.34 galera4
[root@galera4 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.226.32 galera2 192.168.226.33 galera3 192.168.226.31 galera1
四臺虛擬機都導入創(chuàng)建該服務的yum源
[root@galera1 ~]# cat /etc/yum.repos.d/mysql-wsrep.repo [galera] name=galera baseurl=http://releases.galeracluster.com/mysql-wsrep-8.0/redhat/9/x86_64/ enabled=1 gpgcheck=0
[root@galera1 ~]# yum clean all
四臺虛擬機都下載mysql-wsrep-8.0和galera
[root@galera1 ~]# yum install -y mysql-wsrep-8.0 galera
mysql-wsrep-8.0
:
mysql-wsrep-8.0
是 MySQL 8.0 版本的一個變體,集成了 WSREP(Write Set Replication)協(xié)議。這是一個用于數(shù)據庫集群的復制協(xié)議,允許 MySQL 數(shù)據庫節(jié)點之間的同步復制。- 這個版本的 MySQL 是通過 WSREP 協(xié)議來實現(xiàn)數(shù)據的一致性和高可用性。WSREP 協(xié)議確保了所有節(jié)點的數(shù)據庫狀態(tài)保持一致,支持事務的自動同步和沖突解決。
- 通常,這種版本的 MySQL 是在集群配置中使用的,旨在提高系統(tǒng)的容錯能力和可擴展性。
Galera:
- Galera 是一個用于 MySQL 數(shù)據庫的同步復制插件,它實現(xiàn)了 WSREP 協(xié)議。Galera 提供了一個多主節(jié)點的數(shù)據庫集群方案,允許所有節(jié)點同時進行讀寫操作,并且確保數(shù)據在所有節(jié)點之間保持一致。
- 它主要用于提高數(shù)據庫的可用性、容錯性和擴展性。Galera 使得所有節(jié)點都能夠同時處理讀寫請求,這比傳統(tǒng)的主從復制模式更具靈活性。
- Galera 可以與 MySQL 以及 MariaDB 配合使用,它的主要特點包括全同步復制、自動節(jié)點加入、沖突檢測和解決等。
二. 配置
四臺虛擬器都啟動服務,并改個密碼
[root@galera1 ~]# systemctl start mysqld
# 獲取數(shù)據庫初始化密碼的步驟 [root@galera1 ~]# password=$(grep "temporary password" /var/log/mysqld.log | awk -F': ' '{print $2}') [root@galera1 ~]# echo $password D.s#wfreZ8L% #改密碼 [root@galera1 ~]# mysqladmin -p"$password" password "Qaz123456+"
四臺服務都配置一個遠程用戶并授權,采用腳本方式執(zhí)行
[root@galera1 ~]# vim user.sh #每臺都要執(zhí)行一次,用來創(chuàng)建遠程用戶并授權 #!/bin/bash # MySQL 連接參數(shù) MYSQL_USER="root" MYSQL_PASS="Qaz123456+" # 替換為實際的 root 密碼 MYSQL_HOST="localhost" # 或者使用 IP 地址 # 創(chuàng)建遠程用戶和授予權限的 SQL 命令 SQL_COMMANDS=" CREATE USER 'syncuser'@'%' IDENTIFIED BY 'Qaz123456+'; GRANT ALL PRIVILEGES ON *.* TO 'syncuser'@'%'; FLUSH PRIVILEGES; " # 執(zhí)行 SQL 命令 mysql -u "$MYSQL_USER" -p"$MYSQL_PASS" -h "$MYSQL_HOST" -e "$SQL_COMMANDS"
[root@galera1 ~]# sh user.sh
四臺虛擬機服務都先停止
[root@galera1 ~]# systemctl stop mysqld
1. 配置 galera1 主機的my.cnf的文件
[root@galera1 ~]# vim /etc/my.cnf #在最后行增加即可 server-id=1 # 服務器 ID,用于唯一標識 MySQL 服務器 binlog_format=row # 二進制日志格式,行級別 innodb_file_per_table=1 # 為每個 InnoDB 表使用一個獨立的表空間文件 innodb_autoinc_lock_mode=2 # 自增鎖模式,2表示更高效的鎖模式 wsrep_on=ON # 啟用 Galera 集群 wsrep_provider=/usr/lib64/galera/libgalera_smm.so # Galera 提供者庫的路徑 wsrep_cluster_name='galera' # Galera 集群的名稱 wsrep_cluster_address='gcomm://' # Galera 集群的地址,通常為 `gcomm://` 表示集群初始節(jié)點 wsrep_node_name='galera1' # 當前節(jié)點的名稱 wsrep_node_address='192.168.226.31' # 當前節(jié)點的 IP 地址 wsrep_sst_auth=syncuser:'Qaz123456+' # SST(狀態(tài)快照傳輸)認證信息 wsrep_sst_method=rsync # SST 方法,使用 rsync 進行狀態(tài)快照傳輸
啟動galera1主機的mysql
[root@galera1 ~]# systemctl start mysqld
[root@galera1 ~]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 4096 0.0.0.0:4567 0.0.0.0:* users:(("mysqld",pid=5145,fd=9)) LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=819,fd=3)) LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=5145,fd=33)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=819,fd=4)) LISTEN 0 151 *:3306 *:* users:(("mysqld",pid=5145,fd=35)) 4567: 用于 Galera 集群的內部通信。這是 MySQL 數(shù)據庫的默認端口,用于客戶端連接 MySQL 數(shù)據庫。所有的 SQL 查詢和數(shù)據庫操作通過這個端口進行。 33060: 用于 MySQL X Protocol。這是 MySQL 的 X Plugin 端口,主要用于 MySQL 的 X Protocol (MySQL Shell、MySQL Router 和其他 MySQL X API 客戶端) 的連接。 3306: 用于 MySQL 數(shù)據庫客戶端連接。這是 MySQL 數(shù)據庫的默認端口,用于客戶端連接 MySQL 數(shù)據庫。所有的 SQL 查詢和數(shù)據庫操作通過這個端口進行。
2. 配置 galera2 主機的my.cnf的文件
[root@galera2 ~]# systemctl start mysqld server-id=2 binlog_format=row innodb_file_per_table=1 innodb_autoinc_lock_mode=2 wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_name='galera' wsrep_cluster_address='gcomm://galera1,galera3,galera4' wsrep_node_name='galera2' wsrep_node_address='192.168.226.32' wsrep_sst_auth=syncuser:'Qaz123456+' wsrep_sst_method=rsync
啟動galera2主機的mysql
[root@galera2 ~]# systemctl start mysqld
3. 配置 galera3 主機的my.cnf的文件
[root@galera3 ~]# vim /etc/my.cnf server-id=3 binlog_format=row innodb_file_per_table=1 innodb_autoinc_lock_mode=2 wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_name='galera' wsrep_cluster_address='gcomm://galera1,galera3,galera4' wsrep_node_name='galera2' wsrep_node_address='192.168.226.33' wsrep_sst_auth=syncuser:'Qaz123456+' wsrep_sst_method=rsync
啟動galera3主機的mysql
[root@galera3 ~]# systemctl start mysqld
4. 在給galera1 主機的my.cnf的文件增加節(jié)點
[root@galera1 ~]# vim /etc/my.cnf #增加上節(jié)點,即修改這行內容如下,或見下圖所示: wsrep_cluster_address='gcomm://galera2,galera3,galera4' # Galera 集群的地址,通常為 `gcomm://` 表示集群初始節(jié)點
重啟動galera1主機的mysql
[root@galera1 ~]# systemctl restart mysqld
5. 寫入數(shù)據驗證同步
在 galera1主機寫入數(shù)據,創(chuàng)建一個庫
[root@galera1 ~]# mysql -uroot -p"Qaz123456+" mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 8.0.37 Galera Cluster for MySQL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE DATABASE database_name; Query OK, 1 row affected (0.00 sec)
在 galera2和galera3主機查看
[root@galera2 ~]# mysql -uroot -p"Qaz123456+" mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 8.0.37 Galera Cluster for MySQL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | database_name | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
[root@galera3 ~]# mysql -uroot -p"Qaz123456+" mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 8.0.37 Galera Cluster for MySQL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | database_name | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
可以看到在 galera1主機創(chuàng)建的庫,同步寫入到了其他節(jié)點的mysql中。
6. 配置 galera4 主機的my.cnf的文件
[root@galera4 ~]# vim /etc/my.cnf server-id=4 binlog_format=row innodb_file_per_table=1 innodb_autoinc_lock_mode=2 wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_name='galera' wsrep_cluster_address='gcomm://galera1,galera2,galera3' wsrep_node_name='galera1' wsrep_node_address='192.168.226.34' wsrep_sst_auth=syncuser:'Qaz123456+' wsrep_sst_method=rsync
啟動galera4主機的mysql
[root@galera4 ~]# systemctl start mysqld
登錄數(shù)據庫查看數(shù)據
[root@galera4 ~]# mysql -uroot -p"Qaz123456+" mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 8.0.37 Galera Cluster for MySQL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | database_name | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
如此驗證成功,新啟動的mysq即為新加入集群的msyql,啟動即發(fā)現(xiàn),并完成同步數(shù)據。
MySQL Galera Cluster 和傳統(tǒng)My SQL的區(qū)別
1. 集群架構
傳統(tǒng) MySQL:
- 主從復制:傳統(tǒng)的 MySQL 配置通常包括一個主服務器和多個從服務器。從服務器從主服務器接收數(shù)據更新。這是一種異步復制方式,意味著從服務器可能會滯后于主服務器。
- 主主復制:一些系統(tǒng)配置多個主節(jié)點進行主主復制,但仍存在潛在的數(shù)據一致性問題。
MySQL Galera Cluster:
- 多主復制:Galera Cluster 是一個同步復制系統(tǒng),每個節(jié)點都可以充當主節(jié)點。所有節(jié)點都可以讀寫操作,確保數(shù)據在所有節(jié)點之間保持一致。
- 同步復制:數(shù)據更改會在所有節(jié)點之間同步,這意味著所有節(jié)點的數(shù)據是一致的,沒有主從的延遲。
2. 一致性和容錯
傳統(tǒng) MySQL:
- 最終一致性:在主從復制中,從節(jié)點的數(shù)據更新可能會有延遲,直到從節(jié)點追上主節(jié)點。數(shù)據一致性在主從復制中不是實時保證的。
- 容錯性:單個主節(jié)點的故障可能會導致整個系統(tǒng)停機,盡管從節(jié)點可以作為備用,但恢復時間可能會較長。
MySQL Galera Cluster:
- 強一致性:Galera Cluster 提供強一致性,即所有節(jié)點都在數(shù)據更新時保持一致。每個事務都需要在集群中的所有節(jié)點上成功提交才能被認為是成功的。
- 高可用性:集群中的任何節(jié)點都可以提供服務,并且集群能夠在節(jié)點失效時繼續(xù)運行,只要集群中有足夠的節(jié)點來維持“法定人數(shù)”(quorum)。
3. 復制機制
傳統(tǒng) MySQL:
- 異步復制:數(shù)據從主服務器復制到從服務器,可能存在延遲。
- 半同步復制:可以配置為在主服務器確認至少一個從服務器已收到數(shù)據之前才提交事務,但這并不保證所有從服務器都已更新。
MySQL Galera Cluster:
- 同步復制:所有節(jié)點在事務提交時需要在集群中多數(shù)節(jié)點上確認事務,從而確保數(shù)據一致性。
4. 配置和管理
傳統(tǒng) MySQL:
- 復雜性:主從復制的配置和管理可能較為復雜,尤其是在處理主節(jié)點故障、重新配置主從關系以及負載均衡時。
- 自動化工具:通常需要外部工具和腳本來處理故障轉移和負載均衡。
MySQL Galera Cluster:
- 集群管理:配置集群較為復雜,但它提供了內置的節(jié)點加入和故障恢復機制。
- 故障轉移:Galera Cluster 內置的機制使得節(jié)點故障轉移和恢復更加自動化和高效。
5. 事務處理
傳統(tǒng) MySQL:
- 單節(jié)點事務:事務處理通常發(fā)生在單個主節(jié)點上,可能會受制于主節(jié)點的性能和負載。
MySQL Galera Cluster:
- 全局事務:事務需要在所有參與的節(jié)點上進行一致性檢查和提交,可能會對事務性能產生影響,但保證了數(shù)據一致性。
6. 數(shù)據完整性
傳統(tǒng) MySQL:
- 數(shù)據一致性問題:在高負載和故障情況下,可能會出現(xiàn)數(shù)據一致性問題,尤其是在主從復制中。
MySQL Galera Cluster:
- 數(shù)據完整性:通過同步復制和全節(jié)點一致性檢查,Galera Cluster 更好地保證了數(shù)據的完整性和一致性。
擴展:
如果節(jié)點全部關閉,重新都起來的解決辦法:
現(xiàn)在將四個節(jié)點全部停止,再起來mysql就都起不來了,這是因為集群的特點,并且已經有了集群的信息數(shù)據。只需要將一個節(jié)點的集群數(shù)據刪除即可。
[root@galera1 ~]# rm -rf /var/lib/mysql/g*
在刪除這個節(jié)點配置文件中配置的其他的節(jié)點名
[root@galera1 ~]# vim /etc/my.cnf 將這個行修改wsrep_cluster_address='gcomm://galera2,galera3,galera4' 修改為此行wsrep_cluster_address='gcomm://'
然后啟動這個節(jié)點服務
[root@galera1 ~]# systemctl start mysqld
但這個幾點起來后,在將其余幾個節(jié)點啟動就可以了。
[root@galera2 ~]# systemctl start mysqld [root@galera3 ~]# systemctl start mysqld [root@galera4 ~]# systemctl start mysqld
然后在把剛刪除galera1的配置文件中節(jié)點名再添加回去并重啟服務即可。
[root@galera1 ~]# vim /etc/my.cnf 再將這個行修改wsrep_cluster_address='gcomm://' 修改為此行wsrep_cluster_address='gcomm://galera2,galera3,galera4'
[root@galera1 ~]# systemctl restart mysqld
這是查驗數(shù)據數(shù)據是否還完整
[root@galera1 ~]# mysql -uroot -p"Qaz123456+" mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 8.0.37 Galera Cluster for MySQL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | database_name | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
可以看到這樣數(shù)據依舊存在。
到此這篇關于MySQL Galera Cluster部署與使用的文章就介紹到這了,更多相關MySQL Galera Cluster部署內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MySQL實現(xiàn)replace函數(shù)的幾種實用場景
這篇文章主要介紹了MySQL實現(xiàn)replace函數(shù)的幾種實用場景,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02解決mysql的賦權操作之GRANT ALL PRIVILEGES ON *.*
這篇文章主要介紹了解決mysql的賦權操作之GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘123456‘ WITH GRANT OPTION問題,本文給大家分享兩種情況分析分享解決方案,感興趣的朋友一起看看吧2022-11-11MySQL計劃任務(事件調度器) Event Scheduler介紹
MySQL5.1.x版本中引入了一項新特性EVENT,顧名思義就是事件、定時任務機制,在指定的時間單元內執(zhí)行特定的任務,因此今后一些對數(shù)據定時性操作不再依賴外部程序,而直接使用數(shù)據庫本身提供的功能2013-10-10