redismariadb + keepalived 高可用

news/2025/2/12 5:33:13/

目录

机器准备

安装后服务

redis

安装redis

mariadb

安装mariadb

启动和配置

互为主从同步配置

keepalived

keepalived安装

修改主从 keepalived的配置

主从配置-mariadb监控

主从配置-redis监控

查看和使用

Keepalived

Mariadb

redis


机器准备

两台机器:172.171.16.108-109   

操作系统 :centos7.6

两个虚拟ip :172.171.16.110-111

关闭防火墙

关闭防火墙//临时关闭systemctl stop firewalld//禁止开机启动systemctl disable firewalld

安装后服务

172.171.16.108

主数据库:Mariadb 10.5  3306  root  123456

主redis:Redis5.05  6379  123456

Keepalived

172.171.16.109

主数据库:Mariadb 10.5  3306  root  123456

从redis:Redis5.05  6379  123456  

Keepalived

Mariadb 虚拟ip  172.171.16.110

Redis虚拟ip  172.171.16.111

redis

安装redis

1.下载redis,并解压到/home/redis/目录下

mkdir /home/redis/

cd /home/redis/

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

tar -xzvf redis-5.0.5.tar.gz

2.安装编译环境

yum -y install make automake cmake gcc g++

cd /home/redis/redis-5.0.5/

3.编译安装redis

make && make install

4、配置开机自启

vi /etc/systemd/system/redis.service

[Unit]Description=redis-serverAfter=network.target[Service]Type=forkingExecStart= /home/redis/redis-5.0.5/src/redis-server /home/redis/redis-5.0.5/redis.confPrivateTmp=true[Install]WantedBy=multi-user.target

ExecStart=/opt/software/redis-6.2.6/src/redis-server :启动服务的命令的位置
/etc/redis.conf 配置文件命令的位置

systemctl daemon-reload

systemctl enable redis

5.修改两台服务器上的redis配置文件,绑定所有ip,后台方式运行进程,设置密码

vim redis.conf

主库  172.171.16.108

bind 0.0.0.0protected-mode nopidfile  /home/redis/redis_6379.piddaemonize yesdir /home/redislogfile "/home/redis/redis_6379.log"requirepass 123456

从库  172.171.16.109

bind 0.0.0.0protected-mode nopidfile  /home/redis/redis_6379.piddaemonize yesdir /home/redislogfile "/home/redis/redis_6379.log"requirepass 123456replicaof 172.171.16.108 6379masterauth 123456

命令

# 启动systemctl start redis# 停止systemctl stop redis# 重启systemctl restart redis# 查看状态systemctl status redis

mariadb

安装mariadb

查看已安装的包

命令:rpm -qa | grep mysql

如果存在已安装,逐个卸载掉这些包

命令:rpm -e --nodeps

配置yum源,并yum安装

vi /etc/yum.repos.d/MariaDB.repo

# MariaDB 10.5.9 CentOS repository list
# http://downloads.mariadb.org/mariadb/repositories/[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

yum update

yum install MariaDB-server MariaDB-client

配置两台服务器上的数据库配置文件

vim /etc/my.cnf.d/server.cnf

主库  172.171.16.108

## These groups are read by MariaDB server.# Use it for options that only the server (but not clients) should see## See the examples of server my.cnf files in /usr/share/mysql/## this is read by the standalone daemon and embedded servers[server]# this is only for the mysqld standalone daemon[mysqld]skip-name-resolve#event_scheduler=ONdatadir=/home/mariadbsocket=/home/mariadb/mysql.sockslow_query_log=onslow_query_log_file=/home/mariadb/slow_query_log.loglong_query_time=10max_allowed_packet=512Mmax_connections=1000character-set-server=utf8mb4collation-server=utf8mb4_general_cilower_case_table_names=0#skip_name_resolve=ONinnodb_file_per_table=ONserver-id=108log-bin=mysql-binlog-bin-index=master-bin.index# ignored databasesbinlog-ignore-db=mysqlbinlog-ignore-db=information_schemabinlog-ignore-db=performance_schema# ignored tables# #replicate-ignore-table=testauto-increment-increment=2auto-increment-offset=1slave-skip-errors=allrelay_log=slave_relay_binwait_timeout=31536000interactive_timeout=31536000thread_pool_size=128thread_pool_stall_limit=10# Specifies the number of days before automatic removal of binary log filesexpire_logs_days=30## * Galera-related settings#[galera]# Mandatory settings#wsrep_on=ON#wsrep_provider=#wsrep_cluster_address=#binlog_format=row#default_storage_engine=InnoDB#innodb_autoinc_lock_mode=2## Allow server to accept connections on all interfaces.##bind-address=0.0.0.0## Optional setting#wsrep_slave_threads=1#innodb_flush_log_at_trx_commit=0innodb_flush_log_at_trx_commit=0innodb_autoextend_increment=128Minnodb_log_buffer_size=16Minnodb_log_file_size=128M# this is only for embedded server[embedded]# This group is only read by MariaDB servers, not by MySQL.# If you use the same .cnf file for MySQL and MariaDB,# you can put MariaDB-only options here[mariadb]# This group is only read by MariaDB-10.5 servers.# If you use the same .cnf file for MariaDB of different versions,# use this group for options that older servers don't understand[mariadb-10.5]

