Mysql双向复制+KeepAlived:配置mysql的高可用


说明:

   此文章是借鉴好朋友的文档 http://sunys.blog.51cto.com/8368410/1639820完成的

   主从同步的原理:http://732233048.blog.51cto.com/9323668/1616386






环境:

   mysql的版本:mysql-5.6.22

   系统:centos6.4

   master:192.168.186.132

   slave:192.168.186.133

   vip: 192.168.186.140          #虚拟ip,web服务器连接的ip

   防火墙和selinux关闭

 





步骤:

   配置mysql的单向复制:

   注意:mysql的安装步骤省略

   master:         #开启二进制日志文件

   vi /usr/local/mysql/my.cnf

   在[mysqld]下添加:

   server-id = 1

   log-bin = /opt/mysql/binlog/mysql-binlog          #二进制日志文件路径随便设置,最好单独放在一个目录下

   /etc/init.d/mysqld  restart                       #服务必须要重启restart,二进制日志才会生效


   slave:

   vi /usr/local/mysql/my.cnf

   在[mysqld]下添加:

   server-id = 2

   /etc/init.d/mysqld  restart                       #必须要用restart,用reload不生效


   master:          #对slave授权

   mysql -uroot -p123456
   grant replication slave on *.* to 'slave'@'192.168.186.133' identified by '123456';

   flush privileges;


   master:         #备份数据

   mysql -uroot -p123456

   flush tables with read lock;         #锁表,只读

   show master status;                  #查看此时的binlog位置和pos值,这个要记录下来

+---------------------+----------+--------------+------------------+-------------------+

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+---------------------+----------+--------------+------------------+-------------------+

| mysql-binlog.000044 |      412 |              |                  |                   |

+---------------------+----------+--------------+------------------+-------------------+

   打开另外一个终端:mysqldump -u root -p123456 --all-databases > /tmp/mysqldump.sql

   回到之前终端:unlock tables;         #解表

   注意:锁表--查看--备份--解表 顺序要注意

   scp /tmp/mysqldump.sql 192.168.186.133:/tmp/        #把备份数据拷到slave


   slave:       #导入数据

   mysql -uroot -p123456 < /tmp/mysqldump.sql

   

   slave:       #开始同步

   mysql -uroot -p123456 

   change master to master_host='192.168.186.132',master_user='slave',master_password='123456',master_log_file='mysql-binlog.000044',master_log_pos=412,master_port=3306;

   start slave;

   show slave status\G;         #查看是否成功,如下:出现两个Yes则成功

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.186.132

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-binlog.000044

          Read_Master_Log_Pos: 412

               Relay_Log_File: scj-relay-bin.000002

                Relay_Log_Pos: 286

        Relay_Master_Log_File: mysql-binlog.000044

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

    注意:

         第一个Yes:与连接相关(若为no,查看防火墙,selinux,是否授权grant等等)

         第二个Yes:与数据相关(若为no,查看数据是否拷过去,配置文件是否配置server-id参数)

         单向主从同步:不能在从机进行任何写操作,所有写操作都是在主机进行(注意:经过不断测试:在从库执行写操作,主从同步并不会断掉,只是从库数据不会同步到主库而已,且主从同步仍可正常工作)

         若从机出现问题(如:突然关机),恢复正常后会继续同步主库数据,一般不会出现主从断掉的情况


         如下图:Mysql双向复制+KeepAlived:配置mysql的高可用_第1张图片

         如上图:如果我们没有执行图中的这几步,而是跳过直接在slave上执行change master to,从库会从000044这个文件里的412开始同步主库上的数据,这样的话:主从同步仍可建立,只是主库与从库上的数据不一致,从库只有pos值为412之后的数据 


   

 



   配置mysql的双向复制:注意:在单向复制的基础上进行)

   slave:           #开启二进制日志文件

   vi /usr/local/mysql/my.cnf

   在[mysqld]下添加:

   log-bin = /opt/mysql/binlog/mysql-binlog        #保证两台主机上的这个文件名字一致

   /etc/init.d/mysqld  restart


   master:          #授权

   grant replication slave on *.* to 'slave'@'192.168.186.132' identified by '123456';    #在master上执行授权语句,会同步到slave上的(实质:slave给master授权)

   flush privileges;


   slave:          #查看二进制日志和pos信号值,记录下来

   show master status; 

+---------------------+----------+--------------+------------------+

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+---------------------+----------+--------------+------------------+

| mysql-binlog.000001 |      106 |              |                  |

