码迷,mamicode.com
首页 > 移动开发 > 详细

利用keepalived构建高可用MySQL High Application

时间:2015-07-13 20:17:00      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

      关于MySQL-HA,目前有多种解决方案,比如heartbeat、drbd、mmm、共享存储,但是它们各有优缺点。heartbeat、 drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换;对于mmm,生产环境中很少有人 用,且mmm 管理端需要单独运行一台服务器上,要是想实现高可用,就得对mmm管理端做HA,这样无疑又增加了硬件开支;对于共享存储,个人觉得MySQL数据还是放 在本地较为安全,存储设备毕竟存在单点隐患。

  使用MySQL双master+keepalived是一种非常好的解决方案,在MySQL-HA环境中,MySQL互为主从关系,这样就保证 了两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换。

  下面是MySQL-HA是实现方式:

一、环境

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)

二、Mysql Master to Master 配置

  1、配置MySQL-master1:10.0.5.41

    a) 修改mysql配置文件

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) 新建授权用户

/etc/init.d/mysql restart
mysql>CREATE USER replicationIDENTIFIED 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

  2、配置MySQL-master2:10.0.5.75

    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 replicationIDENTIFIED 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

  3、MySQL同步测试

   如上述均正确配置,现在任何一台MySQL上更新数据都会同步到另一台MySQL。

三、配置keepalived

  1、在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。

  2、在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健康检查程序是否会触发我们编写的脚本。

四、测试

  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上

 

利用keepalived构建高可用MySQL High Application

标签:

原文地址:http://www.cnblogs.com/chrisDuan/p/4643535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!