主库  172.171.16.109

## These groups are read by MariaDB server.# Use it for options that only the server (but not clients) should see## See the examples of server my.cnf files in /usr/share/mysql/## this is read by the standalone daemon and embedded servers[server]# this is only for the mysqld standalone daemon[mysqld]skip-name-resolve#event_scheduler=ONdatadir=/home/mariadbsocket=/home/mariadb/mysql.sockslow_query_log=onslow_query_log_file=/home/mariadb/slow_query_log.loglong_query_time=10max_allowed_packet=512Mmax_connections=1000character-set-server=utf8mb4collation-server=utf8mb4_general_cilower_case_table_names=0#skip_name_resolve=ONinnodb_file_per_table=ONserver-id=109log-bin=mysql-binlog-bin-index=master-bin.index# ignored databasesbinlog-ignore-db=mysqlbinlog-ignore-db=information_schemabinlog-ignore-db=performance_schema# ignored tables# #replicate-ignore-table=testauto-increment-increment=2auto-increment-offset=2slave-skip-errors=allrelay_log=slave_relay_binwait_timeout=31536000interactive_timeout=31536000thread_pool_size=128thread_pool_stall_limit=10# Specifies the number of days before automatic removal of binary log filesexpire_logs_days=30## * Galera-related settings#[galera]# Mandatory settings#wsrep_on=ON#wsrep_provider=#wsrep_cluster_address=#binlog_format=row#default_storage_engine=InnoDB#innodb_autoinc_lock_mode=2## Allow server to accept connections on all interfaces.##bind-address=0.0.0.0## Optional setting#wsrep_slave_threads=1#innodb_flush_log_at_trx_commit=0innodb_flush_log_at_trx_commit=0innodb_autoextend_increment=128Minnodb_log_buffer_size=16Minnodb_log_file_size=128M# this is only for embedded server[embedded]# This group is only read by MariaDB servers, not by MySQL.# If you use the same .cnf file for MySQL and MariaDB,# you can put MariaDB-only options here[mariadb]# This group is only read by MariaDB-10.5 servers.# If you use the same .cnf file for MariaDB of different versions,# use this group for options that older servers don't understand[mariadb-10.5]

创建数据库目录,并将原有的数据库目录下文件复制到新的目录下,给sock文件设置软连接

mkdir /home/mariadb

chown -R mysql:mysql /home/mariadb

