mysql5.7双主 高可用_Mysql5.7.22+Keepalived双主互备高可用集群

配置mysql双主备

DB1修改配置文件(需重启)

vi /etc/my.cnf

#在[mysqld]添加

server-id=166

#开启mysql日志功能

log-bin=mysql-bin

#定义日志命名格式

relay-log=mysql-relay-bin

#以下table复制过滤

replicate-wild-ignore-table=test.%

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=performance_schema.%

DB2修改配置文件(需重启)

vi /etc/my.cnf

#在[mysqld]添加

server-id=168

#开启mysql日志功能

log-bin=mysql-bin

#定义日志命名格式

relay-log=mysql-relay-bin

DB1,DB2分别创建复制帐号

mysql -u root -p

#创建用户slave_up允许从192.168.254网段登录

create user [email protected]%‘ identified by ‘pass‘;

grant replication slave on . to [email protected]%‘;

exit

DB1,DB2分别获取二进制日志信息

mysql -u root -p

#对数据库进行只读锁定(防止查看二进制日志同时有人对数据库修改操作)

flush tables with read lock;

#查询主机二进制文件信息

show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000005 | 154 | | | |

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

1 row in set (0.00 sec)

#解除只读锁定

unlock tables;

数据库里面的数据一定要相同!!!如果不同就要先做同步数据!

在DB1和DB2上分别设置对方为主服务器!

change master to

master_host=‘192.168.254.128‘ ,

master_user=‘slave_cp‘,

master_password=‘pass‘,

master_log_file=‘mysql-bin.000001‘,

master_log_pos=154;

change master to

master_host=‘192.168.254.129‘ ,

master_user=‘slave_cp‘,

master_password=‘pass‘,

master_log_file=‘mysql-bin.000001‘,

master_log_pos=154;

#启动slave

start slave;

#分别查看DB1,DB2是否正常工作

DB1:192.168.254.128服务器

show slave status\G

1. row

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.254.129

Master_User: slave_cp

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 154

Relay_Log_File: mysql-relay-bin.000002

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

DB2:192.168.254.129服务器

mysql> show slave status\G

1. row

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.254.128

Master_User: slave_cp

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000005

Read_Master_Log_Pos: 154

Relay_Log_File: mysql-relay-bin.000011

Relay_Log_Pos: 367

Relay_Master_Log_File: mysql-bin.000005

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

检验双主互备

①通过分别在两台服务器上使用show slave status\G,查询主库信息以及IO进程、SQL进程工作状态。若两台服务器的查询结果都为Slave_IO_Running: Yes,Slave_SQL_Running: Yes;则表示当前双主互备状态正常。

②在Mysql248数据库上建库建表,检查Mysql249上是否同步正常;然后在Mysql249上建库建表,检查Mysql248上是否同步正常。

成功之后退出MySQL

exit

检测haproxy脚本

vrrp_script chk_mysql {

script "/etc/keepalived/check_slave.sh"

interval 2

weight 2

}

#定义VRRP实例,实例名自定义

vrrp_instance mysql_msun {

#指定Keepalived的角色,MASTER主机 BACKUP备份

state BACKUP #此处两个都设置为BACKUP

#指定HA监测的接口

interface ens32

#虚拟路由标识,这个标识是一个数字(1-255),在一个VRRP实例中主备服务器ID必须一样

virtual_router_id 68

#优先级,数字越大优先级越高,在一个实例中主服务器优先级要高于备服务器

priority 100 #从服务器99

#设置主备之间同步检查的时间间隔单位秒

advert_int 1

#设置不抢占模式(DB1设置即可)

nopreempt

#设置验证类型和密码

authentication {

#验证类型有两种{PASS|HA}

auth_type PASS

#设置验证密码,在一个实例中主备密码保持一样

auth_pass 1689

}

track_script {

chk_mysql # 执行监控的服务

}

#定义虚拟IP地址,可以有多个,每行一个

virtual_ipaddress {

192.168.254.160

}

}

检测haproxy脚本

vrrp_script chk_mysql {

script "/etc/keepalived/check_slave.sh"

interval 2

weight 2

}

#定义VRRP实例,实例名自定义

vrrp_instance mysql_msun {

#指定Keepalived的角色,MASTER主机 BACKUP备份

state BACKUP #此处两个都设置为BACKUP

#指定HA监测的接口

interface ens32

#虚拟路由标识,这个标识是一个数字(1-255),在一个VRRP实例中主备服务器ID必须一样

virtual_router_id 68

#优先级,数字越大优先级越高,在一个实例中主服务器优先级要高于备服务器

priority 99 #从服务器99

#设置主备之间同步检查的时间间隔单位秒

advert_int 1

#设置不抢占模式(DB1设置即可)

#nopreempt

#设置验证类型和密码

authentication {

#验证类型有两种{PASS|HA}

auth_type PASS

#设置验证密码,在一个实例中主备密码保持一样

auth_pass 1689

}

track_script {

chk_mysql # 执行监控的服务

}

#定义虚拟IP地址,可以有多个,每行一个

virtual_ipaddress {

192.168.254.160

}

}

创建监控脚本

分别在两台服务器上面创建脚本,

chmod +x /etc/keepalived/check_slave.sh

要根据自己的情况修改

/etc/keepalived/check_slave.sh

#!/bin/bash

#This scripts is check for Mysql Slave status

Mysqlbin=/usr/bin/mysql

user=root

pw=‘a123456‘

port=3306

host=127.0.0.1

#最大延时

sbm=120

#Check for $Mysqlbin

if [ ! -f $Mysqlbin ];then

echo ‘Mysqlbin not found,check the variable Mysqlbin‘

exit 99

fi

#Get Mysql Slave Status

IOThread=$Mysqlbin -h $host -P $port -u$user -p$pw -e ‘show slave status\G‘ 2>/dev/null|grep ‘Slave_IO_Running:‘|awk ‘{print $NF}‘

SQLThread=$Mysqlbin -h $host -P $port -u$user -p$pw -e ‘show slave status\G‘ 2>/dev/null|grep ‘Slave_SQL_Running:‘|awk ‘{print $NF}‘

SBM=$Mysqlbin -h $host -P $port -u$user -p$pw -e ‘show slave status\G‘ 2>/dev/null|grep ‘Seconds_Behind_Master:‘|awk ‘{print $NF}‘

#Check if the mysql run

if [[ -z "$IOThread" ]];then

exit 1

fi

#Check if the thread run

if [[ "$IOThread" == "No" || "$SQLThread" == "No" ]];then

exit 1

elif [[ $SBM -ge $sbm ]];then

exit 1

else

exit 0

fi

原文:http://blog.51cto.com/10158955/2129878

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值