192.168.10(MySQL,keepalived)192.168.20(MySQL,keepalived)192.168.100(连接测试)

  1. 构建MySQL互为主主同步

    (1)首先分别在10和20主机上安装MySQL

    (2)在两台主机上修改配置文件,开启bin-log日志

    (3)首先在10上做授权:grant  replication slave  on *.*  to slaveuser@"192.168.136.20" identified by "123456";

    (4)然后查看10的master状态:show master status;

    (5)在20服务器上进行同步配置:change  master  to master_host="192.168.136.10",master_user="slaveuser",master_password="123456",master_log_file="master.000001",master_log_pos=107;

    (6)开启slave:start slave

    (7)查看slave状态:show slave status\G

    (8)此时20是10的从,然后再20主机做相同的操作,让10作为20的从,然后两主机互为主主结构

    (9)分别在两台主机上操作数据库,查看是否同步

  2. 安装配置keepalived,故障转移

    (1)分别在10和20上安装keepalived

    (2)修改10的keepalived配置文件

global_defs {

   notification_email {

        18600135671@139.com

   }

   notification_email_from  18600135671@139.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id mysql-ha

}


vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 30

    priority 90  #设置优先级大

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.136.100

    }

}


virtual_server 192.168.136.100 3306 {

    delay_loop 2

    lb_algo wrr

    lb_kind DR

    persistence_timeout 60

    protocol TCP


    real_server 192.168.136.10 3306 {

        weight 3

        notify_down /var/lib/mysql/mysql.sh  #检测MySQL服务停掉之后执行的脚本


        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

(3)修改20的keepalived配置文件

global_defs {

   notification_email {

        18600135671@139.com

   }

   notification_email_from  18600135671@139.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id mysql-ha

}


vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 30

    priority 50  #设置优先级小

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.136.100

    }

}


virtual_server 192.168.136.100 3306 {

    delay_loop 2

    lb_algo wrr

    lb_kind DR

    persistence_timeout 60

    protocol TCP


    real_server 192.168.136.20 3306 {

        weight 3

        notify_down /usr/local/mysql/bin/mysql.sh


        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

(4)mysql.sh脚本文件

service network restart

service keepalived stop

sleep 10

mysql=`ps -ef | grep mysqld | grep -v grep`

if [ -z "$mysql" ];then

rm -rf /var/lib/mysql/mysql.sock

service mysqld start

fi

service keepalived start

(5)测试故障转移

首先ip a命令查看VIP是否出现

然后停掉10的MySQL服务,然后等下看MySQL是否自动启动

停掉10的keepalived服务,看20是否切换到VIP

然后在10和20授权30可以登录VIP MySQL

在30上连接VIP MySQL测试功能