【MySQL】MySQL 主备切换

MySQL 主备切换

搭建 MySQL(主主模式),实现数据同步和支持两边读写(但某一时刻只能写一边);利用 KeepAlived 设置虚拟 IP,一旦发生故障,切换备用 MySQL。

1、环境准备

1.1、MySQL 安装

IP port
192.168.186.121 30001
192.168.186.122 30001

【MySQL】MySQL 安装

1.2、KeepAlived 安装

yum install keepalived

2、配置

2.1、MySQL 配置

1、my.cnf

配置两个 MySQL 的 my.cnf,除了 server_id 不同,其他相同。

## my.cnf 文件
[mysqld]
port=30001
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL

# 服务器id (id 不能相同)
server_id = 1 

# 指定哪些库不同步,其他库默认都同步
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys

# 开启 binlog
log_bin=mysql-binlog
sync-binlog=0

# relay log
relay_log=mysql-relay-bin
log_slave_updates=1

2、设置互为主节点

# 进入 MySQL,相互设置主节点,设置参数如下
# master_host:主节点的 IP
# master_port:主节点的 端口
# master_user:用户
# master_password:密码
# master_log_file:bin-log 文件,可通过 show master status 查看
# master_log_pos:偏移位置,可通过 show master status 查看

# 对 192.168.186.121 设置主节点为 192.168.186.122
change master to master_host='192.168.186.122',master_port=30001,master_user='root',master_password='123456$',master_log_file='mysql-binlog.000004',master_log_pos=156;
# 192.168.186.121 开启从模式
start slave

# 对 192.168.186.122 设置主节点 192.168.186.121
change master to master_host='192.168.186.121',master_port=30001,master_user='root',master_password='123456$',master_log_file='mysql-binlog.000004',master_log_pos=156;
# 192.168.186.122 开启从模式
start slave

2.2、KeepAlived 配置

vim /etc/keepalived/keepalived.conf

1、广播模式

192.168.186.121 作为主 KeepAlived

global_defs {  
    router_id node01
}

# 执行 chk_mysql_port 脚本,检查 MySQL 是否运行
vrrp_script chk_mysql_port {
    script "/home/sh/chk_mysql.sh"   
    interval 2                     
    weight –5                      
    fall 2                         
    rise 1   
}

vrrp_instance VI_1 {  
    # 设置 BACKUP 状态,不抢占
    state BACKUP  
    interface ens33  
    virtual_router_id 51 
    priority 100  
    advert_int 1 
    # 广播模式 
    mcast_src_ip 192.168.186.121
    nopreempt
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    # 虚拟 IP
    virtual_ipaddress {  
        192.168.186.100  
    }
    track_script {               
        chk_mysql_port             
    }
}  

192.168.186.122 作为备 KeepAlived

global_defs {  
    router_id node01
}

vrrp_script chk_mysql_port {
    script "/home/sh/chk_mysql.sh"   
    interval 2                     
    weight –5                      
    fall 2                         
    rise 1   
}

vrrp_instance VI_1 {  
    state BACKUP  
    interface ens33
    # 与主的保持一致  
    virtual_router_id 51 
    priority 99  
    advert_int 1  
    mcast_src_ip 192.168.186.122
    nopreempt
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    virtual_ipaddress {  
        192.168.186.100  
    }  
    track_script {               
        chk_mysql_port             
    }
}  

2、单播模式

如果节点处于的网络被限制了广播或多播模式,那么就需要取消 Keepaalived 广播模式,设置单播模式。

192.168.186.121 作为主 KeepAlived

global_defs {  
    router_id node01
}

vrrp_script chk_mysql_port {
    script "/home/sh/chk_mysql.sh"
    interval 2
    weight –5
    fall 2
    rise 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51 
    priority 100
    advert_int 1
    Nopreempt
    # 本机地址
    unicast_src_ip  192.168.186.121
    # 单播节点列表
    unicast_peer {
        192.168.186.122
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.186.100  
    }
    track_script {
        chk_mysql_port
    }
}

192.168.186.122 作为备 KeepAlived

global_defs {  
    router_id node01
}

vrrp_script chk_mysql_port {
    script "/home/sh/chk_mysql.sh"
    interval 2
    weight –5
    fall 2
    rise 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51 
    priority 100
    advert_int 1
    Nopreempt
    # 本机地址
    unicast_src_ip  192.168.186.122
    # 单播节点列表
    unicast_peer {
        192.168.186.121
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.186.100  
    }
    track_script {
        chk_mysql_port
    }
}

2.3、chk_mysql.sh 脚本

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "30001"|wc -l)
if [ "${counter}" -eq 0 ]; then
    systemctl stop keepalived
fi

如果 Keepalived 执行检测脚本属于 root 用户,那么会执行失败。

需要输入命令 setenforce 0

3、验证

连接虚拟 IP,停用主 MySQL,查看备用 KeepAlived 能否正常切换节点。

# 查看 Keepalived 的运行日志,检查 KeepAlived 是否作身份切换
tail -f /var/log/messages

# 查看备用节点是否为虚拟 IP 
ip addr | grep <虚拟 IP>
创作不易,转载请注明出处: 【MySQL】MySQL 主备切换
上一篇
下一篇