MySQL雙主搭建+keepalived高可用的實(shí)現(xiàn)
一、測(cè)試環(huán)境準(zhǔn)備
節(jié)點(diǎn)1 | 節(jié)點(diǎn)2 | |
---|---|---|
IP地址 | 192.168.101.77 | 192.168.101.79 |
MySQL版本 | 8.0.32 | 8.0.32 |
二、主從搭建
1.創(chuàng)建復(fù)制用戶
節(jié)點(diǎn)1執(zhí)行:
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123123'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; mysql> FLUSH PRIVILEGES; mysql> show master status;
2.創(chuàng)建復(fù)制關(guān)系
節(jié)點(diǎn)2執(zhí)行
mysql> change master to master_host='192.168.101.77', MASTER_PORT=3306, master_user='repl', master_password='123123' , MASTER_AUTO_POSITION=1;
3.開啟復(fù)制,確認(rèn)復(fù)制是否成功
開啟復(fù)制 mysql> start slave; 查看復(fù)制狀態(tài)(下面是正常狀態(tài)) root@localhost:(none)>show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for source to send event Master_Host: 192.168.101.77 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 197 Relay_Log_File: localhost-relay-bin.000004 Relay_Log_Pos: 413 Relay_Master_Log_File: mysql-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 197 Relay_Log_Space: 2098 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: f6ca767b-2144-11ee-80e4-080027a4fdbc Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: f6ca767b-2144-11ee-80e4-080027a4fdbc:4-8 Executed_Gtid_Set: f6ca767b-2144-11ee-80e4-080027a4fdbc:1-8 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set, 1 warning (0.00 sec) 可在節(jié)點(diǎn)1插入數(shù)據(jù),然后在節(jié)點(diǎn)2查詢數(shù)據(jù)是否同步,確定同步是否正常。
4.同步排錯(cuò)
報(bào)錯(cuò)1:
Last_IO_Error: error connecting to master 'repl@192.168.101.77:3306' - retry-time: 60 retries: 21 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
解決方法:
ALTER USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY '123123';
報(bào)錯(cuò)2:
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Cannot replicate because the master purged required binary logs. Replicate the missing transactions from elsewhere, or provision a new slave from backup. Consider increasing the master's binary log expiration period. The GTID set sent by the slave is '75433121-6a95-11ef-8b71-080027123ed0:1-3', and the missing transactions are 'f6ca767b-2144-11ee-80e4-080027a4fdbc:1-3''
解決方法:
root@localhost:(none)>stop slave; Query OK, 0 rows affected, 1 warning (0.11 sec) root@localhost:(none)>reset master; Query OK, 0 rows affected (0.10 sec) root@localhost:(none)>reset slave; Query OK, 0 rows affected, 1 warning (0.20 sec) root@localhost:(none)>set @@GLOBAL.GTID_PURGED='f6ca767b-2144-11ee-80e4-080027a4fdbc:1-3'; Query OK, 0 rows affected (0.06 sec) root@localhost:(none)>start slave; Query OK, 0 rows affected, 1 warning (0.10 sec)
三、雙主配置
雙主配置也就是將上面的主從操作反向操作一遍。
1.創(chuàng)建復(fù)制用戶
mysql> CREATE USER 'repl1'@'%' IDENTIFIED BY '123123'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl1'@'%'; mysql> FLUSH PRIVILEGES; mysql> show master status;
2.創(chuàng)建復(fù)制關(guān)系
mysql> change master to master_host='192.168.101.79', MASTER_PORT=3306, master_user='repl', master_password='123123' , MASTER_AUTO_POSITION=1;
3.開啟復(fù)制,確認(rèn)復(fù)制是否成功
mysql> start slave; mysql>show slave status \G;
4.雙主測(cè)試
節(jié)點(diǎn)1插入數(shù)據(jù),節(jié)點(diǎn)2查詢
節(jié)點(diǎn)2插入數(shù)據(jù),節(jié)點(diǎn)1查詢
四、keepalived配置
1.keepalived安裝、啟停操作
yum -y install keepalived systemctl status keepalived systemctl start keepalived systemctl stop keepalived
2.keepalived配置文件
vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc router_id MYCAT_HA # 定義名稱 } vrrp_script chk_mycat_alive { script "/etc/keepalived/mysql_check.sh" # 返回狀態(tài)碼為0表示正常,檢測(cè)腳本為true;返回狀態(tài)碼非0表示異常,檢測(cè)腳本為false interval 2 # 檢測(cè)腳本執(zhí)行的間隔,單位是秒 weight 20 } vrrp_instance VI_1 { state MASTER #主節(jié)點(diǎn)為MASTER,備節(jié)點(diǎn)為BACKUP interface enp0s3 #根據(jù)實(shí)際網(wǎng)卡名配置 virtual_router_id 88 priority 100 advert_int 1 authentication { auth_type PASS auth_pass qwert } track_script { chk_mycat_alive # 調(diào)用檢測(cè)腳本 } virtual_ipaddress { 192.168.101.199/24 # 定義虛擬ip(VIP) } }
3.keepalived配置文件
服務(wù)狀態(tài)檢查腳本
cat /etc/keepalived/mysql_check.sh #!/bin/bash count=`ps -C mysqld --no-heading|wc -l` time=$(date "+%Y-%m-%d %H:%M:%S") if [ $count = 0 ]; then echo "$time : count=$count, mysql is not running..." >> /var/log/keepalived_check.log exit 1 # 返回1說明腳本非正常執(zhí)行,mysql不在運(yùn)行中 else echo "$time : count=$count, mysql is running..." >> /var/log/keepalived_check.log exit 0 # 返回0說明腳本正常執(zhí)行,mysql正在運(yùn)行中 fi
4.keepalived配置成功
配置完成后可在其中一節(jié)點(diǎn)上看到虛擬地址,同網(wǎng)段也可以ping通虛擬地址,同時(shí)也可以通過虛擬地址連接上數(shù)據(jù)庫(kù)。
五、集群狀態(tài)測(cè)試
兩節(jié)點(diǎn)物理地址均可連接數(shù)據(jù)庫(kù),也可通過虛擬地址連接數(shù)據(jù)庫(kù),下面我們測(cè)試一下關(guān)閉一臺(tái)服務(wù)器(虛擬地址所服務(wù)器)測(cè)試是否還是可通過虛擬地址連接數(shù)據(jù)庫(kù)。
節(jié)點(diǎn)2關(guān)機(jī)后虛擬地址跑到節(jié)點(diǎn)1上去了,測(cè)試用虛擬地址連接數(shù)據(jù)庫(kù)依然可以連接
重新啟動(dòng)節(jié)點(diǎn)2后,啟動(dòng)數(shù)據(jù)庫(kù)即可拉起雙主集群
到此這篇關(guān)于MySQL雙主搭建+keepalived高可用的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL雙主搭建+keepalived高可用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ubuntu下取消MySQL數(shù)據(jù)庫(kù)本機(jī)綁定限制方法
在Ubuntu系統(tǒng)中,添加了MySQL賬戶,賦予了數(shù)據(jù)庫(kù)完全操作權(quán)限,并且允許數(shù)據(jù)庫(kù)從外部鏈接 但是,還是無法遠(yuǎn)程訪問MySQL數(shù)據(jù)庫(kù)2013-06-06MySql報(bào)錯(cuò)Table mysql.plugin doesn’t exist的解決方法
一般產(chǎn)生原因是手工更改my.ini的數(shù)據(jù)庫(kù)文件存放地址導(dǎo)致的,大家可以參考下下面的方法2013-02-02mysql 8.0.22 zip壓縮包版(免安裝)下載、安裝配置步驟詳解
這篇文章主要介紹了mysql 8.0.22 zip壓縮包版(免安裝)下載、安裝配置步驟詳解,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12非常實(shí)用的MySQL函數(shù)全面總結(jié)詳解示例分析教程
這篇文章主要為大家介紹了非常實(shí)用的MySQL函數(shù)的詳解示例分析,文中全面的概括了MySQL函數(shù),并進(jìn)行了詳細(xì)的示例講解,有需要的朋友可以借鑒參考下2021-10-10mysql通過my.cnf修改默認(rèn)字符集為utf-8的方法和注意事項(xiàng)
本文主要給大家介紹mysql通過my.cnf修改默認(rèn)字符集為utf-8的方法,當(dāng)然你也可以設(shè)置成別的,國(guó)際點(diǎn)還是utf-8好,以及在修改過程中要注意的一些事項(xiàng),有需要的朋友們可以參考借鑒。2016-09-09MYSQL數(shù)據(jù)庫(kù)GTID實(shí)現(xiàn)主從復(fù)制實(shí)現(xiàn)(超級(jí)方便)
這篇文章主要介紹了MYSQL數(shù)據(jù)庫(kù)GTID實(shí)現(xiàn)主從復(fù)制實(shí)現(xiàn)(超級(jí)方便),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11工作中常用的mysql語句分享 不用php也可以實(shí)現(xiàn)的效果
本文給大家介紹幾條比較有用的MySQL的SQL語句,可能很多人都通過PHP來實(shí)現(xiàn)這些功能,其實(shí)數(shù)據(jù)也是能實(shí)現(xiàn)很多功能的2012-05-05