MySQL+Keepalived实现主主高可用方案

Wesley13
• 阅读 472

Mysql主主高可用方案

master配置

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

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

! Configuration File for keepalived

global_defs {

**router_id LVS_**MASTER-A

}

vrry_script mysql {

script "/opt/mysql.sh"

interval 2

weight -5

fall 2

rise 1

}

vrrp_instance VI_1 {

state MASTER

interface ens33

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

mysql

}

virtual_ipaddress {

192.168.200.210

}

}

}

[root@master ~]# vim /opt/mysql.sh

#!/bin/bash

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

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

then

systemctl stop keepalived

fi

~

[root@master ~]# chmod +x /opt/mysql.sh

[root@master ~]# systemctl start keepalived

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

inet 192.168.30.25/24 brd 192.168.30.255 scope global noprefixroute ens33

inet 192.168.200.210/32 scope global ens33

[root@master ~]# tail -f /var/log/messages

Apr  9 15:36:43 master ntpd_intres[3721]: host name not found: 0.centos.pool.ntp.org

Apr  9 15:36:43 master ntpd_intres[3721]: host name not found: 1.centos.pool.ntp.org

Apr  9 15:36:43 master ntpd_intres[3721]: host name not found: 2.centos.pool.ntp.org

Apr  9 15:36:43 master ntpd_intres[3721]: host name not found: 3.centos.pool.ntp.org

Apr  9 15:36:45 master Keepalived_vrrp[9706]: Sending gratuitous ARP on ens33 for 192.168.200.210

Apr  9 15:36:45 master Keepalived_vrrp[9706]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.200.210

Apr  9 15:36:45 master Keepalived_vrrp[9706]: Sending gratuitous ARP on ens33 for 192.168.200.210

Apr  9 15:36:45 master Keepalived_vrrp[9706]: Sending gratuitous ARP on ens33 for 192.168.200.210

Apr  9 15:36:45 master Keepalived_vrrp[9706]: Sending gratuitous ARP on ens33 for 192.168.200.210

Apr  9 15:36:45 master Keepalived_vrrp[9706]: Sending gratuitous ARP on ens33 for 192.168.200.210

Apr  9 15:40:01 master systemd: Started Session 40 of user root.

Apr  9 15:40:01 master systemd: Starting Session 40 of user root.

第二台master2从

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

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

! Configuration File for keepalived

global_defs {

**router_id LVS_**MASTER-B

}

vrry_script mysql {

script "/opt/mysql.sh"

interval 2

weight -5

fall 2

rise 1

}

vrrp_instance VI_1 {

state BACKUP

interface ens33

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

mysql

}

virtual_ipaddress {

192.168.200.210

}

}

}

测试VIP转移

[root@master1 ~]# vim /opt/mysql.sh

#!/bin/bash

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

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

then

systemctl stop keepalived

fi

[root@master1 ~]# chmod +x /opt/mysql.sh

[root@master1 ~]# systemctl start keepalived

[root@master1 ~]# tail -f /var/log/messages

[root@master ~]# systemctl stop mariadb

[root@master ~]# ip a |grep ens33

[root@localhost ~]# ip a |grep ens33

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

inet 192.168.30.25/24 brd 192.168.30.255 scope global noprefixroute ens33

2,在远程客户端 测试

Mysql 服务器授权

[root@localhost ~]# mysql -uroot

MariaDB [(none)]> grant all on *.* to 'root'@'192.168.30.%'identified by '123456';

MariaDB [(none)]> flush privileges;

通过vip 登陆测试

[root@localhost ~]# mysql -uroot -p123456 -h 192.168.30.210;

MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| sampdb             |

| test               |

| test01             |

| test02             |

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

点赞
收藏
评论区
推荐文章
blmius blmius
1年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
1年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
1年前
ASMSupport教程4.9 生成三元运算符
<p这节我们介绍如何用ASMSupport生成三元运算符(...?...:...)运算符。我们预计生成如下代码:</p<divid"scid:9D7513F9C04C4721824A2B34F0212519:935e30cc33214e0093ba9834f3a4e044"class"wlWriterEditableS
Wesley13 Wesley13
1年前
MySQL查询按照指定规则排序
1.按照指定(单个)字段排序selectfromtable_nameorderiddesc;2.按照指定(多个)字段排序selectfromtable_nameorderiddesc,statusdesc;3.按照指定字段和规则排序selec
Stella981 Stella981
1年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
1年前
Angular material mat
IconIconNamematiconcode_add\_comment_addcommenticon<maticonadd\_comment</maticon_attach\_file_attachfileicon<maticonattach\_file</maticon_attach\
Wesley13 Wesley13
1年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
helloworld_34035044 helloworld_34035044
5个月前
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
3A网络 3A网络
2个月前
开发一个不需要重写成 Hive QL 的大数据 SQL 引擎
开发一个不需要重写成HiveQL的大数据SQL引擎学习大数据技术的核心原理,掌握一些高效的思考和思维方式,构建自己的技术知识体系。明白了原理,有时甚至不需要学习,顺着原理就可以推导出各种实现细节。各种知识表象看杂乱无章,若只是学习
3A网络 3A网络
2个月前
理解 virt、res、shr 之间的关系(linux 系统篇)
理解virt、res、shr之间的关系(linux系统篇)前言想必在linux上写过程序的同学都有分析进程占用多少内存的经历,或者被问到这样的问题——你的程序在运行时占用了多少内存(物理内存)?通常我们可以通过t