cp -a /var/lib/mysql/*     /home/mariadb

ln -s /home/mariadb/mysql.sock /var/lib/mysql/mysql.sock

启动和配置

命令

systemctl start mariadb  #启动MariaDB
systemctl stop mariadb  #停止MariaDB
systemctl restart mariadb  #重启MariaDB
systemctl enable mariadb  #设置开机启动

对两台服务器上的数据库做初始化配置

mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user. If you've just installed MariaDB, andhaven't set the root password yet, you should just press enter here.Enter current password for root (enter for none):OK, successfully used password, moving on...Setting the root password or using the unix_socket ensures that nobodycan log into the MariaDB root user without the proper authorisation.You already have your root account protected, so you can safely answer 'n'.Switch to unix_socket authentication [Y/n] n... skipping.You already have your root account protected, so you can safely answer 'n'.Change the root password? [Y/n] yNew password:Re-enter new password:Password updated successfully!Reloading privilege tables..... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y... Success!Normally, root should only be allowed to connect from 'localhost'.  Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n... skipping.By default, MariaDB comes with a database named 'test' that anyone canaccess.  This is also intended only for testing, and should be removedbefore 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 farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB!

设置远程登录 ,并更新生效

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;

互为主从同步配置

保持 108 和 109 上的数据库、表完全一致

通过执行如下sql 设置两台数据库互相同步

108认为是master1  需要同步master2

change master 'master2' to master_host='172.171.16.109',master_port=3306,master_user='root',master_password='123456';start SLAVE 'master2';show all slaves status;

109认为是master2  需要同步master1

change master 'master1' to master_host='172.171.16.108',master_port=3306,master_user='root',master_password='123456';start SLAVE 'master1';show all slaves status;

keepalived

keepalived安装

卸载

查找安装目录

[root@CentOS-003 keepalived]# find / -name keepalived

删除安装目录

rm -rf /etc/sysconfig/keepalivedrm -rf /etc/keepalivedrm -rf /var/lib/selinux/targeted/active/modules/100/keepalivedrm -rf /usr/sbin/keepalivedrm -rf /usr/share/doc/keepalivedrm -rf /usr/share/selinux/targeted/default/active/modules/100/keepalivedrm -rf /usr/libexec/keepalivedrm -rf /etc/selinux/targeted/active/modules/100/keepalived

查看进程

[root@CentOS-001 /]#  ps aux|grep keepalived

创建目录

mkdir /home/keepalived

cd  /home/keepalived

下载压缩包 keepalived-2.0.20.tar.gz 并放在该目录下

解压

tar xvf keepalived-2.0.20.tar.gz

安装

[root@CentOS-003 opt]# cd keepalived-2.0.20/

[root@CentOS-003 keepalived-2.0.20]#  ./configure --prefix=/usr/local/keepalived

如果报错

configure: error:

  !!! OpenSSL is not properly installed on your system. !!!

  !!! Can not include OpenSSL headers files.            !!!

Yum安装解决

yum -y install openssl openssl-devel

解决后继续执行命令

[root@CentOS-003 keepalived-2.0.20]#  ./configure --prefix=/usr/local/keepalived

[root@CentOS-003 keepalived-2.0.20]# make & make install

# keepalived 启动脚本变量引用文件,默认文件路径是 /etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)[root@CentOS-001 keepalived-2.0.20]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived# 将keepalived 主程序加入到环境变量(安装目录下)[root@CentOS-001 keepalived-2.0.20]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived# keepalived 启动脚本(源码目录下),放到 /etc/init.d/ 目录下就可以使用 service 命令便捷调用[root@CentOS-001 keepalived-2.0.20]# cp /home/keepalived/keepalived-2.0.20/keepalived/etc/init.d/keepalived  /etc/init.d/keepalived# 将配置文件放到默认路径下[root@CentOS-001 keepalived-2.0.20]# mkdir /etc/keepalived[root@CentOS-001 keepalived-2.0.20]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

chmod 644 /etc/keepalived/keepalived.conf

启动

[root@CentOS-001 keepalived-2.0.20]# service keepalived start

日志位置

[root@CentOS-001 log]# tail -f /var/log/messages

加为系统服务:chkconfig --add keepalived开机启动:chkconfig keepalived on查看开机启动的服务:chkconfig --list启动、关闭、重启 service keepalived start|stop|restart

ip a show |grep -w inet

修改主从 keepalived的配置

vim /etc/keepalived/keepalived.conf

主库  172.171.16.108


global_defs {router_id redis-mysql-masterscript_user rootenable_script_security
}vrrp_script chk_mysql
{#监控脚本script "/home/keepalived-scripts/mysql/mysql_check.sh"#监控时间,脚本的执行时间间隔interval 2#超时时间timeout 2weight -15#3次失败才算失败,会用weight减少优先级(1-255之间)fall 3#1次成功就算成功,但不修改优先级rise 1
}vrrp_script chk_redis
{#监控脚本script "/home/keepalived-scripts/redis/redis_check.sh"#监控时间,脚本的执行时间间隔interval 2#超时时间timeout 2weight -15#3次失败才算失败,会用weight减少优先级(1-255之间)fall 3#1次成功就算成功,但不修改优先级rise 1
}vrrp_instance VI_1 {#设置为MASTERstate MASTER#本机监控网卡interface ens192# garp_master_delay 10#主从需要在同一路由编号virtual_router_id 55#权重值priority 100#默认抢占#nopreempt#vrrp通告的时间间隔,默认1sadvert_int 1authentication {#加密auth_type PASS#密码auth_pass 123456}virtual_ipaddress {#Keppalived虚拟出来的VIP172.171.16.110}track_script {chk_mysql}
}vrrp_instance VI_2 {#设置为MASTERstate MASTER#本机监控网卡interface ens192#主从需要在同一路由编号virtual_router_id 60#权重值priority 100#vrrp通告的时间间隔,默认1sadvert_int 5authentication {#加密auth_type PASS#密码auth_pass 123456}virtual_ipaddress {#Keppalived虚拟出来的VIP172.171.16.111}track_script {chk_redis}#keepalived成为masternotify_master /home/keepalived-scripts/redis/redis_master.sh#keepalived成为backupnotify_backup /home/keepalived-scripts/redis/redis_slave.sh#keepalived fault时notify_fault /home/keepalived-scripts/redis/redis_fault.sh#keepalived服务中止时notify_stop /home/keepalived-scripts/redis/redis_stop.sh}

主库  172.171.16.109


global_defs {router_id redis-mysql-slavescript_user rootenable_script_security
}vrrp_script chk_mysql
{#监控脚本script "/home/keepalived-scripts/mysql/mysql_check.sh"#监控时间,脚本的执行时间间隔interval 2#超时时间timeout 2weight -15#3次失败才算失败,会用weight减少优先级(1-255之间)fall 3#1次成功就算成功,但不修改优先级rise 1
}vrrp_script chk_redis
{       #监控脚本script "/home/keepalived-scripts/redis/redis_check.sh"#监控时间,脚本的执行时间间隔interval 2#超时时间timeout 2weight -15#3次失败才算失败,会用weight减少优先级(1-255之间)fall 3#1次成功就算成功,但不修改优先级rise 1
}vrrp_instance VI_1 {#设置为BACKUPstate BACKUP#本机监控网卡interface em1
#   garp_master_delay 10#主从需要在同一路由编号virtual_router_id 55#权重值priority 90#默认抢占#nopreempt#vrrp通告的时间间隔,默认1sadvert_int 1authentication {#加密auth_type PASS#密码auth_pass 123456}virtual_ipaddress {#Keppalived虚拟出来的VIP172.171.16.110}track_script {chk_mysql}
}vrrp_instance VI_2 {#设置为BACKUPstate BACKUP#本机监控网卡interface em1#主从需要在同一路由编号virtual_router_id 60#权重值priority 90#vrrp通告的时间间隔,默认1sadvert_int 5authentication {#加密auth_type PASS#密码auth_pass 123456}virtual_ipaddress {#Keppalived虚拟出来的VIP172.171.16.111}track_script {chk_redis}#keepalived成为masternotify_master /home/keepalived-scripts/redis/redis_master.sh#keepalived成为backupnotify_backup /home/keepalived-scripts/redis/redis_slave.sh#keepalived fault时notify_fault /home/keepalived-scripts/redis/redis_fault.sh#keepalived服务中止时notify_stop /home/keepalived-scripts/redis/redis_stop.sh}

主从配置-mariadb监控

创建监控脚本目录,用来放监控脚本文件

mkdir /home/keepalived-scripts

mkdir /home/keepalived-scripts/mysql

cd  /home/keepalived-scripts/mysql

vim mysql_check.sh(当前目录的 /script目录下)

编辑mysql_check.sh脚本,108、109服务器一致

#!/bin/bash
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
#mysql is working MYSQL_OK is 1, mysql down MYSQL_OK is 0
MYSQL_OK=1
#select 1;
function check_mysql_helth() {mysqladmin -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} ping &>/dev/nullif [ $? = 0 ] ;thenMYSQL_OK=1elseMYSQL_OK=0fiecho $MYSQL_OKreturn $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
dolet "CHECK_TIME -= 1"check_mysql_helthif [ $MYSQL_OK = 1 ]; thenCHECK_TIME=0exit 0
fi
if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]
thenexit 1
fi
sleep 2
done

主从配置-redis监控

mkdir /home/keepalived-scripts

mkdir /home/keepalived-scripts/redis

mkdir /home/keepalived-scripts/log

cd  /home/keepalived-scripts/ redis

vim 编辑如下脚本

/home/keepalived-scripts/redis/redis_master.sh
/home/keepalived-scripts/redis/redis_slave.sh
/home/keepalived-scripts/redis/redis_fault.sh
/home/keepalived-scripts/redis/redis_stop.sh

/home/keepalived-scripts/redis/redis_stop.sh

具体查看下面链接中资源

       keepalived 监测mysql&&redis 高可用配置和脚本icon-default.png?t=N5K3https://download.csdn.net/download/mengo1234/87954486

chmod +x /home/keepalived-scripts/redis/*

redis脚本日志所在位置:/home/keepalived-scripts/log

chmod +x /home/keepalived-scripts/mysql/*

查看和使用

Keepalived

启动、关闭、重启 service keepalived start|stop|restart

查看keepalived集群主节点在哪台服务器,执行命令:ip a

查看日志:tail -f /var/log/messages

Mariadb

systemctl start mariadb  #启动MariaDB
systemctl stop mariadb  #停止MariaDB
systemctl restart mariadb  #重启MariaDB
systemctl enable mariadb  #设置开机启动

redis

启动

systemctl start redis

# 停止

systemctl stop redis

# 重启

systemctl restart redis

# 查看状态

systemctl status redis

登录:redis-cli 

查看信息:Info

查看主从信息:info Replicatio

切换主从:replicaof NO ONE

replicaof 172.171.16.109 6379    108上redis从109

replicaof 172.171.16.108 6379    109上redis从108


http://www.ppmy.cn/news/659216.html

相关文章

店宝宝:9.9致富经

一把小小的电动牙刷还掀起了一个百亿级别的市场浪潮。 淘宝第三方监测数据显示,2017年淘宝电动牙刷销售额约18亿元;2018年翻了一倍多,达到38亿元;2019年全年销售额同比继续增长50%,约为57亿元,行业实现爆发…

亚马逊Prime会员日:销量超去年“黑五”及“网络星期一”总和

近日,亚马逊宣布2019年亚马逊Prime会员日销售在全球范围内再创新高,7月15、16日两天的销量超过去年 “黑色星期五”及“网络星期一”的总和,再度成为亚马逊全球史上最大的购物狂欢。2019亚马逊中国Prime会员日海外购销售额同比去年也实现了3倍…

机器学习优化器和SGD和SGDM实验对比(编程实现SGD和SGDM)

机器学习优化器和SGD和SGDM实验对比 博主最近在学习优化器,于是呢,就做了一个SGD和SGDM的实验对比,可谓是不做不知道,一做吓一跳,这两个算法最终对结果的影响还是挺大的,在实验中SGDM明星要比SGD效果好太多…

2022-2027年中国智能炒菜机行业发展监测及投资战略研究报告

【报告格式】电子版、纸介版 【出品单位】华经产业研究院 本报告由华经产业研究院出品,对中国智能炒菜机行业的发展现状、竞争格局及市场供需形势进行了具体分析,并从行业的政策环境、经济环境、社会环境及技术环境等方面分析行业面临的机遇及挑战。还…

高性能电工电子电拖及自动化技术实训与考核装置

ZN-800D高性能电工电子电拖及自动化技术实训与考核装置 一、概述: ZN-800D高性能电工电子电拖及自动化技术实训与考核装置是吸取国内外同类产品合理的实训方法,先进、科学的实训手段并加以消化、整合、提高而研制的针对高等院校、职业技术教育而开发的综合性实训考核装置。其…

1253. 重构 2 行二进制矩阵(力扣)

1253. 重构 2 行二进制矩阵(力扣) 题目第一种方式分析测试代码运行结果 第二种方式测试代码运行结果 题目 给你一个 2 行 n 列的二进制数组: 矩阵是一个二进制矩阵,这意味着矩阵中的每个元素不是 0 就是 1。 第 0 行的元素之和为…

浅谈基于分项计量的校园能源监管平台解决方案设计

张心志 关注acrelzxz 安科瑞电气股份有限公司 上海嘉定 201801 摘要:伴随着我国经济的飞速发展,国家机关办公建筑和大型公共建筑高耗能的问题日益突出,如何解决建筑能耗己成为一个国家总能耗的重要组成部分。学校是肩负着教育、科研和社会服…

windows store 下载软件出现错误代码: 0x80D03805

windows store 下载软件出现错误代码: 0x80D03805 8265 8260的网卡驱动安装后在Microsoft store会出现错误。 把网卡驱动卸载后 在重启 就可以更新了 不过更新好后 在把驱动装上。。。。 以下是原话 The latest ProSet/Driver package of v20.80.1 with driver v20.70.2.1 fo…