+---------------------+----------+--------------+------------------+

   

   master:         #开始同步

   change master to master_host='192.168.186.133',master_user='slave',master_password='123456',master_log_file='mysql-binlog.000001',master_log_pos=106;

   start slave;

   show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.186.133

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-binlog.000001

          Read_Master_Log_Pos: 106

               Relay_Log_File: mysqld-relay-bin.000002

                Relay_Log_Pos: 254

        Relay_Master_Log_File: mysql-binlog.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

   注意:

       双向主从:当主库(主1)出现问题,则由从库(主2)接过任务继续对外提供服务,等到主库恢复正常后会去同步从库上的数据,达到主从数据一致,且主从同步仍正常使用






   搭建keepalived实现主从自动切换:

   原理:

       keepalived用来解决单点故障,提供vrrp(虚拟路由冗余协议)以及healthcheck功能,提供一个vip虚拟ip;keepalived的作用是检测服务器的状态,当其中一台服务器出现问题,则将它从组中剔除,恢复后再将它加进来

      参考文档:

               http://blog.csdn.net/jibcy/article/details/7826158

               http://bbs.nanjimao.com/thread-855-1-1.html

   注意:以下步骤在两台mysql服务器上都要操作

   安装keepalived:

    cd /usr/local/src/

    wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

    tar -zxf keepalived-1.2.15.tar.gz

    cd keepalived-1.2.15

    ./configure --prefix=/usr/local/keepalived

    make

    make install


    拷贝文件:

    cp -a /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/

    cp -a /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

    mkdir /etc/keepalived/

    cp -a /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

    cp -a /usr/local/keepalived/sbin/keepalived /usr/sbin/

    注意:

        /etc/sysconfig/keepalived /etc/keepalived/keepalived.conf 的路径一定要正确,因为在执行/etc/init.d/keepalived这个启动脚本时,会读取/etc/sysconfig/keepalived 和 /etc/keepalived/keepalived.conf 这两个文件


    修改配置文件:

    master:                #修改master配置文件

    mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.old

    vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    notification_email {
      [email protected]
    }
    notification_email_from root@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id mysql
}

vrrp_instance VI_1 {
    state master
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    #nopreempt

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.186.140
    }
}

virtual_server 192.168.186.140 3306 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    real_server 192.168.186.132 3306 {
        weight 3
        notify_down /etc/keepalived/killkeepalived.sh
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}


    slave:                #修改slave配置文件

    mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.old

    vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {  
    notification_email {  
      [email protected]  
    }  
    notification_email_from root@localhost  
    smtp_server 127.0.0.1  
    smtp_connect_timeout 30  
    router_id mysql  
}
 
vrrp_instance VI_1 {
    state backup      
    interface eth0
    virtual_router_id 51
    priority 100       
    advert_int 1
    #nopreempt         
         
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.186.140
    }
}
 
virtual_server 192.168.186.140 3306 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 50        
    protocol TCP
    real_server 192.168.186.133 3306 {
        weight 3
        notify_down /etc/keepalived/killkeepalived.sh  
        TCP_CHECK {
            connect_timeout 10        
            nb_get_retry 3           
            delay_before_retry 3      
            connect_port 3306        
        }
    }
}


    vi /etc/keepalived/killkeepalived.sh          #创建脚本

  #!/bin/sh  
  pkill keepalived

    注意:此脚本的作用:

          keepalived子进程会时刻监控本机的mysql服务,若mysql服务出现问题(如:stop),则会去执行此脚本,把keepalived进程杀掉(正常情况master会时刻发送vrrp通信给backup,当master出现问题(如:keepalived进程停掉),backup一段时间内接受不到master发来的vrrp信息,则slave会转变为master,接管任务继续对外提供服务)

          千万不要把killkeepalived.sh这个脚本放到/var/run/mysqld/这个目录下,否则每次开机都会把这个脚本删掉

    chmod 755 /etc/keepalived/killkeepalived.sh            #加执行权限

    chkconfig keepalived on                        #设置开机自动启动


    查看是否绑定vip:

    master:         #查看master日志文件

    /etc/init.d/keepalived start

    tail -f /var/log/messages

May  8 17:08:01 localhost Keepalived[10191]: Starting Keepalived v1.2.15 (05/07,2015)

May  8 17:08:01 localhost Keepalived[10192]: Starting Healthcheck child process, pid=10194

May  8 17:08:01 localhost Keepalived[10192]: Starting VRRP child process, pid=10195

May  8 17:08:01 localhost Keepalived_vrrp[10195]: Netlink reflector reports IP 192.168.186.132 added

May  8 17:08:01 localhost Keepalived_vrrp[10195]: Netlink reflector reports IP fe80::20c:29ff:fec7:fd75 added

