目录
一. 环境准备
准备三个新环境的虚拟机,关闭防火墙和SElinux安全策略,并设置时间同步和配置好YUM源。
虚拟IP用:192.168.226.200
主机名 | 主机IP | 系统 | 用途 |
master | 192.168.226.139 | Rockly9.4 | 主节点 |
slave | 192.168.226.140 | Rockly9.4 | 备用节点 |
localhost | 192.168.226.141 | Rockly9.4 | 模拟客户端 |
二. 部署软件
两台主机,master和slave都操作
下载mysql-server和keepalived
yum install -y mysql-server keepalived
启动数据库
systemctl restart mysqld
使用mysql登录数据库,在rocky_linux9.4社区版没有初始化密码限制和低安全策略,直接可以登录登陆后修改一个密码使用。
Mysql版本:8.0.36
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36 Source distribution
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';
再创建一个远程root用户和密码并设置放开权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
ERROR 1410 (42000): You are not allowed to create a user with GRANT
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '1234';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
注意到这里,只用在master或者slave其中一台主机的数据库中创建一个库,这里我选择对192.168.226.139创建,这里是创建一个后面用客户端测试观察的库。
mysql> CREATE DATABASE dbtest;
三. master主机配置keepalived
vi /etc/keepalived/keepalived.conf
删除原文件keepalived.conf里的内容,新增如下内容,注意修改虚拟IP
! Configuration File for keepalived
global_defs {
router_id master
}
vrrp_script check_run {
script /etc/keepalived/keepalived_check_mysql.sh
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.226.200/24 #虚拟IP
}
track_script {
check_run
}
}
四. slave主机配置keepalived
vim /etc/keepalived/keepalived.conf
删除原文件keepalived.conf里的内容,新增如下内容,注意修改虚拟IP
! 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 66
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.226.200/24 #虚拟IP
}
track_script {
check_run
}
}
五. 两台机器配置数据库检查脚本
这里两台机器master和salve都要配置
vi /etc/keepalived/keepalived_check_mysql.sh
写入如下内容
#!/bin/bash
/usr/bin/mysql -uroot -p'1234' -e "show status" &>/dev/null
if [ $? -ne 0 ] ;then
# service keepalived stop
systemctl stop keepalived
fi
给脚本加上权限
chmod 777 /etc/keepalived/keepalived_check_mysql.sh
启动keepalived并设置开机自启
systemctl enable --now keepalived
此时,虚拟IP已经出现在master主机上。
六. 客户端模拟访问
在主机192.168.226.141机器上下载数据库用来远程登录模拟是客户端使用
yum install -y mysql-server
systemctl start mysqld
使用mysql 登录,初始化没密码的,登陆后修改一个和前面实验主机不同的数据库密码并退出。
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
EXIT
使用虚拟IP192.168.226.200远程登录数据库
mysql -uroot -p1234 -h 192.168.226.200 -P 3306
登录后
[root@localhost ~]# mysql -uroot -p1234 -h 192.168.226.200 -P 3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.36 Source distribution
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| dbtest |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
可以看到在这master机器创建的那个库,然后停止master主机的keepalived,观察虚拟IP是否漂移。
可以发现,虚拟IP漂移到了slave主机上,再次使用客户主机查看库命令发现没有了那个测试创建的库datest
mysql> show databases;
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
Connection id: 13
Current database: *** NONE ***
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.03 sec)
现在重新开启master上的keepalived,观察到虚拟IP又转移到了master主机上,并使用客户端再次查看数据库,这次就又可以看到这个用于测试创建的库了
mysql> show databases;
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
Connection id: 88
Current database: *** NONE ***
+--------------------+
| Database |
+--------------------+
| dbtest |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.03 sec)
标签:+--------------------+,IP,Keepalived,MySQL,keepalived,主机,master,切换,mysql
From: https://blog.csdn.net/Lzcsfg/article/details/139692266