准备工作
系统:Centos7.6 / 64 位
MySql:v5.7.34
Keepalived:v1.2.24
Rpm:http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
二进制:https://downloads.mysql.com/archives/community/
MySql部署规划(双主互备)
实例 | IP | 端口 | 备注 |
---|---|---|---|
MySql | 10.88.40.134 | 3306 | 主1 |
MySql | 10.88.40.175 | 3306 | 主2 |
keepalived | 10.88.40.200 | 3306 | vip |
两台服务器安装Mysql(操作一样)
- 安装mysql
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
yum -y install mysql-server
- 启动mysql服务
service mysqld restart
- 重置密码
grep "password" /var/log/mysqld.log
mysql -u root -p 回车
密码:xxx
#更改密码策略为LOW
set global validate_password_policy=0;
#更改密码长度
set global validate_password_length=0;
alter user 'root'@'localhost' identified by '123456';
CREATE USER 'ab'@'%' IDENTIFIED BY '123456';
grant all on *.* to 'ab'@'%' identified by '123456' with grant option;
update mysql.user set host='%' where user='root';
flush privileges;
exit;
service mysqld restart
- 关闭防火墙
firewall-cmd --state //查看防火墙状态
systemctl stop firewalld.service //停止firewall
systemctl disable firewalld.service //禁止firewall开机启动
补充一下:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
设置主从同步
配置主库1my.cnf,末尾追加
# #将复制事件写入binlog,一台服务器既要做主库又要做从库,此选项必须开启
log-slave-updates=true
#### replication ####
replicate-ignore-db=information_schema
replicate-ignore-db=mysql
replicate-ignore-db=performace_schema
#启用二进制日志
log-bin=mysql-bin
#服务器唯一ID,一般取IP最后一段
server-id=134
sql-mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
service mysqld restart
配置主库2my.cnf,末尾追加
# #将复制事件写入binlog,一台服务器既要做主库又要做从库,此选项必须开启
log-slave-updates=true
#### replication ####
replicate-ignore-db=information_schema
replicate-ignore-db=mysql
replicate-ignore-db=performace_schema
#启用二进制日志
log-bin=mysql-bin
#服务器唯一ID,一般取IP最后一段
server-id=175
sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
service mysqld restart
查询主1master的状态
show master status;
配置主2服务器
#配置从服务器
mysql>change master to master_host='10.88.40.134',master_port=3306,master_user='ab',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=4219;
#注意语句中间不要断开,master_port为mysql服务器端口号(无引号),master_user为执行同步操作的数据库账户,“4219”无单引号(此处的4219就是show master status 中看到的position的值,这里的mysql-bin.000002就是file对应的值)
#启动从服务器复制功能
Mysql>start slave;
#检查从服务器复制功能状态:
mysql> show slave status\G;
【查询master的状态、配置主库2服务器】两台都要做,先做主1,再做主2
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
校验主从同步
配置keepalived
yum install keepalived
修改配置文件(主1)路径:/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MYSQL_HA #标识,双主相同
}
vrrp_instance VI_1 {
state BACKUP #两台配置此处均是BACKUP
interface ens33
virtual_router_id 51 #主备相同
priority 100 #优先级,另一台backup改为90
advert_int 1
nopreempt #不抢占,只在优先级高master的机器上设置即可,优先级低backup的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.88.40.239
}
}
virtual_server 10.88.40.239 3306 {
delay_loop 6 #每个6秒检查一次real_server状态
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind NAT #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 60 #会话保持时间,同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
real_server 10.88.40.139 3306 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /etc/keepalived/mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}
修改配置文件(主2)
! Configuration File for keepalived
global_defs {
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.88.40.239
}
}
virtual_server 10.88.40.239 3306 {
delay_loop 6
#lb_algo rr
#lb_kind NAT
persistence_timeout 60
protocol TCP
real_server 10.88.40.181 3306 {
weight 3
notify_down /etc/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
mysql.sh
#!/bin/bash
pkill keepalived
启动
systemctl start keepalived
tail -500f /var/log/messages