mysql使用keepalived做高可用
mysql+keepalived
先做两台服务器数据库互为主从
主库服务器配置
开启binlog服务
mkdir -pv /data/ //创建存放binlog日志目录
chown mysql.mysql /data //修改目录所有者和属组
vim /etc/my.cnf //mysql配置文件
server-id=175 //设置serverid
log-bin=/data/mysql-bin //设置logbin日志存放目录
systemctl restart mysqld //修改mysql文件,需要重启服务
创建一个提供远程复制的用户
mysql> grant replication slave on *.* to 'replication'@'%' identified by '0'; //创建一个只有复制权限的用户
mysql> flush privileges; //刷新数据表
mysql> reset master; //重新记载binlog日志
mysql> show master status\G
mysql> CHANGE MASTER TO //配置从库连接主库信息
MASTER_HOST='10.12.153.172',
MASTER_USER='replication',
MASTER_PASSWORD='0',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154,
MASTER_CONNECT_RETRY=10;
mysql>start slave;
mysql> show slave status\G
从库服务器配置
开启binlog服务
mkdir -pv /data/ //创建存放binlog日志目录
chown mysql.mysql /data //修改目录所有者和属组
vim /etc/my.cnf //mysql配置文件
server-id=175 //设置serverid
log-bin=/data/mysql-bin //设置logbin日志存放目录
systemctl restart mysqld //修改mysql文件,需要重启服务
创建一个提供远程复制的用户
mysql> grant replication slave on *.* to 'replication'@'%' identified by '0'; //创建一个只有复制权限的用户
mysql> flush privileges; //刷新数据表
mysql> reset master; //重新记载binlog日志
mysql> show master status\G
mysql> CHANGE MASTER TO //配置从库连接主库信息
MASTER_HOST='10.12.153.172',
MASTER_USER='replication',
MASTER_PASSWORD='0',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154,
MASTER_CONNECT_RETRY=10;
mysql>start slave;
mysql> show slave status\G
两台服务器安装
yum -y install keepalived
master端设置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master
}
vrrp_script check_run {
script "/etc/keepalived/keepalived_chech_mysql.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 89
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.246.100/24 #vip地址
}
track_script {
check_run
}
}
backup端设置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id backup
}
vrrp_script check_run {
script "/etc/keepalived/keepalived_check_mysql.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface ens33
virtual_router_id 89
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.246.100/24
}
track_script {
check_run
}
}
两台服务器都设置mysql检测脚本
vim /etc/keepalived/keepalived_check_mysql.sh
#!/bin/bash
/usr/bin/mysql -uroot -p'QianFeng@2019!' -e "show status" &>/dev/null
if [ $? -ne 0 ] ;then
# service keepalived stop
systemctl stop keepalived
fi
chmod +x /etc/keepalived/keepalived_check_mysql.sh
systemctl start keepalived //启动keepalived