redis配置文件详解及实现主从同步切换
原理:redis复制是怎么进行工作
如果设置了一个slave,不管是在第一次链接还是重新链接master的时候,slave会发送一个同步命令 然后master开始后台保存,收集所有对修改数据的命令。当后台保存完成,master会将这个数据文件传送到slave,然后保存在磁盘,加载到内存中;master接着发送收集到的所有的修改数据的命令,这好比一个流命令,是redis协议本身来实现的。
当master和slave因一些故障宕机时,slaves会自动的重链,如果master收到多个slave的同步请求,master会执行一个后台保存,以确保所有的slaves都是正常的。 当master和slave能够维持链接,就会有一个完整的同步进行。
配置redis主从
配置主从同步是很简单的,仅仅在slave的配置文件中增加类似下面这行的内容:
slaveof 192.168.1.1 6379
你可以更换master的ip地址或地址,或者,你可以使用slaveof命令,master就会启动和slave的同步。
设置slave到master的认证
如果master需要通过密码登陆,那就需要配置slave在进行所有同步操作也要使用到密码。 在一个运行的实例上尝试,使用 redis-cli :
config set masterauth <password>
你也可以设置永久的。在配置文件中添加
masterauth <password>
redis主从实战
开启主服务器的后台运行,将配置文件中
daemonize no =》》 daemonize yes
。
开启从服务器的后台运行,及设置slaveof的IP和端口:
vim redis.conf
port 6379daemonize yes
slaveof 192.168.1.21 6379
masterauth xxxxxxxx
主从切换
停止redis主服务器:
[root@localhost redis]# redis-cli -p 6379 shutdown
[root@localhost redis]# redis-cli -p 6379
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
将redis从服务改为主:
#也可以直接在数据库命令行中输入slaveof no one
[root@localhost redis]# redis-cli -p 6379 slaveof NO ONEOK
假如原来的主redis恢复正常啦。要重新切换回去。
1). 将现在的主redis数据进行保存。
[root@localhost redis]# redis-cli
127.0.0.1:6379> save
OK
2).将现在的主redis的dump.rdb文件拷贝到原来的主dump.rdb目录下面
[root@localhost redis]# scp /data/redis/dump.rdb 192.168.1.21:/data/redis/dump.rdb
3).启动原来的主redis
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
4).将原来的从依然切换成从
[root@localhost redis]# redis-cli -p 6379 slaveof 192.168.1.21 6379
OK
这时你依然可以读取刚才从库写入的数据,如果不能读取则切换失败。
在Master和Slave上安装Keepalived 注意事项:主从的redis都要开启本地备份
$ yum install keepalived
默认安装完成keepalived有默认的配置文件,因此我们重写覆盖它:
首先,在Master上创建如下配置文件:
$ vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id redis100
}
vrrp_script chk_redis
{
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
timeout 2
fall 3
}
vrrp_instance redis {
state MASTER # master set to SLAVE also
interface eth0
virtual_router_id 50
priority 150
nopreempt # no seize,must add
advert_int 1
authentication { #all node must same
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.200/24
}
track_script {
chk_redis
}
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.0.101 6379"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.0.101 6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
然后,在Slave上创建如下配置文件:
! Configuration File for keepalived
global_defs {
router_id redis101
}
vrrp_script chk_redis
{
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
timeout 2
fall 3
}
vrrp_instance redis {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication { #all node must same
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.200/24
}
track_script {
chk_redis
}
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.0.100 6379"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.0.100 6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
在Master和Slave上创建监控Redis的脚本
$ mkdir /etc/keepalived/scripts
$ vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/redis/redis-cli -h $1 -p $2 PING`
LOGFILE="/var/log/keepalived-redis-check.log"
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [ $ALIVE == "PONG" ]; then :
echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1
exit 0
else
echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1
exit 1
fi
编写以下负责运作的关键脚本:
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
因为Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop
首先,在Redis Master上创建notity_master与notify_backup脚本:
$ vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/redis/redis-cli -h $1 -p $3"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run MASTER cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
sleep 10 #delay 10 s wait data async cancel sync
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
$ sudo vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/redis/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1
# echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #delay 15 s wait data sync exchange role
接着,在Redis Slave上创建notity_master与notify_backup脚本:
$ vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/redis/redis-cli -h $1 -p $3"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ... " >> $LOGFILE
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1
#echo "SLAVEOF $2 cmd can't excute ... " >> $LOGFILE
sleep 10 ##delay 15 s wait data sync exchange role
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
$ vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/redis/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[BACKUP]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
sleep 100 #delay 10 s wait data async cancel sync
exit(0)
然后在Master与Slave创建如下相同的脚本:
$ vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
$ sudo vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
给脚本都加上可执行权限:
(这点很重要,最开始由于这不没做,运行后一直报错 "VRRP_Instance(Redis) Now in FAULT state")
$ sudo chmod +x /etc/keepalived/scripts/*.sh
脚本创建完成以后,我们开始按照如下流程进行测试:
1.启动Master上的Redis
$ /etc/init.d/redis start
2.启动Slave上的Redis
$ /etc/init.d/redis start
3.启动Master上的Keepalived
$ /etc/init.d/keepalived start
4.启动Slave上的Keepalived
$ /etc/init.d/keepalived start
5.尝试通过VIP连接Redis:
$ redis-cli -h 10.6.1.200 INFO
连接成功,Slave也连接上来了。
role:master
slave0:10.6.1.144,6379,online
6.尝试插入一些数据:
$ redis-cli -h 10.6.1.200 SET Hello Redis
OK
从VIP读取数据
$ redis-cli -h 10.6.1.200 GET Hello
"Redis"
从Master读取数据
$ redis-cli -h 10.6.1.143 GET Hello
"Redis"
从Slave读取数据
$ redis-cli -h 10.6.1.144 GET Hello
"Redis"
下面,模拟故障产生:
将Master上的Redis停了
$ service redis_6379 stop
查看Master上的Keepalived日志
$ tailf /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2012
同时Slave上的日志显示:
$ tailf /var/log/keepalived-redis-state.log
[master]
Fri Sep 28 14:14:09 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
$ redis-cli -h 192.168.0.200 INFO
role:master
然后我们恢复Master的Redis进程
$ service redis_6379 start
查看Master上的Keepalived日志
$ tailf /var/log/keepalived-redis-state.log
[master]
Thu Sep 27 08:31:33 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
同时Slave上的日志显示:
$ tailf /var/log/keepalived-redis-state.log
[backup]
Fri Sep 28 14:16:37 CST 2012
Being slave....
Run SLAVEOF cmd ...
OK
可以发现目前的Master已经再次恢复了Master的角色,故障切换以及自动恢复都成功了。
redis配置文件详解及实现主从同步切换的更多相关文章
- redis配置文件详解-3
redis3.0以上配置文件 #################################INCLUDES ################################### include ...
- 转:Redis配置文件详解
配置与特性详解 1. 在redis中,合法的"尺寸单位",无大小写区分. # 1k => bytes # 1kb => bytes # 1m => bytes # ...
- redis配置文件详解
基于redis2.4版本的配置文件. # 注意单位问题:当需要设置内存大小的时候,可以使用类似1k.5GB.4M这样的常见格式:## 1k => 1000 bytes# 1kb => 10 ...
- Redis 配置文件详解
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写)## 1k => 1000 bytes# 1kb => ...
- [转]Redis配置文件详解
本文转自http://blog.csdn.net/neubuffer/article/details/17003909 redis是一款开源的.高性能的键-值存储(key-value store),和 ...
- Redis配置文件详解(redis.conf)
# vi redis.conf daemonize yes #是否以后台进程运行 pidfile /var/run/redis/redis-server.pid #pid文件位置 port ...
- Spring整合redis配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Redis—配置文件详解
https://www.cnblogs.com/shizhengwen/p/9283973.html https://www.cnblogs.com/yangy608/p/4443665.html h ...
- Redis for Windows(C#缓存)配置文件详解
Redis for Windows(C#缓存)配置文件详解 前言 在上一篇文章中主要介绍了Redis在Windows平台下的下载安装和简单使用http://www.cnblogs.com/aehy ...
随机推荐
- Android类似Periscope点赞效果
原文 https://github.com/AlanCheen/PeriscopeLayout 主题 安卓开发 PeriscopeLayout A layout with animation li ...
- 重磅︱文本挖掘深度学习之word2vec的R语言实现
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者寄语:2013年末,Google发布的 w ...
- 修改或隐藏Nginx的版本号
隐藏版本号 隐藏nginx的版本号很简单,nginx的HttpCoreModule提供了一条叫做server_tokens指令,我这要将这条指令设置为“server_tokens off”就可以了. ...
- Linux显示登入系统的帐号名称和总人数
Linux显示登入系统的帐号名称和总人数 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ who -q youhaidong youhaidong # 用户数= ...
- 配置文件properties读取使用的好方法
首先在spring配置文件applicationContext.xml中配置. <bean id="placeholderConfig" class="com.be ...
- 异常-----freemarker.core.ParseException: Encountered
1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column 12 in myself.ftl ...
- freemark标签从后台接过来数据Boolean在前台还是Boolean输出(四)
FREEMARK标签中输出BOOLEAN值 private boolean showHeader=true; public boolean getShowHeader(){ return this.s ...
- C# Coding Conventions(译)
C# Coding Conventions C#编码规范 Naming Conventions 命名规范Layout Conventions 布局规范Commenting Conventions 注释 ...
- ThreadLocal原理
ThreadLocal类可以看作是当前线程的一个局部变量,只有当前线程可以访问,因此是线程安全的. ThreadLocal内部维护了一个ThreadLocalMap类,ThreadLocalMap是一 ...
- ArXiv最受欢迎开源深度学习框架榜单:TensorFlow第一,PyTorch第四
[导读]Kears作者François Chollet刚刚在Twitter贴出最近三个月在arXiv提到的深度学习框架,TensorFlow不出意外排名第一,Keras排名第二.随后是Caffe.Py ...