利用keepalived構建高可用MySQL High Application

      關於MySQL-HA,目前有多種解決方案,好比heartbeat、drbd、mmm、共享存儲,可是它們各有優缺點。heartbeat、 drbd配置較爲複雜,須要本身寫腳本才能實現MySQL自動切換;對於mmm,生產環境中不多有人 用,且mmm 管理端須要單獨運行一臺服務器上,要是想實現高可用,就得對mmm管理端作HA,這樣無疑又增長了硬件開支;對於共享存儲,我的以爲MySQL數據仍是放 在本地較爲安全,存儲設備畢竟存在單點隱患。mysql

  使用MySQL雙master+keepalived是一種很是好的解決方案,在MySQL-HA環境中,MySQL互爲主從關係,這樣就保證 了兩臺MySQL數據的一致性,而後用keepalived實現虛擬IP,經過keepalived自帶的服務監控功能來實現MySQL故障時自動切換。sql

  下面是MySQL-HA是實現方式:ubuntu

1、環境vim

MySQL-VIP:10.0.5.76 MySQL-master1:10.0.5.41 MySQL-master2:10.0.5.75 OS版本:ubuntu 14.04 LTS MySQL版本:5.5.43 Keepalived版本:v1.2.7 (08/14,2013)

2、Mysql Master to Master 配置安全

  一、配置MySQL-master1:10.0.5.41服務器

    a) 修改mysql配置文件ide

vim /etc/mysql/my.cnf user = mysql log-bin=mysql-bin server-id=1 binlog-ignore-db=mysql replicate-ignore-db=mysql,information_schema sync_binlog=1 auto_increment_offset=1 auto_increment_increment=2 slave-skip-errors=all 

    b) 新建受權用戶oop

/etc/init.d/mysql restart mysql>CREATE USER 'replication'IDENTIFIED BY '123456'; mysql>grant replication slave on *.* to 'replication'@'10.0.5.75' identified by '123456' with grant option; mysql> show master status \G *************************** 1. row *************************** File: mysql-bin.000032 Position: 107 Binlog_Do_DB: Binlog_Ignore_DB: mysql 1 row in set (0.00 sec)

    c) 將10.0.5.75設置爲10.0.5.41的主服務器測試

mysql>stop slave; mysql>change master to master_host='10.0.5.75',master_user='replication',master_password='123456', master_log_file='mysql-bin.000006',master_log_pos=107; mysql>start slave; mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.5.75 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000019 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 333 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql,information_schema Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 187 Relay_Log_Space: 490 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1

  二、配置MySQL-master2:10.0.5.75spa

    a) 修改配置文件

vim /etc/mysql/my.cnf user = mysql log-bin=mysql-bin server-id=2 binlog-ignore-db=mysql replicate-ignore-db=mysql,information_schema sync_binlog=1 auto_increment_offset=2 auto_increment_increment=2 slave-skip-errors=all 

      b) 新建受權用戶

/etc/init.d/mysql restart mysql>CREATE USER 'replication'IDENTIFIED BY '123456'; mysql>grant replication slave on *.* to 'replication'@'10.0.5.41' identified by '123456' with grant option; mysql> show master status\G *************************** 1. row *************************** File: mysql-bin.000019 Position: 107 Binlog_Do_DB: Binlog_Ignore_DB: mysql 1 row in set (0.00 sec)

     c) 將10.0.5.41設置爲10.0.5.75的主服務器

mysql>stop slave; mysql>change master to master_host='10.0.5.41',master_user='replication',master_password='123456', master_log_file='mysql-bin.000006',master_log_pos=107; mysql>start slave; mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.5.41 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000032 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 333 Relay_Master_Log_File: mysql-bin.000010 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: chris,ian Replicate_Ignore_DB: mysql,information_schema Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 187 Relay_Log_Space: 490 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2

  三、MySQL同步測試

   如上述均正確配置,如今任何一臺MySQL上更新數據都會同步到另外一臺MySQL。

3、配置keepalived

  一、在10.0.5.41上配置keepalived

apt-get install keepalived vim /etc/keepalived/keepalived.conf # Global Configuration global_defs { router_id MySQL-ha } # VRRP Configuration vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass root } virtual_ipaddress { 10.0.5.76 # * VIP } } # Virtual Server Configuration virtual_server 10.0.5.76 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP # Real Server 1 configuration real_server 10.0.5.41 3306 { weight 3 notify_down /etc/keepalived/kill_keepalived.sh TCP_CHECK { connection_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port=3306 } } }

  編寫檢測服務down後所要執行的腳本

vim  /etc/keepalived/kill_keepalived.sh #!/bin/sh pkill keepalived chmod +x /etc/keepalived/kill_keepalived.sh
/etc/init.d/keepalived start
 

  注:此腳本是上面配置文件notify_down選項所用到的,keepalived使用notify_down選項來檢查real_server的服務狀態,當發現real_server服務故障時,便觸發此腳本;咱們能夠看到,腳本就一個命令,經過pkill keepalived強制殺死keepalived進程,從而實現了MySQL故障自動轉移。另外,咱們不用擔憂兩個MySQL會同時提供數據更新操做, 由於每臺MySQL上的keepalived的配置裏面只有本機MySQL的IP+VIP,而不是兩臺MySQL的IP+VIP。

  二、在10.0.5.75上配置keepalived

apt-get install keepalived vim /etc/keepalived/keepalived.conf # Global Configuration global_defs { router_id MySQL-ha } # VRRP Configuration vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass root } virtual_ipaddress { 10.0.5.76 # * VIP } } # Virtual Server Configuration virtual_server 10.0.5.76 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP # Real Server 1 configuration real_server 10.0.5.75 3306 { weight 3 notify_down /etc/keepalived/kill_keepalived.sh TCP_CHECK { connection_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port=3306 } } }

  編寫檢測服務down後所要執行的腳本

vim  /etc/keepalived/kill_keepalived.sh #!/bin/sh pkill keepalived chmod +x /etc/keepalived/kill_keepalived.sh 
/etc/init.d/keepalived start

  中止MySQL服務,看keepalived健康檢查程序是否會觸發咱們編寫的腳本。

4、測試

  MySQL遠程登陸測試,選擇一臺內網機器10.0.5.74遠程登陸VIP,看是否能登陸,在登陸以前兩臺MySQL服務器都要受權容許從遠程登陸。

MySQL> grant all privileges on *.* to 'root'@'%' identified by 'root'; MySQL> flush privileges;  

  遠程登陸測試

mysql -uroot -proot -h 10.0.5.76 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10818 Server version: 5.5.43-0ubuntu0.14.04.1-log (Ubuntu) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

  keepalived故障轉移測試

  從10.0.5.74一直去ping  VIP,而後關閉10.0.5.41上的keepalived,正常狀況下VIP就會切換到10.0.5.75上面去

  開啓10.0.5.75上的keepalived,關閉10.0.5.41上的keepalived,看是否能自動切換,正常狀況下VIP又會屬於10.0.5.41

   MySQL故障轉移測試

  在10.0.5.41上關閉MySQL服務,看VIP是否會切換到10.0.5.75上

  開啓10.0.5.41上的MySQL和keepalived,而後關閉10.0.5.75上的MySQL,看VIP是否會切換到10.0.5.41上

相關文章
相關標籤/搜索