设为首页 收藏本站
查看: 1480|回复: 0

[经验分享] 利用keepalived构建高可用MySQL High Application

[复制链接]

尚未签到

发表于 2015-9-4 09:15:16 | 显示全部楼层 |阅读模式
        关于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 '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
    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 '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
    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上
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-109236-1-1.html 上篇帖子: LVS与keepalived 下篇帖子: LVS + keepalived的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表