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

[经验分享] keepalived+mysql实现高可用

[复制链接]

尚未签到

发表于 2018-10-5 09:57:37 | 显示全部楼层 |阅读模式
  为了响应公司需求,打造出更安全的mysql集群,能够实现mysql故障后切换,研究了几天终于有了成果,一起分享一下。
  首先介绍一下这套集群方案实现的功能
  1、mysql服务器故障后自动转移,修好后自动切回
  2、mysql服务故障自动转移,修好后自动切回
  3、可以实现在几秒钟内转移
  以下内容均是实验环境,请根据实际情况修改响应参数
  生产环境MySQL主主同步主键冲突处理 http://www.linuxidc.com/Linux/2013-07/86890.htm
  MySQL + KeepAlived + LVS 单点写入主主同步高可用架构实验 http://www.linuxidc.com/Linux/2013-05/84002.htm
  MySQL 主主同步配置 http://www.linuxidc.com/Linux/2013-05/83815.htm
  CentOS 6.3下MySQL主从复制笔记 http://www.linuxidc.com/Linux/2013-06/85983.htm
  Linux下的MySQL主主复制 http://www.linuxidc.com/Linux/2013-10/91683.htm
  实验环境:
  mysql1 ip:10.1.1.20
  mysql2  ip:10.1.1.21
  mysql vip:10.1.1.25
  三台机器均安装centos 6.5 32位(虚拟机环境)
  实验开始!!!
  一、安装mysql,并打造主主同步。
  相信主从同步大家都会做,一样的道理,主主同步就是两台机器互为主的关系,在任何一台机器上写入都会同步。
  安装mysql的过程不解释,yum就好啦
  配置主主同步
  1.配置 /etc/my.cnf
  [mysqld]
  datadir=/var/lib/mysql
  socket=/var/lib/mysql/mysql.sock
  user=mysql
