MySQL + Keepalived 双主热备搭建( 六 )

master_port=3308,master_user='repuser',master_password='repuser123',master_auto_position=1;start slave;
复制代码 
使用 show slave statusG 命令查看主从同步状态,IO 线程和 SQL 线程都为 YES 表示同步正常,主库 A 和主库 B 互为主从 。
 

MySQL + Keepalived 双主热备搭建

文章插图
部署 Keepalived下载并解压安装包wget https://www.keepalived.org/software/keepalived-2.2.4.tar.gztar -xzvf keepalived-2.2.4.tar.gz
复制代码安装相关依赖yum install kernel-devel openssl-devel popt-devel -y
复制代码安装 keepalived,设置开机自动启动mkdir /software/keepalivedcd keepalived-2.2.4./configure --prefix=/software/keepalivedmake && make installsystemctl enable keepalivedmkdir /etc/keepalived
复制代码配置 Keepalived主库 A 配置 Keepalived主库 A keepalived 配置文件,编辑 /etc/keepalived/keepalived.conf 文件:
 
Python">global_defs { router_id keep_mysql_repl_g1# 负载均衡标识,在局域网内应该是唯一的 }# vrrp_script 级别和 vrrp_instance 一样vrrp_script chk_mysql {# 配置虚拟脚本 chk_mysqlscript "/etc/keepalived/check_mysql.sh"# 执行脚本,检查 mysql 服务是否存活interval 3# 脚本执行间隔:秒}# vrrp_instancevrrp_instance v_mysql_1 { state BACKUP# 指定该 keepalived 节点的初始状态(MASTER|BACKUP)interface ens192# VRRP 实例绑定的网口,用于发送 VRRP 包virtual_router_id 200# 路由 ID,范围是 0-255,主备都一样priority 100# 指定优先级,优先级高的将成为 MASTERadvert_int 1# 指定发送 VRRP 广播的间隔 。单位是秒nopreempt# 设置为不抢占 。默认是抢占的authentication {# 身份验证auth_type PASS# 指定认证方式auth_pass mysql# 指定认证所使用的密码 mysql,主备都一样}track_script {# 调用"vrrp_script"的脚本chk_mysql# 增加一个跟踪脚本到网口上}virtual_ipaddress {# 虚拟 IP192.168.1.38/24 } }
复制代码 
主库 A 检查脚本,编辑 /etc/keepalived/check_mysql.sh 文件:
 
#!/bin/bash#/etc/keepalived/check_mysql.sh#chmod u+x /etc/keepalived/check_mysql.sh#Linux 7 使用,如果是配置Linux 6 需要修改脚本# MySQL账号密码mysql_user="root"mysql_pass="123456"# MySQL错误日志输出mysql_err="/mysql/log/3308/check_mysql_err.log"# MySQL杀进程脚本mysql_kill_session="/tmp/kill.sql"# MySQL连接字符串mysql_con="mysql -u${mysql_user} -p${mysql_pass} -S /mysql/data/3308/mysql.sock"source ~/.bash_profileif [ `ps -ef|grep -w "$0"|grep "/bin/sh*"|grep "?"|grep "?"|grep -v "grep"|wc -l` -gt 2 ];then#exit 0fifunction excute_query {$mysql_con -e "select 1 from dual;" 2>> $mysql_err


推荐阅读