MYSQL双主双从,使用Keepalived双机热备+LVS高可用群集

MYSQL双主双从,使用Keepalived双机热备+LVS高可用群集

​ 文档只记录Keepalived+LVS+mysql主从,不包含检验,如需检验,请自行添加web服务器

一、IP规划

服务器 IP 备注
master1 192.168.100.131 master2的从
master2 192.168.100.132 master1的从
slave1 192.168.100.134 master1的从
slave2 192.168.100.135 master2的从
KI1 192.168.100.137
KI2 192.168.100.138
虚拟ip 192.168.100.200 热备ip

二、具体配置

1.master1

​ 配置ip:192.168.100.131

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=1

# 日志文件名称前缀
log-bin=mysql-bin

# 排除不复制的库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

# 开启联等备份
log_slave_updates=1

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 进入mysql
mysql -uroot -p123456

# 创建用户
create user "backup"@"%" identified by '123456';

# 授权
grant replication slave on *.* to "backup"@"%" identified by "123456";
grant all privileges on *.* to 'root'@'%' identified by '123456';

# 刷新权限
flush privileges;

# 查询Log_file,log_pos
show master status;
+------------------+----------+--------------+-------------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                |
+------------------+----------+--------------+-------------------------------------------------+
| mysql-bin.000003 |     1947 |              | mysql,information_schema,performance_schema,sys |
+------------------+----------+--------------+-------------------------------------------------+
1 row in set (0.00 sec)

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.132', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000003', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

2.master2

​ 配置ip:192.168.100.132

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=2

# 日志文件名称前缀
log-bin=mysql-bin

# 排除不复制的库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

# 开启联等备份
log_slave_updates=1

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 进入mysql
mysql -uroot -p123456

# 创建用户
create user "backup"@"%" identified by '123456';

# 授权
grant replication slave on *.* to "backup"@"%" identified by "123456";
grant all privileges on *.* to 'root'@'%' identified by '123456';

# 刷新权限
flush privileges;

# 查询Log_file,log_pos
show master status;
+------------------+----------+--------------+-------------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                |
+------------------+----------+--------------+-------------------------------------------------+
| mysql-bin.000003 |     1947 |              | mysql,information_schema,performance_schema,sys |
+------------------+----------+--------------+-------------------------------------------------+
1 row in set (0.00 sec)

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.134', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000004', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

3.slave1

​ 配置ip:192.168.100.134

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=3

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格式
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 连接数据库
mysql -uroot -p123456

# 授权
grant all privileges on *.* to "root"@"%" identified by "123456"; 

# 刷新权限
flush privileges;

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.131', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000003', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

4.slave2

​ 配置ip:192.168.100.135

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=4

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 连接数据库
mysql -uroot -p123456

# 授权
grant all privileges on *.* to "root"@"%" identified by "123456"; 

# 刷新权限
flush privileges;

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.132', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000003', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

5.KI1

​ 配置ip:192.168.100.137

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载keepalived和ipvsadm

sh 复制代码
yum -y install keepalived ipvsadm

​ 备份默认配置文件

sh 复制代码
cp -p /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

​ 删除原有文件,创建新内容

sh 复制代码
# 删除原有文件
rm -rf /etc/keepalived/keepalived.conf 

# 创建新文件
vi /etc/keepalived/keepalived.conf 

​ 添加以下内容

sh 复制代码
! Configuration File for keepalived

# 配置双机热备(主)
lobal_defs {
   router_id 1
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 1
    priority 120
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.100.200
    }
}

# 虚拟服务器ip及mysql地址
virtual_server 192.168.100.200 3306 {
        delay_loop 15
        lb_algo rr
        lb_kind DR
        protocol TCP
        real_server 192.168.100.131 3306 {
        weight 1
        TCP_CHECK {
                connect_port 3306
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
                }
        }
        real_server 192.168.100.132 3306 {
        weight 1
        TCP_CHECK {
                connect_port 3306
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
                }
        }
}

​ 启动keepalived

sh 复制代码
systemctl start keepalived

​ 服务模块

sh 复制代码
# 加载系统内核的服务模块
modprobe ip_vs