Disabling symbolic-links is recommended to prevent assorted security risks
  symbolic-links=0
  log-bin=binlog  #开启binlog功能
  log-bin-index=binlog.index
  sync_binlog=0
  server_id = 1    #两台机器不能重复,一个1 一个2 就好
  [mysqld_safe]
  log-error=/var/log/mysqld.log
  pid-file=/var/run/mysqld/mysqld.pid
  2.分别在两台机器上配置同步账号
  10.1.1.20机器上:
  [root@localhost ~]# mysql
  Welcome to the MySQL monitor. Commands end with; or \g.

  Your MySQL connection>  Server version: 5.0.77-log Sourcedistribution
  Type 'help;' or '\h' for help. Type '\c' toclear the buffer.

  mysql> GRANT replication slave ON . TO'ab'@'%'>  Query OK, 0 rows affected (0.00 sec)
  mysql> flush privileges;
  Query OK, 0 rows affected (0.00 sec)
  10.1.1.21机器上:
  [root@localhost ~]# mysql
  Welcome to the MySQL monitor. Commands end with; or \g.

  Your MySQL connection>  Server version: 5.0.77-log Sourcedistribution
  Type 'help;' or '\h' for help. Type '\c' toclear the buffer.

  mysql> GRANT replication slave ON . TO'ab'@'%'>  Query OK, 0 rows affected (0.00 sec)
  mysql> flush privileges;
  Query OK, 0 rows affected (0.00 sec)
  注:由于本文是实验环境下编写,所以没考虑任何安全性问题,同步账号也是最高权限,请根据实际情况设置响应权限!!
  3.设置同步
  10.1.1.20机器上:
  mysql> flush tables with read lock;
  mysql> show master status;
  +---------------+----------+--------------+------------------+
  | File | Position | Binlog_Do_DB |Binlog_Ignore_DB |
  +---------------+----------+--------------+------------------+
  | binlog.000003 | 365 | | |
  +---------------+----------+--------------+------------------+
  1 row in set (0.03 sec)
  mysql> unlock tables;
  Query OK, 0 rows affected (0.03 sec)
  10.1.1.21机器上:
  mysql> change master tomaster_host='10.1.1.20', master_port=3306, master_user='ab',master_password='123', master_log_file='binlog.000003',master_log_pos=365;
  Query OK, 0 rows affected (0.06 sec)
  mysql> start slave;
  Query OK, 0 rows affected (0.00 sec)
  mysql> show slave status \G  #执行这命令后 注意观察下面这两个参数,必须要都是yes才行
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  同样的 反过来做相同操作
  10.1.1.21机器上:
  mysql> flush tables with read lock;
  mysql> show master status;
  +---------------+----------+--------------+------------------+
  | File | Position | Binlog_Do_DB |Binlog_Ignore_DB |
  +---------------+----------+--------------+------------------+
  | binlog.000004 | 207 | | |
  +---------------+----------+--------------+------------------+
  1 row in set (0.03 sec)
  mysql> unlock tables;
  Query OK, 0 rows affected (0.03 sec)
  10.1.1.20机器上:
  mysql> change master tomaster_host='10.1.1.21', master_port=3306, master_user='ab',master_password='123', master_log_file='binlog.000004',master_log_pos=207;
  Query OK, 0 rows affected (0.06 sec)
  mysql> start slave;
  Query OK, 0 rows affected (0.00 sec)
  mysql> show slave status \G  #执行这命令后 注意观察下面这两个参数,必须要都是yes才行
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  介此,主主同步打造完成,可以简单测试一下,分别在两个机器上写数据 看看会不会同步到另一台机器上
  PS:如果报错  Slave_IO_Running: NO  可以检查同步的账号是否创建正常!
  二、安装keepalived 并设置监控
  keepalived是安装在两台MySQL服务器上的
  首先安装keepalived 过程不解释就正常解压安装就好
  安装后配置 vim /etc/keepalived/keepalived.conf 内容如下
  10.1.1.20的配置文件
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  acassen@firewall.loc
  failover@firewall.loc
  sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_1 {
  state backup      #两台配置此处均是BACKUP
  interface eth0
  virtual_router_id 51
  priority 100      #优先级,另一台改为90 
  advert_int 1
  nopreempt          #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.1.1.25
  }
  }
  virtual_server 10.1.1.25 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  persistence_timeout 50        #会话保持时间 
  protocol TCP
  real_server 10.1.1.20 3306 {
  weight 3
  notify_down /tmp/nimei.sh    #检测到mysql服务挂了就执行这个脚本(脚本要自己写哈)
  TCP_CHECK {
  connect_timeout 10        #连接超时时间
  nb_get_retry 3            #重连次数 
  delay_before_retry 3      #重连间隔时间
  connect_port 3306        #健康检查端口 
  }
  }
  }
  10.1.1.21 的配置文件
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  acassen@firewall.loc
  failover@firewall.loc
  sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_1 {
  state backup
  interface eth0
  virtual_router_id 51
  priority 90
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.1.1.25
  }
  }
  virtual_server 10.1.1.25 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  persistence_timeout 50
  protocol TCP
  real_server 10.1.1.21 3306 {
  weight 3
  notify_down /tmp/nimei.sh
  TCP_CHECK {
  connect_timeout 10
  nb_get_retry 3
  delay_before_retry 3
  connect_port 3306
  }
  }
  } 
  编写监控mysql服务是否挂了的脚本,按照上面配置文件的位置编写脚本。
  vim /tmp/nimei.sh
  #!/bin/sh 
  pkill keepalived
  脚本很简单啊 就一句,目的是当keepalived检测到mysql服务挂了之后触发这个脚本,杀死keepalived进程,让另一台机器接管
  好 修改后启动keeplived服务
  介此整个集群搭建完成
  三、测试
  找一台机器用虚拟ip连接mysql
  [root@localhost html]# mysql -uab  -h 10.1.1.25 -p123
  Welcome to the MySQL monitor.  Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.1.66-log Source distribution
  Copyright (c) 2000, 2012, 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>


运维网声明 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.iyunv.com/thread-612533-1-1.html 上篇帖子: Mysql5.7.19安装步骤 下篇帖子: mysql-5.5.55 多实例安装总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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