對(duì)于 MySQL 數(shù)據(jù)庫(kù)作為各個(gè)業(yè)務(wù)系統(tǒng)的存儲(chǔ)介質(zhì),在系統(tǒng)中承擔(dān)著非常重要的職責(zé),如果數(shù)據(jù)庫(kù)崩了,那么對(duì)于讀和寫(xiě)數(shù)據(jù)庫(kù)的操作都會(huì)受到影響。如果不能迅速恢復(fù),對(duì)業(yè)務(wù)的影響是非常大的。之前 B 站不是出過(guò)一次事故么,2 小時(shí)才恢復(fù)過(guò)來(lái),詳細(xì)可以看之前寫(xiě)的文章。
檢測(cè)和重啟:兩臺(tái)服務(wù)器上都部署 keepalived 軟件,定時(shí)檢測(cè) MySQL 服務(wù)是否正常,如果一個(gè)數(shù)據(jù)庫(kù)服務(wù)崩了,keepalived 會(huì)用腳本嘗試重啟 mysql 服務(wù)。
備份:兩個(gè) keepalived 服務(wù)都提供了虛擬 IP 供客戶端使用,但是流量只會(huì)轉(zhuǎn)到一臺(tái) MySQL 服務(wù)上。
虛擬 IP:keepalived 配置好了后,會(huì)有一個(gè) 虛擬 IP,對(duì)于客戶端來(lái)說(shuō),不關(guān)心連接的是哪臺(tái) MySQL,訪問(wèn)虛擬 IP 就可以了。
流量切換:如果客戶端正在訪問(wèn)的 MySQL 服務(wù)崩了后,keepalived 會(huì)用我們寫(xiě)的腳本自動(dòng)重啟 MySQL,如果重啟失敗,腳本主動(dòng)停掉 keepalived,客戶端的流量就不會(huì)訪問(wèn)到這臺(tái)服務(wù)器上的 MySQL 服務(wù),后續(xù)訪問(wèn)的流量都會(huì)切到另外一臺(tái) MySQL 服務(wù)。
檢測(cè)和重啟的原理如下所示:
需要配置的內(nèi)容如下:
兩臺(tái) Ubuntu 服務(wù)器上啟動(dòng) MySQL 容器。
配置 MySQL 主從復(fù)制架構(gòu)。
將 MySQL 主從改為主主復(fù)制架構(gòu)。
兩臺(tái)服務(wù)器搭建 keepalived 環(huán)境監(jiān)控 MySQL 和自動(dòng)重啟 MySQL。
二、主主復(fù)制的原理
對(duì)于 MySQL 的主主架構(gòu),其實(shí)原理就是兩臺(tái)服務(wù)器互為主從,雙向復(fù)制。而復(fù)制的原理如下:
重新查詢客戶端的連接信息,發(fā)現(xiàn)已經(jīng)切換到 92b (node1)機(jī)器上的 mysql 了。
SHOW VARIABLES LIKE '%hostname%'
再次查看 node1 上 keepalived 上的狀態(tài),再發(fā)送信息給
sudo systemctl status keepalived
七、遇到的坑
7.1 密碼不正確,無(wú)法登錄的問(wèn)題
密碼不正確,設(shè)置跳過(guò)密碼驗(yàn)證
apt-get update
apt install vim
修改 mysql 配置文件
vim /etc/mysql/my.cnf
添加一行配置,跳過(guò) mysql 密碼驗(yàn)證
skip-grant-tables
重啟容器
docker restart 9e6
重新計(jì)入 mysql 容器,連接 mysql,不需要密碼就可以連接上 mysql。
mysql
修改登錄密碼
update mysql.user set authentication_string=PASSWORD('123456') where User='root';
重啟容器
7.2 沒(méi)有映射 mysql 文件夾
cd /home/hss/mysql/etc sudo chmod 777 mysql -R
拷貝 mysql 文件夾
7.3 mysql data 文件夾沒(méi)有權(quán)限
sudo chmod 777 /home/hss/mysql/data -R
7.4 安裝依賴(lài)包失敗
sudo apt-get install -y libnl-dev libnl-3-dev libnl-genl-3.dev Reading package lists... Done Building dependency tree Reading state information... Done Note, selecting 'libnl-genl-3-dev'for regex 'libnl-genl-3.dev' Package libnl-dev is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source
E: Package 'libnl-dev' has no installation candidate
解決方案:更新包
sudo apt-get -y update
7.5 更新包失敗
Err:15 https://download./linux/ubuntu bionic Release
Could not wait for server fd - select (11: Resource temporarily unavailable) [IP: 198.18.0.39 443]
Reading package lists... Done
E: The repository 'https://download./linux/ubuntu bionic Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
解決方案:
修改配置文件
sudo mv /etc/apt/sources.list /etc/apt/sources.list.backup sudo vim /etc/apt/sources.list
配置內(nèi)容如下:
deb http://archive./ubuntu/ trusty main restricted universe multiverse deb http://archive./ubuntu/ trusty-security main restricted universe multiverse deb http://archive./ubuntu/ trusty-updates main restricted universe multiverse deb http://archive./ubuntu/ trusty-proposed main restricted universe multiverse deb http://archive./ubuntu/ trusty-backports main restricted universe multiverse
執(zhí)行更新
sudo apt-get update
依賴(lài)包版本太高
apt-get install libnl-dev
The following packages have unmet dependencies: libnl-3-dev : Depends: libnl-3-200 (= 3.2.21-1ubuntu4.1) but 3.2.29-0ubuntu3 is to be installed Conflicts: libnl-dev but 1.1-8ubuntu1 is to be installed libnl-genl-3-dev : Depends: libnl-genl-3-200 (= 3.2.21-1ubuntu4.1) but 3.2.29-0ubuntu3 is to be installed E: Unable to correct problems, you have held broken packages.
root@node1:/usr/local/keepalived/etc/keepalived# service keepalived restart Failed to restart keepalived.service: Unit keepalived.service is masked. root@node1:/usr/local/keepalived/etc/keepalived# systemctl status keepalived.service ● keepalived.service Loaded: masked (/dev/null; bad) Active: inactive (dead) Condition: start condition failed at Wed 2022-05-11 02:40:46 UTC; 1 day 3h ago
WARNING - default user 'keepalived_script'for script execution does not exist - please create.
解決方案:
配置文件的 global_defs 配置里面增加 script_user root
global_defs { script_user rot }
執(zhí)行腳本失敗
May 16 03:50:54 node1 Keepalived_vrrp[19855]: WARNING - script '/usr/local/keepalived/restart_mysql.sh' is not executable for uid:gid 0:0 - disabling. May 16 03:50:54 node1 Keepalived_vrrp[19855]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.