Mysql5.7.22+Keepalived双主互备高可用集群

DB1:192.168.254.128
DB2:192.168.254.129
配置前进行校时操作
#安装ntpdate工具
yum install ntpdate -y
#使用ntpdate校时(后面的是ntp服务器)
ntpdate pool.ntp.org

配置mysql双主备

安装数据库链接(在主页数据库里面可以看到)
http://blog.51cto.com/10158955/1926574

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 'slave_cp'@'192.168.254.%' identified by 'pass';
grant replication slave on . to 'slave_cp'@'192.168.254.%';
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

配置keepalived实现MySQL双主高可用
128服务器上配置
! Configuration File for keepalived
global_defs {
#设置报警通知邮件地址,可以设置多个
notification_email { br/>[email protected]
}
#设置邮件的发送地址
notification_email_from [email protected]
#设置smtp server的地址,该地址必须是存在的
smtp_server 127.0.0.1
#设置连接smtp server的超时时间
smtp_connect_timeout 30
#运行Keepalived服务器的标识,发邮件时显示在邮件标题中的信息
router_id mysql_msun
}

检测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
}
}

129服务器上配置
! Configuration File for keepalived
global_defs {
#设置报警通知邮件地址,可以设置多个
notification_email {
br/>[email protected]
}
#设置邮件的发送地址
notification_email_from [email protected]
#设置smtp server的地址,该地址必须是存在的
smtp_server 127.0.0.1
#设置连接smtp server的超时时间
smtp_connect_timeout 30
#运行Keepalived服务器的标识,发邮件时显示在邮件标题中的信息
router_id mysql_msun
}

检测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

猜你喜欢

转载自blog.51cto.com/10158955/2129878
今日推荐