redis设置主从复制-slave Replication--解决报错:(error) READONLY You can't write against a read only slave.

本文介绍了如何配置Redis主从复制,并解决在从节点写操作时遇到的'(error) READONLY You can't write against a read only slave.'错误。主要步骤包括在从节点设置密码、允许外网访问,以及将slave-read-only选项设为no,以允许写操作。通过redis-cli检查角色确认配置成功。

我的个人博客:zhang0peter的个人博客


主节点按照我上篇文章的内容配好redis后就可以了:ubuntu, debian 安装redis,设置开机自动启动和密码,允许外网访问
在从节点安装好redis,然后配置redis.conf:

cp /etc/redis/redis.conf /etc/redis/redis.conf2
nano /etc/redis/redis.conf2

设置密码,允许外网访问。
最关键的是slave配置:

#定义master信息
slaveof 192.168.1.103 6379   
#密码认证
masterauth redisredis  

如果想要从节点不仅可以读,也可以写的话,需要设置从节点的slave-read-only为no,

否则你会看到报错**(error) READONLY You can’t write against a read only slave.
**

# You can configure a slave instance to accept writes or not. Writing against
# a slave instance may be useful to store some ephemeral data (because data
# written on a slave will be easily deleted after resync with the master) but
# may also cause problems if clients are writing to it because of a
# misconfiguration.
#
# Since Redis 2.6 by default slaves are read-only.
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only no

然后重启redis就可以了:

service redis-server stop 
redis-server /etc/redis/redis.conf2  

启动redis-cli查看是否成功:

-> # redis-cli -p 6379-a 13575800231    
127.0.0.1:6379> client list
id=3 addr=192.168.1.103:6379fd=8 name= age=6876 idle=1 flags=M db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=sadd
id=5 addr=127.0.0.1:44860 fd=9 name= age=13 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.1.103
master_port:34343
master_link_status:up

看到role是slave说明是从节点,role是master说明是主节点。


我的数据库系列文章:

2025-03-17 05:17:30.641 ERROR 15424 --- [nio-3004-exec-1] c.bishe.product.GlobalExceptionHandler : 全局异常捕获: URL=/product/list, Error=Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: READONLY You can't write against a read only replica. org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: READONLY You can't write against a read only replica. at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:54) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:52) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:273) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.convertLettuceAccessException(LettuceStringCommands.java:799) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.set(LettuceStringCommands.java:180) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.DefaultedRedisConnection.set(DefaultedRedisConn
03-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值