Redis主从配置及主从切换

摘要:
环境及部署图本次配置在同一台主机配置文件及端口号不一样6379配置文件/etc/redis/6379.confbind0.0.0.0protected-modeyesport6379tcp-backlog511timeout0tcp-keepalive300daemonizeyessupervisednopidfile/var/run/redis_6379.pidloglevelnoticelog

环境及部署图

Redis主从配置及主从切换第1张

Redis主从配置及主从切换第2张

本次配置在同一台主机配置文件及端口号不一样

6379配置文件/etc/redis/6379.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

6380配置文件/etc/redis/6380.conf

bind 0.0.0.0
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slaveof 10.1.43.212 6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

说明:配置文件大部分相同不同的是端口号以及备配置文件增加以下两行

slaveof 10.1.43.212 6379
masterauth 123456

PS:如果主redis没有配置密码则不需要配置masterauth

启动两个redis测试同步

redis-server /etc/redis/6379.conf 
redis-server /etc/redis/6380.conf 

连接6379插入数据

redis-cli -p 6379 -a 123456

127.0.0.1:6379> set name minseo
OK
127.0.0.1:6379> get name
"minseo"

连接6380查看同步的数据

redis-cli -p 6380 -a 123456

127.0.0.1:6380> get name
"minseo"

默认是读写分离的主库可写从库只能读取

127.0.0.1:6380> set name 123
(error) READONLY You can't write against a read only slave.

主从切换

停止主库

 src/redis-cli -n 6379 shutdown

将从库设置成主库

redis-cli -p 6380 -a123456 slaveof NO ONE

主库恢复正常了

从库保存数据

127.0.0.1:6380> save
OK

把根目录下面的数据文件dump.rdb拷贝至主库对应的目录下面启动原来的主库

从库恢复成从库

redis-cli -p 6380 slaveof 10.1.43.212 6379

免责声明:文章转载自《Redis主从配置及主从切换》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇pythonTCP UDP IPv4 IPv6 客户端和服务端的实现unsigned long long类型与long long类型下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

redis的管理和监控工具treeNMS

TreeNMS可以帮助您搭建起一套用于redis的监控管理系统,也支持Memcached,让您可以通过web的方式对数据库进行管理,有了它您就可以展示NOSQL数据库、编辑修改内容,另外还配备了sql语法帮助,让您的管理工作更加轻松。 一、安装 官网下载地址:http://www.treesoft.cn/dms.html(没有旧版本),若安装最新版本1.7...

Linux下常用redis指令

set get 取值赋值,key值区分大小写 mset mget多个的取值和赋值 del 删除一个或多个key 查看key值是否存在,有就返回1,没有就是0 设置一个有存活时间的key值,设置成功返回1 显示key值剩余存活时间 keys显示符合指定条件的key 创建相关数据类型,并使用keys显示对应的数据类型 创建hash类型的数据...

(一)Redis之简介和windows下安装radis

一、简介  1.1  关于nosql 介绍Redis之前,先了解下NoSQL (Not noly SQL)不仅仅是SQL, 属于非关系型数据库;Redis就属于非关系型数据库, 传统的Mysql ,oracle ,sql server 等 都是关系型数据库。   1.2  nosql的作用 为什么需要NoSQL,主要应对以下问题,传统关系型数据库力不从心...

Gateway Redis令牌桶请求限流过滤器

spring cloud gateway默认基于redis令牌桶算法进行微服务的限流保护,采用RateLimter限流算法来实现。 1.引入依赖包 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spr...

Spring 实现数据隔离

需求 用户数据要同时支持在mysql和redis进行管理、存储。 思路 利用spring的注解,在配置中指定存储类型,启动时识别并选择对应的实现类。 代码 1. 用户管理的接口类 public interfaceIUserManager { booleancreateUser(User user); booleanremoveUser(); ....

Redis简单入门安装(windows系统)

一、首先从点击跳转下载  下载好后安装,一定要确定好目录,我这里选择安装目录在D盘新建了一个redis文件夹  win + r 打开cmd ,或者右键左下角windows图标,点击运行,输入cmd 打开后会在C盘目录  先切换目录到d盘redis目录下面 输入 redis-server.exe --service-install redis.wi...