MYSQL双主高可用方案部署实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL双主高可用方案部署实例相关的知识,希望对你有一定的参考价值。

king01与king02互为master-slave

[[email protected] ~]# mysql -uroot -pabcd.1234

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.130.202

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000004

          Read_Master_Log_Pos: 120

               Relay_Log_File: relay-bin.000012

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-bin.000004

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

                      。。。。。。。


[[email protected] ~]# mysql -uroot -pabcd.1234

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.201

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000009

          Read_Master_Log_Pos: 120

               Relay_Log_File: relay-bin.000013

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-bin.000009

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

                       。。。。。。。


king01安装keepalived

[[email protected] ~]# yum install -y libnl libnl-devel

[[email protected] ~]# cd /usr/local/src

[[email protected] src]# tar zxvf keepalived-1.2.19.tar.gz

[[email protected] src]# cd keepalived-1.2.19

[[email protected] keepalived-1.2.19]# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64

[[email protected] keepalived-1.2.19]# make && make install 

[[email protected] keepalived-1.2.19]# ln -s /usr/local/sbin/keepalived /sbin/

[[email protected] ~]# chkconfig --add keepalived

[[email protected] ~]# chkconfig --level 35 keepalived on

[[email protected] ~]# chkconfig --list |grep keepalived

keepalived      0:off   1:off   2:off   3:on    4:off   5:on    6:off


[[email protected] ~]# 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 192.168.200.15

   smtp_connect_timeout 30

   router_id king01

}

vrrp_script check_mysql {

        script "/usr/local/mysql/scripts/check_mysql.sh"

        interval 2

}

vrrp_instance v01 {

    state BACKUP

    interface eth0

    virtual_router_id 200

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.200

    }

        track_script {

        check_mysql

    }

}


[[email protected] ~]# vi /usr/local/mysql/scripts/check_mysql.sh 

#!/bin/bash

MYSQL=/usr/local/mysql/bin/mysql

MYSQL_HOST=localhost

MYSQL_USER=root

MYSQL_PASSWORD=abcd.1234

$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1

if [ $? == 0 ]

then

    echo " $host mysql login successfully "

    exit 0

else

    echo " $host mysql login faild"

    exit 1

fi


[[email protected] ~]# chmod a+x /usr/local/mysql/scripts/check_mysql.sh


[[email protected] ~]# service keepalived start

Starting keepalived:                                       [  OK  ]


[[email protected] ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 08:00:27:2b:d6:17 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.201/24 brd 192.168.130.255 scope global eth0

    inet 192.168.1.200/32 scope global eth0

    inet6 fe80::a00:27ff:fe2b:d617/64 scope link 

       valid_lft forever preferred_lft forever


[[email protected] ~]# tail -n 100 /var/log/messages 

Feb 23 05:09:08 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Transition to MASTER STATE

Feb 23 05:09:09 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Entering MASTER STATE

Feb 23 05:09:09 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) setting protocol VIPs.

Feb 23 05:09:09 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Sending gratuitous ARPs on eth0 for 192.168.1.200

Feb 23 05:09:09 king01 Keepalived_healthcheckers[11670]: Netlink reflector reports IP 192.168.1.200 added

Feb 23 05:09:14 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Sending gratuitous ARPs on eth0 for 192.168.1.200


以上是关于MYSQL双主高可用方案部署实例的主要内容,如果未能解决你的问题,请参考以下文章

Keepalived+Nginx实现双主高可用负载均衡

基于Keepalived实现LVS双主高可用集群

keepalived+mysql双主高可用配置

Haproxy+Keepalived实现网站双主高可用-理论篇

配置Keepalived实现mysql双主高可用

部署nginx+keepalived实现双机双主高可用性环境