通過Keepalived配置,實現(xiàn)MySQL集群高可用性。在主機(jī)之間免密管理,通過Ansible批量操作。安裝MySQL使用rpm包,通過yum命令直裝。通過Ansible配置MySQL服務(wù)組,進(jìn)行批量操作。
需求描述
為了防止MySQL集群在雙主模式下可能出現(xiàn)的單點故障風(fēng)險,利用Keepalived進(jìn)行配置,從而實現(xiàn)MySQL集群的高可用性。
環(huán)境準(zhǔn)備
|
|
---|---|
|
|
|
|
|
|
|
|
配置主機(jī)之間免密
1.使用ssh-keygen在Master-1上生成秘鑰
ssh-keygen
修改hosts文件,添加主機(jī)名映射(將Master-1作為控制節(jié)點)
vim /etc/hosts
拷貝秘鑰,實現(xiàn)Master-1免密管理其他三臺主機(jī)
for i in {102..104}; do ssh-copy-id root@192.168.100.$i; done
安裝Ansible
利用Ansible批量管理,Master-1作為控制節(jié)點對其他三臺主機(jī)做批量操作。
1.安裝Ansible
dnf install ansible-core
2.添加服務(wù)組
#在文件末尾添加如下參數(shù)
vim /etc/ansible/hosts
[mysql]
Master-2
Slave-1
Slave-2
3.ping測試,全綠表示免密成功,且主機(jī)之間通訊沒有問題。
ansible mysql -m ping
image-20241230151818273
安裝mysql
我這里采用rpm包進(jìn)行安裝,在4臺虛擬機(jī)上進(jìn)行安裝
#mysql下載地址
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.33-1.el9.x86_64.rpm-bundle.tar
這里已經(jīng)配置好本地yum倉庫,使用yum命令直裝
#Master-1節(jié)點執(zhí)行
dnf localinstall *.rpm
ansible mysql -m shell -a "dnf localinstall *.rpm -y"
啟動并修改mysql密碼
systemctl start mysqld
#獲取默認(rèn)密碼
cat /var/log/mysqld.log |grep password|awk '{print $NF}'
修改密碼
mysql --connect-expired-password -uroot -p"pM8t>RW:TtB>" -e "alter user 'root'@'localhost' identified by 'Qclr@123';flush privileges;"
使用ansible啟動其他三臺主機(jī)的mysql服務(wù)
#啟動mysql
ansible mysql -m shell -a "systemctl start mysqld"
獲取默認(rèn)密碼
ansible mysql -m shell -a "cat /var/log/mysqld.log |grep password|awk '{print \$NF}'"
修改密碼,這里為了方便操作,將密碼進(jìn)行統(tǒng)一
ansible mysql -m script -a "/root/reset_mysqpasswd.sh"
#批量腳本內(nèi)容
#!/usr/bin/env bash
list=(
"+.c43u!;A2T1"
":.CrA,4Ow+SP"
"RuL&YfF6aggB"
)
for i in${list[@]}
do
name=$(hostname)
if [[ $name == "Master-2" ]];then
mysql --connect-expired-password -uroot -p"$i" -e "alter user 'root'@'localhost' identified by 'Qclr@123';flush privileges;"
elif [[ $name == "Slave-1" ]];then
mysql --connect-expired-password -uroot -p"$i" -e "alter user 'root'@'localhost' identified by 'Qclr@123';flush privileges;"
elif [[ $name == "Slave-2" ]];then
mysql --connect-expired-password -uroot -p"$i" -e "alter user 'root'@'localhost' identified by 'Qclr@123';flush privileges;"
else
echo"bey~"
exit 1
fi
done
安裝keepalived
通過配置Keepalived在MYSQL雙主模式下實現(xiàn)VIP(虛擬IP)的動態(tài)漂移,以確保MYSQL數(shù)據(jù)庫的高可用性。
1.在Master-1和Master-2上安裝keepalived
dnf install keepalived -y
ssh Master-2 dnf install keepalived -y
修改配置文件,確定主備關(guān)系
! Configuration File for keepalived
global_defs {
router_id 1
script_user root
enable_script_security
}
vrrp_script check_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 3
weight -50
fall 2
rise 1
}
vrrp_instance VI_1{
state MASTER
interface ens160
mcast_src_ip 192.168.100.101
virtual_router_id 51
priority 101
advert_int 2
authentication{
auth_type PASS
auth_pass 123
}
virtual_ipaddress{
192.168.100.254/24
}
track_script{
check_mysql
}
}
編寫檢測腳本,當(dāng)發(fā)現(xiàn)mysql進(jìn)程不存在時,關(guān)閉keepalived實現(xiàn)VIP切換
#!/usr/bin/env bash
if pgrep mysqld 1>/dev/null ;then
true
else
systemctl stop keepalived.service
exit 1
fi
將配置文件拷貝至Master-2
scp -r /etc/keepalived/* Master-2:/etc/keepalived/
修改Master-2配置文件,將其設(shè)置為備份主機(jī)
啟動服務(wù),可以看到此時的vip在Master-1上
systemctl start keepalived
配置mysql主從復(fù)制
配置Master-1
修改Master-1服務(wù)器的mysql配置文件
vim /etc/my.cnf
#添加以下內(nèi)容↓↓↓
server_id=101
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
#開啟GTID
gtid_mode=ON
enforce_gtid_consistency=ON
添加完畢后重啟mysql服務(wù),使配置生效
systemctl restart mysqld
創(chuàng)建數(shù)據(jù)庫同步用戶,并賦予權(quán)限
mysql -uroot -p"Qclr@123" <<EOF
create user 'qclr'@'%' identified by 'Qclr@123';
grant replication slave,replication client on *.* to 'qclr'@'%';
flush privileges;
EOF
配置Master-2
修改Master-2服務(wù)器的mysql配置文件
vi /etc/my.cnf
#添加以下內(nèi)容↓↓↓
server_id=102
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
#開啟GTID
gtid_mode=ON
enforce_gtid_consistency=ON
添加完畢后重啟mysql服務(wù),使配置生效
systemctl restart mysqld
與Master-1一樣,創(chuàng)建一個數(shù)據(jù)庫同步用戶,并賦予權(quán)限
mysql -uroot -p"Qclr@123" <<EOF
create user 'qclr'@'%' identified by 'Qclr@123';
grant replication slave,replication client on *.* to 'qclr'@'%';
flush privileges;
EOF
配置slave-1
修改mysql配置文件,添加如下內(nèi)容
vim /etc/my.cnf
## 設(shè)置 server_id,同一局域網(wǎng)中需要唯一
server_id=103
binlog-ignore-db=mysql
log-bin=mall-mysql-slave1-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=mall-mysql-relay-bin
read_only=1
#開啟GTID
gtid_mode=ON
enforce_gtid_consistency=ON
重啟mysql服務(wù)使配置文件生效
systemctl restart mysqld
配置主從同步
登錄slave節(jié)點,指定Master數(shù)據(jù)庫信息
mysql -uroot -p"Qclr@123"<<EOF
change replication source to source_host='192.168.100.254',source_port=3306,source_user='qclr',source_password='Qclr@123',source_auto_position=1;
start slave;
flush privileges;
EOF
#開啟主從同步
mysql> start slave;
#查看從數(shù)據(jù)庫是否開啟同步
mysql> show slave status \G;
#如配置出錯,則需要先停止再重置
stop slave
reset slave
reset master
出現(xiàn)下圖兩個YES,表示配置成功
配置Slave-2
修改mysql配置文件,添加如下內(nèi)容
vim /etc/my.cnf
## 設(shè)置 server_id,同一局域網(wǎng)中需要唯一
server_id=104
binlog-ignore-db=mysql
log-bin=mall-mysql-slave1-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=mall-mysql-relay-bin
read_only=1
#開啟GTID
gtid_mode=ON
enforce_gtid_consistency=ON
重啟mysql服務(wù)使配置文件生效
systemctl restart mysqld
登錄slave節(jié)點,指定Master數(shù)據(jù)庫信息
mysql -uroot -p"Qclr@123"<<EOF
change replication source to source_host='192.168.100.254',source_port=3306,source_user='qclr',source_password='Qclr@123',source_auto_position=1;
start slave;
flush privileges;
EOF
#開啟主從同步
mysql> start slave;
#查看從數(shù)據(jù)庫是否開啟同步
mysql> show slave status \G;
配置Master之間互為主從關(guān)系
注:為了防止雙主情況下數(shù)據(jù)庫生成的主鍵沖突,在MYSQL數(shù)據(jù)庫中設(shè)置Master-1和Master-2的自增初始值(auto_increment_offset)分別為1,2,設(shè)置增長步長(auto_increment_increment)都為2
#Master-1配置文件末尾添加如下內(nèi)容
auto_increment_increment=2
auto_increment_offset=1
#Master-2配置文件末尾添加如下內(nèi)容
auto_increment_increment=2
auto_increment_offset=2
1.配置兩個主節(jié)點之間互相復(fù)制數(shù)據(jù)
#Master-1
mysql -uroot -p"Qclr@123"<<EOF
change replication source to source_host='192.168.100.102',source_port=3306,source_user='qclr',source_password='Qclr@123',source_auto_position=1;
start slave;
flush privileges;
EOF
#Master-2
mysql -uroot -p"Qclr@123"<<EOF
change replication source to source_host='192.168.100.101',source_port=3306,source_user='qclr',source_password='Qclr@123',source_auto_position=1;
start slave;
flush privileges;
EOF
#開啟主從同步
mysql> start slave;
#查看從數(shù)據(jù)庫是否開啟同步
mysql> show slave status \G;
Master-1
Master-2
效果測試
在Master-2創(chuàng)建新數(shù)據(jù)庫
在其他三個節(jié)點上查看,可以看到數(shù)據(jù)已被同步
模擬Master-1故障,查看vip是否會切換至Master-2
關(guān)閉Master-1的mysql服務(wù)
在Master-2查看,可以看到VIP已切換至Master-2
查看slave-1和2的同步狀態(tài),可以看到從庫不受影響,同步狀態(tài)正常
再次創(chuàng)建數(shù)據(jù)庫測試,主從同步正常
原文來源:https://mp.weixin.qq.com/s/T7Go5Krw2L_JFlXa0yzBpQ
來源:本文內(nèi)容搜集或轉(zhuǎn)自各大網(wǎng)絡(luò)平臺,并已注明來源、出處,如果轉(zhuǎn)載侵犯您的版權(quán)或非授權(quán)發(fā)布,請聯(lián)系小編,我們會及時審核處理。
聲明:江蘇教育黃頁對文中觀點保持中立,對所包含內(nèi)容的準(zhǔn)確性、可靠性或者完整性不提供任何明示或暗示的保證,不對文章觀點負(fù)責(zé),僅作分享之用,文章版權(quán)及插圖屬于原作者。
Copyright©2013-2025 ?JSedu114 All Rights Reserved. 江蘇教育信息綜合發(fā)布查詢平臺保留所有權(quán)利
蘇公網(wǎng)安備32010402000125
蘇ICP備14051488號-3技術(shù)支持:南京博盛藍(lán)睿網(wǎng)絡(luò)科技有限公司
南京思必達(dá)教育科技有限公司版權(quán)所有 百度統(tǒng)計