keepalived+mysql搭建

环境信息:

主节点a: 109.188.14.210

主节点b: 109.188.14.205

虚拟ip:109.188.14.201

主主搭建:

mysql添加参数:
log-bin=mysql-bin
auto_increment_increment=2
auto_increment_offset=1
binlog-ignore=mysql
binlog-ignore=information_schema
binlog-ignore=performance_schema
character_set_server=utf8

a及执行,填b机ip
CREATE USER 'cp_user'@'109.188.14.210' IDENTIFIED WITH mysql_native_password BY 'master2018';
GRANT REPLICATION SLAVE ON *.* TO 'cp_user'@'109.188.14.210';
b及执行,填a机ip
CREATE USER 'cp_user'@'109.188.14.205' IDENTIFIED WITH mysql_native_password BY 'master2018';
GRANT REPLICATION SLAVE ON *.* TO 'cp_user'@'109.188.14.205';

a机执行,填b机ip
CHANGE MASTER TO
       MASTER_HOST='109.188.14.205',
       MASTER_USER='cp_user',
       MASTER_PASSWORD='master2018',
       MASTER_LOG_FILE='binlog.000006',
       MASTER_LOG_POS=688;
   start slave;    
b机执行,填a机ip
CHANGE MASTER TO
       MASTER_HOST='109.188.14.205',
       MASTER_USER='cp_user',
       MASTER_PASSWORD='master2018',
       MASTER_LOG_FILE='binlog.000006',
       MASTER_LOG_POS=688;
 start slave;    
  两个节点都执行

tar 解压keepalived包
 yum install -y gcc gcc-c++ wget popt-devel openssl openssl-devel
 yum install -y libnl libnl-devel libnl3 libnl3-devel
 yum install -y libnfnetlink-devel
 ./configure --sysconf=/etc
 make && make install
 systemctl enable keepalived.service
 cd /etc/keepalived/
 cp keepalived.conf.sample keepalived.conf.bak
 mv keepalived.conf.sample keepalived.conf
 vi keepalived.conf

 [root@mysql8182 keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id mysql-ha
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 210
    priority 100 #优先级,高优先级竞选为master
    advert_int 1
    nopreempt #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高,另一台不要设置这个选项
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        109.188.14.201
    }
}

virtual_server 109.188.14.201 3306 {
    delay_loop 2
    lb_algo rr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
    real_server 109.188.14.210 3306 {
        weight 1
        notify_down /etc/keepalived/mysql.sh
        TCP_CHECK {
            connect_port 3306
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
        }
    }
}
[root@mysql8182 keepalived]# cat /etc/keepalived/mysql.sh
#!/bin/bash
pkill keepalived

chmod 777 /etc/keepalived/mysql.sh
副节点配置keepalived.conf文件的时候注意要把 
real_server 109.188.14.210 3306改为 real_server 109.188.14.205 3306
virtual_router_id 210 改为virtual_router_id 205
priority 100改为priority 90
去掉nopreempt 

两个节点都操作

 /mysql/keepalived-2.2.7/bin/keepalived -f  /etc/keepalived/keepalived.conf -D

查看状态有三个如下进程:

 

你可能感兴趣的:(mysql,mysql)