我的个人博客: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说明是主节点。
我的数据库系列文章:
本文介绍了如何配置Redis主从复制,并解决在从节点写操作时遇到的'(error) READONLY You can't write against a read only slave.'错误。主要步骤包括在从节点设置密码、允许外网访问,以及将slave-read-only选项设为no,以允许写操作。通过redis-cli检查角色确认配置成功。
5773

被折叠的 条评论
为什么被折叠?



