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上

相关推荐
i道i1 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
Oak Zhang3 小时前
sharding-jdbc自定义分片算法,表对应关系存储在mysql中,缓存到redis或者本地
redis·mysql·缓存
久醉不在酒4 小时前
MySQL数据库运维及集群搭建
运维·数据库·mysql
WindFutrue4 小时前
使用Mybatis向Mysql中的插入Point类型的数据全方位解析
数据库·mysql·mybatis
一只爱撸猫的程序猿5 小时前
一个简单的Linux 服务器性能优化案例
linux·mysql·nginx
计算机毕设源码qq-38365310415 小时前
(附项目源码)Java开发语言,215 springboot 大学生爱心互助代购网站,计算机毕设程序开发+文案(LW+PPT)
java·开发语言·spring boot·mysql·课程设计
袁庭新6 小时前
Cannal实现MySQL主从同步环境搭建
java·数据库·mysql·计算机·java程序员·袁庭新
爱学习的白杨树6 小时前
MySQL中有哪几种锁?
数据库·mysql
Stara05119 小时前
Git推送+拉去+uwsgi+Nginx服务器部署项目
git·python·mysql·nginx·gitee·github·uwsgi
不爱学习的啊Biao9 小时前
初识mysql数据库
数据库·mysql·oracle