mysql高可用部署(主主模式)
添加主从同步用户
1. 登陆mysql
mysql -uroot -p
2. 创建同步用户
CREATE USER 'sync_user'@'%' IDENTIFIED BY '1qaz!QAZ';
3. 为用户赋权
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'sync_user'@'%';
FLUSH PRIVILEGES;
配置mysql主主模式
两个节点均需执行
1. 登陆mysql
mysql -uroot -p
2. 查看bin文件
show variables like 'log_bin';
show master status;
3. 配置mysql主主同步
master2节点执行
stop slave;
CHANGE MASTER TO
MASTER_HOST='172.16.38.218',
MASTER_PORT=3306,
MASTER_USER='sync_user',
MASTER_PASSWORD='1qaz!QAZ',
MASTER_LOG_FILE='mysql-bin.000004', 根据步骤3中的内容填写
MASTER_LOG_POS=1041;
start slave;
show slave status \G;
use mysql;
update user set host='%' where user='root';
GRANT ALL ON *.* to 'root'@'%';
flush privileges;
master1节点执行
stop slave;
CHANGE MASTER TO
MASTER_HOST='172.16.38.220',
MASTER_PORT=3306,
MASTER_USER='sync_user',
MASTER_PASSWORD='1qaz!QAZ',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=1041;
start slave;
show slave status \G;
use mysql;
update user set host='%' where user='root';
GRANT ALL ON *.* to 'root'@'%';
flush privileges;
安装keepalived服务
使用yum安装
yum install keepalived -y
chkconfig keepalived on
配置keepalived
global_defs {
}
vrrp_script check_proxy {
script "killall -0 mysqld"
interval 2
weight -30
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP #主为MASTER、备为BACKUP
interface ens160
virtual_router_id 91
priority 100
advert_int 1
virtual_ipaddress {
172.16.38.13
}
track_script {
check_proxy
}
}
启动keepalived
设置开机自启
systemctl enable keepalived
启动keepalived
service keepalived restart