Mysql雙向複製+KeepAlived:配置mysql的高可用

Mysql雙向複製+KeepAlived:配置mysql的高可用html


說明:mysql

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

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






環境:sql

   mysql的版本:mysql-5.6.22centos

   系統:centos6.4bash

   master:192.168.186.132服務器

   slave:192.168.186.133ide

   vip: 192.168.186.140          #虛擬ip,web服務器鏈接的ipoop

   防火牆和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參數)

         單向主從同步:不能在從機進行任何寫操做,全部寫操做都是在主機進行(注意:通過不斷測試:在從庫執行寫操做,主從同步並不會斷掉,只是從庫數據不會同步到主庫而已,且主從同步仍可正常工做)

         若從機出現問題(如:忽然關機),恢復正常後會繼續同步主庫數據,通常不會出現主從斷掉的狀況


         以下圖:wKioL1VK5I6zVMdBAAKIxcicKcA039.jpg

         如上圖:若是咱們沒有執行圖中的這幾步,而是跳過直接在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 {
      732233048@qq.com
    }
    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 {  
      732233048@qq.com  
    }  
    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啓動起來

相關文章
相關標籤/搜索