# 查看运行状态
lsmod | grep ip_vs

​ 查看规则

sh 复制代码
ipvsadm -l

​ 下载net网络工具

sh 复制代码
yum -y install net-tools

​ 查看网卡信息

sh 复制代码
# 停用物理网卡
ifconfig ens33 down

# 启动物理网卡(如在连接工具上,需到虚拟机上启动)
ifconfig ens33 up

# 查看网卡信息(在ens33网卡下是否有ip:192.168.100.200)
ip a

​ 虚拟网卡

sh 复制代码
# 添加虚拟网卡(回环地址)
cat >> /etc/sysconfig/network-scripts/ifcfg-lo:0  << EOF
DEVICE=lo:0
IPADDR=192.168.100.200
NETMASK=255.255.255.255
ONBOOT=yes
EOF

# 重启网络
systemctl restart network

# 查看是否有虚拟ip
ip a

​ 写入路由指向

sh 复制代码
# 添加路由信息
route add -host 192.168.100.200 dev lo:0

6.KI2

​ 配置ip:192.168.100.138

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载keepalived和ipvsadm

sh 复制代码
yum -y install keepalived ipvsadm

​ 备份默认配置文件

sh 复制代码
cp -p /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

​ 添加以下内容

sh 复制代码
! Configuration File for keepalived

# 配置双机热备(备)
lobal_defs {
   router_id 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 1
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.100.200
    }
}

# 虚拟服务器ip及mysql地址
virtual_server 192.168.100.200 3306 {
        delay_loop 15
        lb_algo rr
        lb_kind DR
        protocol TCP
        real_server 192.168.100.131 3306 {
        weight 1
        TCP_CHECK {
                connect_port 3306
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
                }
        }
        real_server 192.168.100.132 3306 {
        weight 1
        TCP_CHECK {
                connect_port 3306
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
                }
        }
}

​ 启动keepalived

sh 复制代码
systemctl start keepalived

​ 服务模块

sh 复制代码
# 加载系统内核的服务模块
modprobe ip_vs

# 查看运行状态
lsmod | grep ip_vs

​ 查看规则

sh 复制代码
ipvsadm -l

​ 下载net网络工具

sh 复制代码
yum -y install net-tools

​ 查看网卡信息

sh 复制代码
# 停用物理网卡
ifconfig ens33 down

# 启动物理网卡(如在连接工具上,需到虚拟机上启动)
ifconfig ens33 up

# 查看网卡信息(在ens33网卡下是否有ip:192.168.100.200)
ip a

​ 虚拟网卡

sh 复制代码
# 添加虚拟网卡(回环地址)
cat >> /etc/sysconfig/network-scripts/ifcfg-lo:0  << EOF
DEVICE=lo:0
IPADDR=192.168.100.200
NETMASK=255.255.255.255
ONBOOT=yes
EOF

# 重启网络
systemctl restart network

# 查看是否有虚拟ip
ip a

​ 写入路由指向

sh 复制代码
# 添加路由信息
route add -host 192.168.100.200 dev lo:0

三、检验

​ 可自行添加一台web 服务器,连接虚拟ip ,开启KI1或KI2的防火墙进行检测

或者 ,开启KI1的防火墙 ,查看ip是否跳转到KI2上

相关推荐
weixin_4493108423 分钟前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
CodingBrother24 分钟前
MySQL 和 PostgreSQL 的使用案例
mysql·adb·postgresql
standxy1 小时前
如何将钉钉新收款单数据高效集成到MySQL
数据库·mysql·钉钉
Narutolxy2 小时前
MySQL 权限困境:从权限丢失到权限重生的完整解决方案20241108
数据库·mysql
Venchill2 小时前
安装和卸载Mysql(压缩版)
数据库·mysql
Humbunklung3 小时前
一种EF(EntityFramework) MySQL修改表名去掉dbo前缀的方法
数据库·mysql·c#
C吴新科8 小时前
MySQL入门操作详解
mysql
Ai 编码助手10 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
白云如幻10 小时前
MySQL排序查询
数据库·mysql
苹果醋310 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx