MySQL+Keepalived實現雙機HA

來源:互聯網
上載者:User

標籤:

host1與host3互為主從,即host1為host3的主機,同時也為host3的從機 host1 192.168.203.131host2 192.168.203.132host3 192.168.203.133 1.安裝keepalivedwww.keepalived.org 2.注意先清空防火牆iptalbles -F 3.配置host1與host3互為主從並授權給host2上的登陸grant all on *.* to ‘replmonitor‘@‘192.168.203.%‘ identified by ‘000000‘ with grant option; 4.配置keealived.conf
! Configuration File for keepalivedhost1上:global_defs {   notification_email {     root@localhost.localdomain   }   notification_email_from root@localhost.localdomain   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id test-mysql-ha}vrrp_instance VI_1 {    state BACKUP     interface eth3 !當前機器的網路介面名稱    virtual_router_id 51    priority 100  !優先順序別    nopreempt   !重啟後也不要拿回主服務許可權    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.203.200  !虛擬ip地址,等會在host2上登陸用此ip    }}virtual_server 192.168.203.200 3306 { !此連接埠即為mysql的服務    delay_loop 1    lb_algo wrr    lb_kind DR    persistence_timeout 50    protocol TCP    real_server 192.168.203.131 3306 { !主機的ip地址        weight 1        notify_down /etc/keepalived/mysql.sh        TCP_CHECK {            connect_timeout 10            bingto 192.168.203.200            nb_get_retry 3            delay_before_retry 3            connect_port 3306        }    }host3上只需將上面加黑的部分做相應的更改並去掉nopreempt 即可 /etc/keepalived/mysql.sh的內容為:#!/bin/bashpkill keepalived
 5.host1與host3開啟keepalived,在host2上執行登陸mysql -h 192.168.203.200 -ureplmonitor -p (此host為虛擬ip地址) /etc/init.d/keepalived start 6.開始測試,我們將host1上的mysql停止[[email protected] keepalived]# /etc/init.d/mysql stop
Shutting down MySQL............ SUCCESS!
[[email protected] keepalived]# ps -ef | grep keepalived
root      5409  5115  0 00:51 pts/2    00:00:00 grep keepalived 在host3上查詢可知host3已經接管過虛擬主機 我們繼續在host2上執行動作陳述式  會首先提示串連失敗並重新串連,並得到查詢結果,故障失效遷移成功 7.在host3上執行:
mysql> delete from t1 where id in(4,5,6);Query OK, 3 rows affected (0.01 sec)在host1上重新開啟mysqlmysql> select * from t1;+------+| id   |+------+|    1 ||    2 ||    4 ||    5 ||    6 |+------+5 rows in set (0.00 sec)mysql> start slave;Query OK, 0 rows affected (0.03 sec)mysql> select * from t1;+------+| id   |+------+|    1 ||    2 |+------+2 rows in set (0.00 sec) 資料全部同步過來,再停掉host3上的mysql,在host1上插入一行資料:mysql> insert into t1 values(3);在host2上進行查詢:mysql> select * from testdb.t1;ERROR 2006 (HY000): MySQL server has gone awayNo connection. Trying to reconnect...Connection id:    5Current database: *** NONE ***mysql> select * from testdb.t1;+------+| id   |+------+|    1 ||    2 ||    3 |+------+3 rows in set (0.00 sec)

 

可見資料也過來了,keepalive可以進行高效的資料移轉工作。  

MySQL+Keepalived實現雙機HA

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.