MySQL实战三:mysql+keepalived实现双机热备

CentOS7关闭防火墙和selinux
直接上命令 
在root用户下 
systemctl stop firewalld 
systemctl disable firewalld 
systemctl status firewalld 


临时关闭selinux 
setenforce 0 
永久关闭selinux 
#vi /etc/selinux/config 
把SELINUX=enforcing 改成 SELINUX=disabled 
重启电脑就可以了
------------------------------------------------------------------------




安装keepalived
#yum install keepalived -y




Keepalived-MySQL-master服务器配置
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived 
global_defs { 
   # 接收邮件地址 
   notification_email { 
     [email protected] 
     [email protected] 
     [email protected] 
   } 
   # 邮件发送地址 
   notification_email_from [email protected] 
   # 本地邮件服务器发邮件 
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id MYSQL_HA 
} 
# vrrp_instance 是一组逻辑分组的 VIP,可以写多个
vrrp_instance VI_1 { 
    state BACKUP  
    interface ens33 # 实例工作的网络接口 
    virtual_router_id 57 # VRRP 路由 ID 实例,每个实例是唯一的 
    priority 100 # 优先级,备服务器设置 90 
    advert_int 1 # 指定 VRRP 心跳包通告间隔时间,默认 1 秒 
    nopreempt 
    # VRRP 验证块 
    authentication { 
        auth_type PASS 
        auth_pass 1111 
    } 
    # VIP 定义块 
    virtual_ipaddress { 
        192.168.31.57/24 
    } 
} 
virtual_server 192.168.31.57 3306 {
delay_loop 6 
    protocol TCP 
    real_server 192.168.31.7 3306 { 
        notify_down "kill -9 $(cat /var/run/keepalived.pid)" 
        # 四层健康检查 
        TCP_CHECK { 
            connect_port 3306 #连接远程服务器的 TCP 端口  
            connect_timeout 3  # 连接远程服务器超时时间 
            nb_get_retry 3 # 最大重试次数 
            delay_before_retry 3 # 连续两个重试之间的延迟时间 
        } 
    } 
} 


Keepalived-MySQL-backup服务器配置
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived 
global_defs { 
   # 接收邮件地址 
   notification_email { 
     [email protected] 
     [email protected] 
     [email protected] 
   } 
   # 邮件发送地址 
   notification_email_from [email protected] 
   # 本地邮件服务器发邮件 
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id MYSQL_HA 
} 
# vrrp_instance 是一组逻辑分组的 VIP,可以写多个
vrrp_instance VI_1 { 
    state BACKUP  
    interface ens33 # 实例工作的网络接口 
    virtual_router_id 57 # VRRP 路由 ID 实例,每个实例是唯一的 
    priority 90	# 优先级,备服务器设置 90 
    advert_int 1 # 指定 VRRP 心跳包通告间隔时间,默认 1 秒  
    # VRRP 验证块 
    authentication { 
        auth_type PASS 
        auth_pass 1111 
    } 
    # VIP 定义块 
    virtual_ipaddress { 
        192.168.31.57/24 
    } 
} 
virtual_server 192.168.31.57 3306 {
delay_loop 6 
    protocol TCP 
    real_server 192.168.31.8 3306 { 
        notify_down "kill -9 $(cat /var/run/keepalived.pid)" 
        # 四层健康检查 
        TCP_CHECK { 
            connect_port 3306 #连接远程服务器的 TCP 端口  
            connect_timeout 3 # 连接远程服务器超时时间 
            nb_get_retry 3 # 最大重试次数 
            delay_before_retry 3	# 连续两个重试之间的延迟时间 
        } 
    } 
} 






启动keepalived
#systemctl start keepalived




Mysql-Mster添加远程登录用户权限
mysql> grant all on *.* to 'root'@'192.168.31.%' identified by '123456';





























猜你喜欢

转载自blog.csdn.net/qq_28710983/article/details/80938193
今日推荐