当前位置:   article > 正文

MySQL主备HA(keepalived)方案_mysql keepalive + vip的ha方案

mysql keepalive + vip的ha方案

序言:

方案:keepalived + MySQL  实现主备(1+1)互备高可用,keepalived为默认抢占模式

环境说明:

centOS   7.5

MySQL   5.7.28

keepalived-2.0.19

 

一、MySQL安装

1、安装检查

      1)rpm -qa | grep mysql      检查老版本

      2)rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64    删除

      3)whereis mysql;find / -name mysql

      4)rm -rf  xxx   删除3)中查到的内容

2、创建用户

      groupadd mysql      

      useradd -g mysql -d /usr/local/mysql  mysql

3、下载安装包,方法很多,不一一介绍(https://dev.mysql.com/downloads/mysql/ 官网下载)

4、安装准备

     tar xzvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz        解压

     mv mysql-5.7.28-linux-glibc2.12-x86_64/* /usr/local/mysql

     mkdir /data/mysql

     chown  -R  mysql:mysql  /data/mysql

     chown  -R  mysql:mysql  /usr/local/mysql

5、安装

     cd /usr/local/mysql/bin

     ./mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

     注意记录生成的临时密码,localhost后内容

6、编辑配置文件my.cnf

     [mysqld]
     datadir=/data/mysql
     port = 7006
     sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
     symbolic-links=0
     max_connections=1024
     innodb_file_per_table=1
     lower_case_table_names=1                    #表名大小写不明感,敏感为0

    [mysqld_safe]
    log-error=/data/mysql/mysqld-err.log
    pid-file=/data/mysql/mysqld.pid

7、启动

     /usr/local/mysql/support-files/mysql.server start

8、修改密码

     mysql -hlocalhost -uroot -p

     输入第5步生成的临时密码

     set password for root@localhost = password('newpasswd');

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

9、添加自动启动

     1)将服务文件拷贝到init.d下,并重命名为mysql
         [root@localhost /]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

         vi  /etc/init.d/mysqld

         添加:

         basedir=/usr/local/mysql

         datadir=/data/mysql
     2)赋予可执行权限
        [root@localhost /]# chmod +x /etc/init.d/mysqld
     3)添加服务
        [root@localhost /]# chkconfig --add mysqld
     4)显示服务列表
        [root@localhost /]# chkconfig --list

备库安装方法同上,不再赘述

二、MySQL主从配置

1、主备增加目录  

      mkdir  -p /data/log/mysql

2、MASTER   /etc/my.conf 修改

[mysqld]
datadir=/data/mysql
port = 7006
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
max_connections=1024
innodb_file_per_table=1
lower_case_table_names=1                    #表名大小写不明感,敏感为

server-id=100                               #IP地址最后一位
log_bin=/data/log/mysql/mysql-bin           #开启binlog 日志
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=4
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
slave_preserve_commit_order=ON
log_slave_updates=ON
relay-log=/data/log/mysql/relay-bin
relay-log-index=/data/log/mysql/relay-bin.index

 

3、重启MASTER

      service mysqld restart;

4、SLAVE   /etc/my.conf 修改

[mysqld]
datadir=/data/mysql
port = 7006
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
max_connections=1024
innodb_file_per_table=1
lower_case_table_names=1                       #表名大小写不明感,敏感为

server-id=200                               #IP地址最后一位
log_bin=/data/log/mysql/mysql-slave-bin     #开启binlog 日志
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=4
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
slave_preserve_commit_order=ON
log_slave_updates=ON
relay-log=/data/log/mysql/relay-bin
relay-log-index=/data/log/mysql/relay-bin.index

5、重启SLAVE

      service mysqld restart;

6、主机创建同步用户

     mysql -hlocalhost -uroot -p

     create user 'config'@'%' identified by '123456';
     grant replication slave on *.* to 'config'@'%' identified by '123456';

7、主机配置

     mysql -hlocalhost -uroot -p

     CHANGE MASTER TO
     MASTER_HOST='mysql-slave',
     MASTER_USER='config',
     MASTER_PASSWORD='123456',
     master_port=7006;

     // 设置多线程复制

      set global slave_parallel_type='logical_clock';
      show variables like 'slave_parallel_type';

      set global slave_parallel_workers=4;
      show variables like 'slave_parallel_workers';

      show processlist;

8、从机配置

     mysql -hlocalhost -uroot -p

     CHANGE MASTER TO
     MASTER_HOST='mysql-master',
     MASTER_USER='config',
     MASTER_PASSWORD='123456',
     master_port=7006;

      // 设置多线程复制

      set global slave_parallel_type='logical_clock';
      show variables like 'slave_parallel_type';

      set global slave_parallel_workers=4;
      show variables like 'slave_parallel_workers';

      show processlist;

9、启动salve配置

     start slave;

     show slave status\G

     输出结果中看到 I/O 线程和 SQL 线程都是 YES, 就表示成功。

10、启动master配置(要后启动master的备份配置,以避免config用户master再次同步slave失败)

     start slave;

     show slave status\G

     输出结果中看到 I/O 线程和 SQL 线程都是 YES, 就表示成功。

 

三、keepalived安装&配置

1、下载安装包(下载方式很多,不一一列举):

      登录 http://www.keepalived.org 下载所需要安装包,本人下载的是最新的 keepalived-2.0.19.tar.gz

2、将安装包上传到 /tmp 下,解压

      tar  -xzvf  keepalived-2.0.19.tar.gz

3、切换目录

      cd  /tmp/keepalived-2.0.19

4、配置检查: 

     ./configure --prefix=/usr/local/keepalived

     检查遇到问题解决(无问题跳过):

     1)ssl.h 不存在,需要安装 openssl

          a、下载 openssl

          b、安装

     2)如果仍提示 ssl.h 不存在

            export C_INCLUDE_PATH=/usr/local/openssl/include

            export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/openssl/lib

     3)*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

           rpm  -list|grep libnl  查看是否安装,如无安装下载安装,我的系统缺少 libnl3-devel,下载 libnl3-devel-3.2.28-4.el7.x86_64.rpm

           安装:rpm -ivh libnl3-devel-3.2.28-4.el7.x86_64.rpm

     注:问题解决后需要重新执行 ./configure --prefix=/usr/local/keepalived 检查。

5、安装:

     make && make install

6、初始化工作

     mkdir  -p /data/log/HAlog

     mkdir /etc/keepalived
     cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
     cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
     cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
     cp /tmp/keepalived-2.0.19/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

7、开机启动设置

     systemctl enable keepalived.service      #设置开机自动启动
     systemctl disable keepalived.service     #取消开机自动启动
     systemctl start keepalived.service         #启动服务
     systemctl restart keepalived.service      #重启服务
     systemctl stop keepalived.service         #停止服务
     systemctl status keepalived.service      #查看服务状态

    service keepalived start|stop|restart      启动、关闭、重启

8、keepalived配置

******************** master   /etc/keepalived/keepalived.conf *********************

global_defs {
   router_id master
}

vrrp_script check_run {
    script "/usr/local/mysql/bin/mysql_HA.sh"
    interval 10
}

vrrp_sync_group VG1 {
    group {
        VI_1
    }
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 150
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 123456
    }

    track_script {
        check_run
    }

    notify_master /usr/local/mysql/bin/master.sh
    notify_stop /usr/local/mysql/bin/stop.sh

    virtual_ipaddress {
        192.168.101.190
    }
}

******************** slave   /etc/keepalived/keepalived.conf *********************
global_defs {
   router_id slave
}

vrrp_script check_run {
    script "/usr/local/mysql/bin/mysql_HA.sh"
    interval 10
}

vrrp_sync_group VG1 {
    group {
        VI_1
    }
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 123456
    }

    track_script {
        check_run
    }

    notify_master /usr/local/mysql/bin/master.sh
    notify_stop /usr/local/mysql/bin/stop.sh

    virtual_ipaddress {
        192.168.101.190
    }
}

四、脚本

mysql_HA.sh

  1. #!/bin/bash
  2. count=1
  3. while true
  4. do
  5. /usr/local/mysql/bin/mysql -uroot -pliyc -e "show status;" > /dev/null 2>&1
  6. i=$?
  7. ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
  8. j=$?
  9. if [ $i = 0 ] && [ $j = 0 ]
  10. then
  11. exit 0
  12. else
  13. if [ $i = 1 ] && [ $j = 0 ]
  14. then
  15. exit 0
  16. else
  17. if [ $count -gt 5 ]
  18. then
  19. break
  20. fi
  21. let count++
  22. continue
  23. fi
  24. fi
  25. done
  26. service keepalived stop

master.sh

  1. #!/bin/bash
  2. Master_Log_File=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
  3. Relay_Master_Log_File=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
  4. Read_Master_Log_Pos=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
  5. Exec_Master_Log_Pos=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')
  6. i=1
  7. while true
  8. do
  9. if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
  10. then
  11. echo "ok"
  12. break
  13. else
  14. sleep 1
  15. if [ $i -gt 60 ]
  16. then
  17. break
  18. fi
  19. continue
  20. let i++
  21. fi
  22. done
  23. /usr/local/mysql/bin/mysql -uroot -pliyc -e "show master status;" > /data/log/HAlog/master_status_$(date "+%y%m%d-%H%M").txt

stop.sh

  1. #!/bin/bash
  2. M_File1=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show master status\G" | awk -F': ' '/File/{print $2}')
  3. M_Position1=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show master status\G" | awk -F': ' '/Position/{print $2}')
  4. sleep 1
  5. M_File2=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show master status\G" | awk -F': ' '/File/{print $2}')
  6. M_Position2=$(/usr/local/mysql/bin/mysql -uroot -pliyc -e "show master status\G" | awk -F': ' '/Position/{print $2}')
  7. i=1
  8. while true
  9. do
  10. if [ $M_File1 = $M_File1 ] && [ $M_Position1 -eq $M_Position2 ]
  11. then
  12. echo "ok"
  13. break
  14. else
  15. sleep 1
  16. if [ $i -gt 60 ]
  17. then
  18. break
  19. fi
  20. continue
  21. let i++
  22. fi
  23. done

 

五、验证

1、启动主、备keepalived,查看主机vip

     主机执行: ip  addr    可以看到  192.168.101.190  vip

     备机执行: ip  addr    无法看到  192.168.101.190  vip

2、用 192.168.101.190  vip 访问数据库

      确认可以访问

3、停主机MySQL

     主机执行: ip  addr    无法看到  192.168.101.190  vip

     备机执行: ip  addr    可以看到  192.168.101.190  vip

     数据库访问无影响,可登录备机  netstat  -an|grep  port(数据库端口)  查看确实连接到了备库。

 

六、一些检测

 tcpdump -i eth0 vrrp -n

***************************************************  完成 *****************************************************

     

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/458586
推荐阅读
相关标签
  

闽ICP备14008679号