MySQL高可用性之Keepalived+MySQL(雙主熱備)

來源:互聯網
上載者:User

MySQL高可用性之Keepalived+MySQL(雙主熱備)

環境描述:
OS:CentOS6.5_X64
MASTER:192.168.0.202
BACKUP:192.168.0.203
VIP:192.168.0.204

1、配置兩台Mysql主主同步
[root@master ~]# yum install mysql-server mysql -y
[root@master ~]# service mysqld start
[root@master ~]# mysqladmin -u root password 123.com
[root@master ~]# vi /etc/my.cnf  #開啟二進位日誌,設定id
[mysqld]
server-id = 1                    #backup這台設定2
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema      #忽略寫入binlog日誌的庫
auto-increment-increment = 2            #欄位變化增量值
auto-increment-offset = 1              #初始欄位ID為1
slave-skip-errors = all                      #忽略所有複製產生的錯誤     
[root@master ~]# service mysqld restart

#先查看下log bin日誌和pos值位置

master配置如下:

[root@ master ~]# mysql -u root -p123.com
mysql> GRANT  REPLICATION SLAVE ON *.* TO 'replication'@'192.168.0.%' IDENTIFIED  BY 'replication';
mysql> flush  privileges;
mysql> change  master to
    ->  master_host='192.168.0.203',
    ->  master_user='replication',
    ->  master_password='replication',
    ->  master_log_file='mysql-bin.000002',
    ->  master_log_pos=106;  #對端狀態顯示的值
mysql> start  slave;        #啟動同步

backup配置如下:

[root@backup ~]#  mysql -u root -p123.com
mysql> GRANT  REPLICATION SLAVE ON *.* TO 'replication'@'192.168.0.%' IDENTIFIED  BY 'replication';
mysql> flush  privileges;
mysql> change  master to
    ->  master_host='192.168.0.202',
    ->  master_user='replication',
    ->  master_password='replication',
    ->  master_log_file='mysql-bin.000002',
    ->  master_log_pos=106;
mysql> start  slave;

#主主同步配置完畢,查看同步狀態Slave_IO和Slave_SQL是YES說明主主同步成功。

在master插入資料測試下:

在backup查看是否同步成功:

可以看到已經成功同步過去,同樣在backup插入到user表資料,一樣同步過去,雙主就做成功了。

2、配置keepalived實現熱備
[root@backup ~]# yum install -y pcre-devel openssl-devel popt-devel #安裝依賴包

[root@master ~]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
[root@master ~]# tar zxvf keepalived-1.2.7.tar.gz
[root@master ~]# cd keepalived-1.2.7
[root@master ~]#./configure --prefix=/usr/local/keepalived
make && make install

#將keepalived配置成系統服務

[root@master ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@master ~]# mkdir /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[root@master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
 }
notification_email_from  [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA      #標識,雙主相同
 }
vrrp_instance VI_1 {
 state BACKUP          #兩台都設定BACKUP
 interface eth0
 virtual_router_id 51      #主備相同
 priority 100          #優先順序,backup設定90
 advert_int 1
 nopreempt            #不主動搶佔資源,只在master這台優先順序高的設定,backup不設定
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 virtual_ipaddress {
 192.168.0.204
 }
}
virtual_server 192.168.0.204 3306 {
 delay_loop 2
 #lb_algo rr              #LVS演算法,用不到,我們就關閉了
 #lb_kind DR              #LVS模式,如果不關閉,待命伺服器不能通過VIP串連主MySQL
 persistence_timeout 50  #同一IP的串連60秒內被分配到同一台真實伺服器
 protocol TCP
 real_server 192.168.0.202 3306 {  #檢測本地mysql,backup也要寫檢測本地mysql
 weight 3
 notify_down /usr/local/keepalived/mysql.sh    #當mysq服down時,執行此指令碼,殺死keepalived實現切換
 TCP_CHECK {
 connect_timeout 3    #連線逾時
 nb_get_retry 3      #重試次數
 delay_before_retry 3 #稍候再試時間
  }
}

 

[root@master ~]# vi /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
[root@master ~]# chmod +x /usr/local/keepalived/mysql.sh
[root@master ~]# /etc/init.d/keepalived start

 

#backup伺服器只修改priority為90、nopreempt不設定、real_server設定本地IP。


#授權兩台Mysql伺服器允許root遠程登入,用於在其他伺服器登陸測試!
mysql> grant all on *.* to'root'@'192.168.0.%' identified by '123.com';
mysql> flush privileges;
3、測試高可用性
1、通過Mysql用戶端通過VIP串連,看是否串連成功。
2、停止master這台mysql服務,是否能正常切換過去,可通過ip addr命令來查看VIP在哪台伺服器上。

3、可通過查看/var/log/messges日誌,看出主備切換過程
4、master伺服器故障恢複後,是否主動搶佔資源,成為活動伺服器。

本文永久更新連結地址:

相關文章

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.