May  8 17:08:01 localhost Keepalived_vrrp[10195]: Registering Kernel netlink reflector

May  8 17:08:01 localhost Keepalived_vrrp[10195]: Registering Kernel netlink command channel

May  8 17:08:01 localhost Keepalived_vrrp[10195]: Registering gratuitous ARP shared channel

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Netlink reflector reports IP 192.168.186.132 added

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Netlink reflector reports IP fe80::20c:29ff:fec7:fd75 added

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Registering Kernel netlink reflector

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Registering Kernel netlink command channel

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Opening file '/etc/keepalived/keepalived.conf'.

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Configuration is using : 11034 Bytes

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Using LinkWatch kernel netlink reflector...

May  8 17:08:01 localhost Keepalived_healthcheckers[10194]: Activating healthchecker for service [192.168.186.132]:3306

May  8 17:08:03 localhost Keepalived_vrrp[10195]: Opening file '/etc/keepalived/keepalived.conf'.

May  8 17:08:03 localhost Keepalived_vrrp[10195]: Configuration is using : 37169 Bytes

May  8 17:08:03 localhost Keepalived_vrrp[10195]: Using LinkWatch kernel netlink reflector...

May  8 17:08:03 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) Entering BACKUP STATE

May  8 17:08:03 localhost Keepalived_vrrp[10195]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

May  8 17:08:07 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) Transition to MASTER STATE

May  8 17:08:08 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) Entering MASTER STATE

May  8 17:08:08 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) setting protocol VIPs.

May  8 17:08:08 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.186.140

May  8 17:08:08 localhost Keepalived_healthcheckers[10194]: Netlink reflector reports IP 192.168.186.140 added

May  8 17:08:13 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.186.140

    

    slave:      #查看slave日志

    /etc/init.d/keepalived start

    tail -f /var/log/messages

.............

.............

May  8 17:11:42 localhost Keepalived_healthcheckers[10724]: Using LinkWatch kernel netlink reflector...

May  8 17:11:42 localhost Keepalived_healthcheckers[10724]: Activating healthchecker for service [192.168.186.133]:3306

May  8 17:11:42 localhost Keepalived_vrrp[10725]: VRRP_Instance(VI_1) Entering BACKUP STATE

May  8 17:11:42 localhost Keepalived_vrrp[10725]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]


    测试:

    master:         #停掉mysql服务,查看日志

    /etc/init.d/mysqld stop

    tail -f /var/log/messages

May  8 17:13:32 localhost Keepalived_healthcheckers[10194]: TCP connection to [192.168.186.132]:3306 failed !!!

May  8 17:13:32 localhost Keepalived_healthcheckers[10194]: Removing service [192.168.186.132]:3306 from VS [192.168.186.140]:3306

May  8 17:13:32 localhost Keepalived_healthcheckers[10194]: Executing [/var/run/mysqld/killkeepalived.sh] for service [192.168.186.132]:3306 in VS [192.168.186.140]:3306

May  8 17:13:32 localhost Keepalived_healthcheckers[10194]: Lost quorum 1-0=1 > 0 for VS [192.168.186.140]:3306

May  8 17:13:32 localhost Keepalived_healthcheckers[10194]: Remote SMTP server [0.0.0.0]:25 connected.

May  8 17:13:33 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) sending 0 priority

May  8 17:13:33 localhost Keepalived_vrrp[10195]: VRRP_Instance(VI_1) removing protocol VIPs.

May  8 17:13:33 localhost Keepalived[10192]: Stopping Keepalived v1.2.15 (05/07,2015)

May  8 17:13:37 localhost Keepalived_healthcheckers[10194]: Netlink reflector reports IP 192.168.186.140 removed


     slave:        #master端停掉mysql服务后查看slave日志

     tail -f /var/log/messages

May  8 17:14:02 localhost Keepalived_vrrp[10725]: VRRP_Instance(VI_1) Transition to MASTER STATE

May  8 17:14:03 localhost Keepalived_vrrp[10725]: VRRP_Instance(VI_1) Entering MASTER STATE

May  8 17:14:03 localhost Keepalived_vrrp[10725]: VRRP_Instance(VI_1) setting protocol VIPs.

May  8 17:14:03 localhost Keepalived_vrrp[10725]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.186.140

May  8 17:14:03 localhost Keepalived_healthcheckers[10724]: Netlink reflector reports IP 192.168.186.140 added

May  8 17:14:08 localhost Keepalived_vrrp[10725]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.186.140

    









注意:

    若有一台服务器出现问题,恢复后记得:

      不仅把mysql启动起来,还要把keepalived启动起来