mysql的高可用
环境说明
主机名 | IP地址 | 服务 | 系统版本 |
---|---|---|---|
master-mysql | 192.168.100.116 | keepliaved mariadb | red-hat7 |
backup-mysql | 192.168.100.169 | keepliaved mariadb | red-hat7 |
client | 192.168.100.33 | mariadb | red-hat7 |
关闭防火墙和Selinux
[root@master-mysql ~]# systemctl stop firewalld
[root@master-mysql ~]# systemctl disable firewalld
[root@master-mysql ~]# setenforce 0
下载keepliaved
[root@master-mysql ~]# yum -y install keepliaved
[root@backup-mysql ~]# yum -y install keepliaved
主服务端配置
[root@master-mysql ~]# cp //etc/keepalived/keepalived.conf /opt/
[root@master-mysql ~]# > /etc/keepalived/keepalived.conf
[root@master-mysql ~]# vim /etc/keepalived/keepalived.conf
[root@master-mysql ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id dsb1
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 10
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.200/24
}
}
virtual_server 192.168.100.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.116 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.169 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
\\重启服务
[root@master-mysql ~]# systemctl retart keepalived.service
[root@master-mysql ~]# 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:05:94:b0 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.116/24 brd 192.168.100.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.100.200/24 scope global secondary ens32
valid_lft forever preferred_lft forever
inet6 fe80::13e4:67f8:1586:dd18/64 scope link
valid_lft forever preferred_lft forever
从服务端配置
[root@backup-mysql ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id dsb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 10
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.200/24
}
}
virtual_server 192.168.100.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.169 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.116 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@backup-mysql ~]# 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:b9:a5:63 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.169/24 brd 192.168.100.255 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::bb22:4f4e:8e9:25fd/64 scope link
valid_lft forever preferred_lft forever
\测试,当master-mysql的keepliaved断掉时,地址是否出现漂移
[root@master-mysql ~]# systemctl stop keepalived.service
[root@backup-mysql ~]# 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:b9:a5:63 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.169/24 brd 192.168.100.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.100.200/24 scope global secondary ens32
valid_lft forever preferred_lft forever
inet6 fe80::bb22:4f4e:8e9:25fd/64 scope link
valid_lft forever preferred_lft forever
安装mysql
master端
[root@master-mysql ~]# yum -y install mariadb mariadb-server
[root@backup-mysql ~]# yum -y install mariadb mariadb-server
[root@master-mysql ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
\登录测试
[root@master-mysql ~]# mysql -uroot -p123.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
[root@backup-mysql ~]# mysql -uroot -p123.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
- 分别建库,以便等下测试
\master-mysql
MariaDB [(none)]> create database dsb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dsb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.01 sec)
\backup-mysql
MariaDB [(none)]> create database dxk;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dxk |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
- 在数据库里授权
//主
MariaDB [(none)]> grant all on *.* to root@'192.168.100.%' identified by '123.com'
-> ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
//从
MariaDB [(none)]> grant all on *.* to root@'192.168.100.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
- 客户端检测
[root@client ~]# mysql -uroot -p123.com -h 192.168.100.200
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dsb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.03 sec)
\当主服务端断掉后,数据库是否切换
[root@master-mysql ~]# systemctl stop keepalived.service
[root@backup-mysql ~]# ip a //IP地址出现转移
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:b9:a5:63 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.169/24 brd 192.168.100.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.100.200/24 scope global secondary ens32
valid_lft forever preferred_lft forever
inet6 fe80::bb22:4f4e:8e9:25fd/64 scope link
valid_lft forever preferred_lft forever
\\查看数据库
[root@client ~]# mysql -uroot -p123.com -h 192.168.100.200
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dxk |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.02 sec)
编写mysql状态检测脚本
[root@master-mysq keepalived]# pwd
/etc/keepalived
[root@master-mysq keepalived]# cat mysql_check.sh
#!/bin/bash
A=$(ps -C mysqld --no-header | wc -l)
if [ $A -eq 0 ];then
systemctl start mariadb.service
sleep 2
if [ $(ps -C mysqld --no-header | wc -l) -eq 0 ];then
pkill keepalived
fi
fi
- 编辑主服务端的配置文件
[root@master-mysq keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id dsb1
}
vrrp_script chk_mysql {
script "/etc/keepalived/mysql_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 10
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.100.200/24
}
}
//重启服务
[root@master-mysq keepalived]# systemctl restart keepalived.service
检测
[root@master-mysq keepalived]# systemctl stop mariadb.service
[root@master-mysq keepalived]# ss -lnpt //虽然停掉服务,但是脚本检测到,所以启动了服务
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=991,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1083,fd=13))
LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=3352,fd=13))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=991,fd=4))
LISTEN 0 100 ::1:25 :::*
//故意将Mysql的配置文件改错,让他启动不了
[root@master-mysq keepalived]# vim /etc/my.cnf
mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
//这是我们关掉mysql的服务
[root@master-mysq keepalived]# systemctl stop mariadb
[root@master-mysq keepalived]# 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:05:94:b0 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.116/24 brd 192.168.100.255 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::13e4:67f8:1586:dd18/64 scope link
valid_lft forever preferred_lft forever
//我们查看从服务端的地址
[root@backup ~]# 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:b9:a5:63 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.169/24 brd 192.168.100.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.100.200/24 scope global secondary ens32
valid_lft forever preferred_lft forever
inet6 fe80::bb22:4f4e:8e9:25fd/64 scope link
valid_lft forever preferred_lft forever
- 脑裂监控事例
[root@slave ~]# mkdir -p /scripts && cd /scripts
[root@slave scripts]# vim check_keepalived.sh
#!/bin/bash
while true
do
if [ `ip a show ens33 |grep 172.16.12.250|wc -l` -ne 0 ]
then
echo "keepalived is error!"
else
echo "keepalived is OK !"
fi
done
- 在从服务端的配置脚本事例
[root@slave scripts]# vim notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" 1470044516@qq.com
}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac