双活mysql搭建

主从搭建配置可以参考一篇文章,这篇文章是基于上一篇文章开始配置的
https://www.cnblogs.com/zyp87/p/18295559


主机配置说明:

192.168.136.101  mysql01  centos7.9  2C4G
192.168.136.102  mysql02  centos7.9  2C4G
192.168.136.109  vip
防火墙主机互相放行
firewall-cmd --zone=public --permanent --add-rich-rule='rule family="ipv4" source address="192.168.136.101" accept'
firewall-cmd --zone=public --permanent --add-rich-rule='rule family="ipv4" source address="192.168.136.102" accept'
firewall-cmd --zone=public --permanent --add-rich-rule='rule family="ipv4" source address="192.168.136.109" accept'

 两台主机的配置都是一样,操作也一样,同步配置即可

1、获取两台主机的事务记录
show master status;

开始分别配置两个主机的从配置

CHANGE MASTER TO
MASTER_HOST='192.168.136.101',
MASTER_USER='rep',
MASTER_PASSWORD='xxxxxx',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;
start slave;
show slave status;

CHANGE MASTER TO
MASTER_HOST='192.168.136.102',
MASTER_USER='rep',
MASTER_PASSWORD='xxxxx',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;
start slave;
show slave status;

 2、尝试登录任意一台主机建表建库

 3、安装keepalived

wget https://www.keepalived.org/software/keepalived-2.3.0.tar.gz
mkdir /data01/keepalived
useradd keepalived
tar -zxf keepalived-2.3.0.tar.gz && cd keepalived-2.3.0
./configure --prefix=/data01/keepalived
make -j2 && make install

 4、配置keepalived

vi /data01/keepalived/etc/keepalived/keepalived.conf # mysql01 192.168.136.101

! Configuration File for keepalived

global_defs {
router_id mysql01
}

vrrp_script check_mysql {
script "/data01/keepalived/shell/check_mysql.sh"
interval 2
weight 2
}

vrrp_instance mysql-ha {
state BACKUP
interface ens33
virtual_router_id 51
priority 100 # 权重大的是主
nopreempt
advert_int 1

authentication {
auth_type PASS
auth_pass 1111
}

track_script {
check_mysql
}

virtual_ipaddress {
192.168.136.109
}
}

vi /data01/keepalived/etc/keepalived/keepalived.conf # mysql01 192.168.136.102

! Configuration File for keepalived

global_defs {
router_id mysql02
}

vrrp_script check_mysql {
script "/data01/keepalived/shell/check_mysql.sh"
interval 2
weight 2
}

vrrp_instance mysql-ha {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
nopreempt
advert_int 1

authentication {
auth_type PASS
auth_pass 1111
}

track_script {
check_mysql
}

virtual_ipaddress {
192.168.136.109
}
}

vi data01/keepalived/shell/check_mysql.sh

#!/bin/bash

mysqlcmd="/data01/mysql/mysql/bin/mysql"
user="xxxxx"
password=xxxxx!"
host="192.168.136.102"
port="3306"

$mysqlcmd -u$user -p$password -h$host -P$port -e "show status;" &> /dev/null
if [ $? -eq 0 ];then
echo "mysql_status=1"
exit 0
else
/usr/bin/systemctl stop keepalived
fi

5、启动配置查询是否开启了vip

6、关停主节点的mysql数据库,查看vip是否漂移

7、使用工具连接vip数据库,验证情况

手动启动vip的配置
sudo ip addr add 192.168.1.200/24 dev eth0
sudo ip link set dev eth0 up
sudo ip addr del 192.168.1.200/24 dev eth0

posted @ 2024-07-13 22:35  dididi崩了  阅读(6)  评论(0编辑  收藏  举报