这里使用keepalived实现mysql的双主热备高可用

实验环境:

主机名

IP

系统版本

软件版本

master

192.168.199.6/vip:192.168.199.111

Rhel7.4

Mysql8.0.17+Keepalived v1.3.5

openStack

192.168.199.7/vip:192.168.199.111

Rhel7.4

Mysql8.0.17+Keepalived v1.3.5

一、MySQL主主同步部署:

master上操作

[root@master ~]# grep -v '^#' /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

server-id=1

gtid-mode=on

enforce-gtid-consistency=on

log-slave-updates=on

log_bin = binlog

[root@master ~]# systemctl start mysqld

[root@master ~]# mysql -uroot -p密码

mysql> create user 'repl'@'192.168.199.%' identified with mysql_native_password by 'kavl7kAkkle!';

mysql> grant replication slave on *.* to 'repl'@'192.168.199.%';

mysql> flush privileges;

Openstack上操作:

[root@openstack ~]# grep -v '^#' /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

server-id=1

gtid-mode=on

enforce-gtid-consistency=on

log-slave-updates=on

log_bin = binlog

[root@master ~]# systemctl start mysqld

[root@master ~]# mysql -uroot -p密码

mysql> create user 'repl'@'192.168.199.%' identified with mysql_native_password by 'kavl7kAkkle!';

mysql> grant replication slave on *.* to 'repl'@'192.168.199.%';

mysql> flush privileges;

Master上操作:

mysql> change master to master_host='192.168.199.7',master_user='repl',master_password='kavl7kAkkle!';     指定主库IP,使用的用户,密码
mysql> start slave;

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.199.7

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: binlog.000002

Read_Master_Log_Pos: 697

Relay_Log_File: master-relay-bin.000008

Relay_Log_Pos: 647

Relay_Master_Log_File: binlog.000002

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Openstack上操作:

mysql> change master to master_host='192.168.199.6',master_user='repl',master_password='kavl7kAkkle!';     指定主库IP,使用的用户,密码
mysql> start slave;

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.199.6

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: binlog.000003

Read_Master_Log_Pos: 692

Relay_Log_File: openstack-relay-bin.000008

Relay_Log_Pos: 570

Relay_Master_Log_File: binlog.000003

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

二、配置mysql+keepalived高可用环境

Master上操作:

[root@master ~]# yum -y install keepalived

[root@master ~]# more /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id MASTER-HA

}

vrrp_script check_mysql_port {

script "/etc/keepalived/check_mysql.sh"

interval 2

weight -5

fall 2

rise 1

}

vrrp_instance VI_1 {

state MASTER

interface ens32

virtual_router_id 51

priority 101

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.199.111

}

track_script {

check_mysql_port

}

}

Openstack上操作:

[root@openstack ~]# yum -y install keepalived

[root@openstack ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id MASTER-HA

}

vrrp_script check_mysql_port {

script "/etc/keepalived/check_mysql.sh"

interval 2

weight -5

fall 2

rise 1

}

vrrp_instance VI_1 {

state BACKUP

interface ens33

virtual_router_id 51

priority 10

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.199.111

}

track_script {

check_mysql_port

}

}

切换脚本,keepalived做心跳检测,如果mysql服务挂了,就会停掉进程,此时从的keepalived做心跳检测会发现这个情况,VIP就会切换到从上。

[root@openstack ~]# vim /etc/keepalived/check_mysql.sh

#!/bin/bash

counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)

if [ "${counter}" -eq 0 ]; then

systemctl stop keepalived.service

fi

启动keepalived。

[root@master ~]# systemctl startkeepalived.service

[root@openstack ~]# systemctl start keepalived.service

三、创建应用账号:

[root@master ~]# mysql -uroot -pkavl7kAkkle!

mysql> create database hke;

mysql> create user 'yingyong'@'192.168.199.%' identified with mysql_native_password by 'kavl7kAkkle!';

mysql> grant all on hke.* to 'yingyong'@'192.168.199.%';

四、Mysql+keepalived故障转移的高可用测试:

通过mysql客户端通过VIP连接,查看是否能够连接成功。

[root@winter ~]# mysql -uyingyong -pkavl7kAkkle! -h 192.168.199.111

mysql> select @@hostname;

+------------+

| @@hostname |

+------------+

| master     |

+------------+

1 row in set (0.00 sec)

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| hke                |

| information_schema |

+--------------------+

2 rows in set (0.01 sec)

mysql> select * from hke.hke_user;

+----+---------+

