这里使用keepalived实现mysql的双主热备高可用
实验环境:
主机名 |
IP |
系统版本 |
软件版本 |
master |
192.168.199.6/vip:192.168.199.111 |
Rhel7.4 |
Mysql8.0.17+Keepalived v1.3.5 |
openStack |
192.168.199.7/vip:192.168.199.111 |
Rhel7.4 |
Mysql8.0.17+Keepalived v1.3.5 |
一、MySQL主主同步部署:
master上操作
[root@master ~]# grep -v '^#' /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=1
gtid-mode=on
enforce-gtid-consistency=on
log-slave-updates=on
log_bin = binlog
[root@master ~]# systemctl start mysqld
[root@master ~]# mysql -uroot -p密码
mysql> create user 'repl'@'192.168.199.%' identified with mysql_native_password by 'kavl7kAkkle!';
mysql> grant replication slave on *.* to 'repl'@'192.168.199.%';
mysql> flush privileges;
Openstack上操作:
[root@openstack ~]# grep -v '^#' /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=1
gtid-mode=on
enforce-gtid-consistency=on
log-slave-updates=on
log_bin = binlog
[root@master ~]# systemctl start mysqld
[root@master ~]# mysql -uroot -p密码
mysql> create user 'repl'@'192.168.199.%' identified with mysql_native_password by 'kavl7kAkkle!';
mysql> grant replication slave on *.* to 'repl'@'192.168.199.%';
mysql> flush privileges;
Master上操作:
mysql> change master to master_host='192.168.199.7',master_user='repl',master_password='kavl7kAkkle!'; 指定主库IP,使用的用户,密码
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.199.7
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 697
Relay_Log_File: master-relay-bin.000008
Relay_Log_Pos: 647
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Openstack上操作:
mysql> change master to master_host='192.168.199.6',master_user='repl',master_password='kavl7kAkkle!'; 指定主库IP,使用的用户,密码
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.199.6
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 692
Relay_Log_File: openstack-relay-bin.000008
Relay_Log_Pos: 570
Relay_Master_Log_File: binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
二、配置mysql+keepalived高可用环境
Master上操作:
[root@master ~]# yum -y install keepalived
[root@master ~]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MASTER-HA
}
vrrp_script check_mysql_port {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.199.111
}
track_script {
check_mysql_port
}
}
Openstack上操作:
[root@openstack ~]# yum -y install keepalived
[root@openstack ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MASTER-HA
}
vrrp_script check_mysql_port {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 10
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.199.111
}
track_script {
check_mysql_port
}
}
切换脚本,keepalived做心跳检测,如果mysql服务挂了,就会停掉进程,此时从的keepalived做心跳检测会发现这个情况,VIP就会切换到从上。
[root@openstack ~]# vim /etc/keepalived/check_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived.service
fi
启动keepalived。
[root@master ~]# systemctl startkeepalived.service
[root@openstack ~]# systemctl start keepalived.service
三、创建应用账号:
[root@master ~]# mysql -uroot -pkavl7kAkkle!
mysql> create database hke;
mysql> create user 'yingyong'@'192.168.199.%' identified with mysql_native_password by 'kavl7kAkkle!';
mysql> grant all on hke.* to 'yingyong'@'192.168.199.%';
四、Mysql+keepalived故障转移的高可用测试:
通过mysql客户端通过VIP连接,查看是否能够连接成功。
[root@winter ~]# mysql -uyingyong -pkavl7kAkkle! -h 192.168.199.111
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| master |
+------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| hke |
| information_schema |
+--------------------+
2 rows in set (0.01 sec)
mysql> select * from hke.hke_user;
+----+---------+
| id | name |
+----+---------+
| 1 | thunder |
| 2 | winter |
| 3 | qq |
+----+---------+
3 rows in set (0.04 sec)
默认情况下VIP是在master上的
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:7d:60:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.199.6/24 brd 192.168.199.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.199.111/32 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::97dd:b327:4a4e:afdb/64 scope link
valid_lft forever preferred_lft forever
停掉主的数据库:
mysql服务停了,keepalived也会停,从而vip资源将会切换到master2机器上。
[root@master ~]# systemctl stop mysqld
[root@master ~]# netstat -tlunp|grep mysqld
[root@master ~]# ps -ef|grep keepalived
root 80720 62500 0 21:43 pts/3 00:00:00 grep --color=auto keepalived
在master上查看地址,此时已经没有VIP了。
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:7d:60:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.199.6/24 brd 192.168.199.255 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::97dd:b327:4a4e:afdb/64 scope link
valid_lft forever preferred_lft forever
在openstack查看网卡地址:
[root@openstack ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:73:f6:6b brd ff:ff:ff:ff:ff:ff
inet 192.168.199.7/24 brd 192.168.199.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.199.111/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::b4bd:5e9f:2805:dfe/64 scope link
valid_lft forever preferred_lft forever
在mysql客户端通过VIP连接,查看连接信息:
[root@winter ~]# mysql -uyingyong -pkavl7kAkkle! -h 192.168.199.111
mysql> select @@hostname; 此时连接的是从
+------------+
| @@hostname |
+------------+
| openstack |
+------------+
1 row in set (0.01 sec)
再次启动主上的mysql和keepalived
[root@master ~]# systemctl start mysqld
[root@master ~]# systemctl start keepalived.service
[root@master ~]# ps -ef|grep mysqld|grep -v 'grep'
mysql 80918 1 15 21:57 ? 00:00:17 /usr/sbin/mysqld
[root@master ~]# ps -ef|grep keepalived |grep -v 'grep';
root 80990 1 0 21:58 ? 00:00:00 /usr/sbin/keepalived -D
root 80991 80990 0 21:58 ? 00:00:00 /usr/sbin/keepalived -D
root 80992 80990 0 21:58 ? 00:00:00 /usr/sbin/keepalived -D
查看master上的网卡:
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:7d:60:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.199.6/24 brd 192.168.199.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.199.111/32 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::97dd:b327:4a4e:afdb/64 scope link
valid_lft forever preferred_lft forever
VIP已经漂移过来了。
再在从上查看此时已经没有VIP了。
[root@openstack ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:73:f6:6b brd ff:ff:ff:ff:ff:ff
inet 192.168.199.7/24 brd 192.168.199.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::b4bd:5e9f:2805:dfe/64 scope link
valid_lft forever preferred_lft forever
在mysql客户端通过VIP连接,查看连接信息:
[root@winter ~]# mysql -uyingyong -pkavl7kAkkle! -h 192.168.199.111
mysql> select @@hostname; 此时连接的主
+------------+
| @@hostname |
+------------+
| master |
+------------+
1 row in set (0.00 sec)