| id | name    |

+----+---------+

|  1 | thunder |

|  2 | winter  |

|  3 | qq      |

+----+---------+

3 rows in set (0.04 sec)

默认情况下VIP是在master上的

[root@master ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

valid_lft forever preferred_lft forever

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:7d:60:e3 brd ff:ff:ff:ff:ff:ff

inet 192.168.199.6/24 brd 192.168.199.255 scope global ens32

valid_lft forever preferred_lft forever

inet 192.168.199.111/32 scope global ens32

valid_lft forever preferred_lft forever

inet6 fe80::97dd:b327:4a4e:afdb/64 scope link

valid_lft forever preferred_lft forever

停掉主的数据库:

mysql服务停了,keepalived也会停,从而vip资源将会切换到master2机器上。

[root@master ~]# systemctl stop mysqld

[root@master ~]# netstat -tlunp|grep mysqld

[root@master ~]# ps -ef|grep keepalived

root      80720  62500  0 21:43 pts/3    00:00:00 grep --color=auto keepalived

在master上查看地址,此时已经没有VIP了。

[root@master ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

valid_lft forever preferred_lft forever

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:7d:60:e3 brd ff:ff:ff:ff:ff:ff

inet 192.168.199.6/24 brd 192.168.199.255 scope global ens32

valid_lft forever preferred_lft forever

inet6 fe80::97dd:b327:4a4e:afdb/64 scope link

valid_lft forever preferred_lft forever

在openstack查看网卡地址:

[root@openstack ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

valid_lft forever preferred_lft forever

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:73:f6:6b brd ff:ff:ff:ff:ff:ff

inet 192.168.199.7/24 brd 192.168.199.255 scope global ens33

valid_lft forever preferred_lft forever

inet 192.168.199.111/32 scope global ens33

valid_lft forever preferred_lft forever

inet6 fe80::b4bd:5e9f:2805:dfe/64 scope link

valid_lft forever preferred_lft forever

在mysql客户端通过VIP连接,查看连接信息:

[root@winter ~]# mysql -uyingyong -pkavl7kAkkle! -h 192.168.199.111

mysql> select @@hostname;   此时连接的是从

+------------+

| @@hostname |

+------------+

| openstack  |

+------------+

1 row in set (0.01 sec)

再次启动主上的mysql和keepalived

[root@master ~]# systemctl start mysqld

[root@master ~]# systemctl start keepalived.service

[root@master ~]# ps -ef|grep mysqld|grep -v 'grep'

mysql     80918      1 15 21:57 ?        00:00:17 /usr/sbin/mysqld

[root@master ~]# ps -ef|grep keepalived |grep -v 'grep';

root      80990      1  0 21:58 ?        00:00:00 /usr/sbin/keepalived -D

root      80991  80990  0 21:58 ?        00:00:00 /usr/sbin/keepalived -D

root      80992  80990  0 21:58 ?        00:00:00 /usr/sbin/keepalived -D

查看master上的网卡:

[root@master ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

valid_lft forever preferred_lft forever

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:7d:60:e3 brd ff:ff:ff:ff:ff:ff

inet 192.168.199.6/24 brd 192.168.199.255 scope global ens32

valid_lft forever preferred_lft forever

inet 192.168.199.111/32 scope global ens32

valid_lft forever preferred_lft forever

inet6 fe80::97dd:b327:4a4e:afdb/64 scope link

valid_lft forever preferred_lft forever

VIP已经漂移过来了。

再在从上查看此时已经没有VIP了。

[root@openstack ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

valid_lft forever preferred_lft forever

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:73:f6:6b brd ff:ff:ff:ff:ff:ff

inet 192.168.199.7/24 brd 192.168.199.255 scope global ens33

valid_lft forever preferred_lft forever

inet6 fe80::b4bd:5e9f:2805:dfe/64 scope link

valid_lft forever preferred_lft forever

在mysql客户端通过VIP连接,查看连接信息:

[root@winter ~]# mysql -uyingyong -pkavl7kAkkle! -h 192.168.199.111

mysql> select @@hostname;  此时连接的主

+------------+

| @@hostname |

+------------+

| master     |

+------------+

1 row in set (0.00 sec)

keepalived+mysql双主热备的更多相关文章

  1. Mysql + keepalived 实现双主热备读写分离【转】

    Mysql + keepalived 实现双主热备读写分离 2013年6月16日frankwong发表评论阅读评论   架构图 系统:CentOS6.4_X86_64软件版本:Mysql-5.6.12 ...

  2. 高可用Mysql架构_Mysql主从复制、Mysql双主热备、Mysql双主双从、Mysql读写分离(Mycat中间件)、Mysql分库分表架构(Mycat中间件)的演变

    [Mysql主从复制]解决的问题数据分布:比如一共150台机器,分别往电信.网通.移动各放50台,这样无论在哪个网络访问都很快.其次按照地域,比如国内国外,北方南方,这样地域性访问解决了.负载均衡:M ...

  3. MySQL双主热备问题处理

    1. Slave_IO_Running: No mysql> show slave status\G *************************** 1. row *********** ...

  4. Mysql双主热备+LVS+Keepalived高可用部署实施手册

    MySQL复制能够保证数据的冗余的同时可以做读写分离来分担系统压力,如果是主主复制还可以很好的避免主节点的单点故障.然而MySQL主主复制存在一些问题无法满足我们的实际需要:未提供统一访问入口来实现负 ...

  5. Mysql双主热备+LVS+Keepalived高可用操作记录

    MySQL复制能够保证数据的冗余的同时可以做读写分离来分担系统压力,如果是主主复制还可以很好的避免主节点的单点故障.然而MySQL主主复制存在一些问题无法满足我们的实际需要:未提供统一访问入口来实现负 ...

  6. mysql双主热备

    先搭建mysql主从模式,主从请参考mysql 主从笔记 然后在在配置文件里添加如下配置 1 log_slave_updates= #双主热备的关键参数.默认情况下从节点从主节点中同步过来的修改事件是 ...

  7. Mysql主从复制,双主热备

    Mysql主从复制: 主从复制: 主机准备工作: 开启bin.Log 注意:server-id  是唯一的值 重启mysql:service mysql restart 查看是否开启成功: 查看当前状 ...

  8. 使用Keepalived实现Nginx的自动重启及双主热备高可用

    1.概述 之前我们使用Keepalived实现了Nginx服务的双机主备高可用,但是有几个问题没有解决,今天一起探讨一下. 1)在双机主备机制中,Keepalived服务如果宕了,会自动启用备机进行服 ...

  9. Mysql+Keepalived双主热备高可用操作记录

    我们通常说的双机热备是指两台机器都在运行,但并不是两台机器都同时在提供服务.当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,而且切换的时间非常短.MySQL双主复制,即互为Mast ...

随机推荐

  1. 基于【 centos7】四 || FastDFS集群+Nginx负载均衡

    1. 架构设计 1.1 架构图 FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用F ...

  2. Mac下安装和配置Vue项目

    题记:学vue有一段时间了,终于今天下定决心每日书写一篇学习笔记.1.访问node.js官网:https://nodejs.org/en/ 下载对应安装包.2.安装完成,在终端输入 : node -v ...

  3. 一步步用ABAP Development Tools连接SAP云平台上的ABAP编程环境

    使用ABAP Development Tools的项目创建向导: New->ABAP Cloud Project: Service Instance Connection,选择SAP Cloud ...

  4. vscode-golang跳转定义无效问题

    问题发现: 本来可以进行"ctrl"+点击或者F12,进行跳转定义的方式,突然失效了 问题分析: 1.辅助工具是否关闭 解决方案,进入首选项,设置,查找go.docsTool,选项 ...

  5. 查看mysql的bin-log日志

    1.查看有哪些binlog show binary logs; show master logs; 2.如何查看log_bin中的内容 show binlog events; 查看第一个binlog的 ...

  6. C C++输出格式 <转载>仅用于个人

    转载链接:C++ C C语言输出格式总结 1 一般格式    printf(格式控制,输出表列)    例如:printf("i=%d,ch=%c\n",i,ch);    说明: ...

  7. Map集合中get不存在的key值

    返回的值是null 测试代码 import java.util.HashMap; import java.util.Map; public class Test { public static voi ...

  8. [HNOI2007]分裂游戏 SG打表博弈

    结论:其实每一个巧克力都是一堆石子 它的石子数就是它到队尾的距离 打一个SG表即可 #include<bits/stdc++.h> using namespace std; typedef ...

  9. JAVA遇见HTML——JSP篇(JSP指令与动作元素)

  10. UPS电源运用在数据中心,有什么优势?

    UPS电源是每个数据中心为了保证服务器与计算设备不被电力线干扰与电能质量问题所影响的设备. 1.电源选择 运用在线式或是后备式UPS电源,均需依照微机设备的需求与经济条件所决定.若是经济条件相对较好, ...