如何在docker-compose.yml中使Redis从站可写?
我有一个正在运行的Python脚本,如果由于以下错误而失败,该脚本将无法转移到从服务器:
File "app.py", line 22, in <module>
r.set(timestamp, num)
File "/usr/local/lib/python2.7/site-packages/redis/client.py", line 1519, in set
return self.execute_command('SET', *pieces)
File "/usr/local/lib/python2.7/site-packages/redis/client.py", line 839, in execute_command
return self.parse_response(conn, command_name, **options)
File "/usr/local/lib/python2.7/site-packages/redis/client.py", line 853, in parse_response
response = connection.read_response()
File "/usr/local/lib/python2.7/site-packages/redis/connection.py", line 717, in read_response
raise response
redis.exceptions.ReadOnlyError: You can't write against a read only replica.
以下是正在使用的docker-compose.yml:
services:
redis-master:
container_name: redis-master
image: redis:latest
command: redis-server --port 6379
ports:
- "6379:6379"
volumes:
- .:/app
redis-slave:
container_name: redis-slave
image: redis:latest
command: redis-server --slaveof redis-master 6379
volumes:
- .:/app
从站输出:
root@29b9b3919c4a:/data# redis-cli -p 6379 info replication
# Replication
role:slave
master_host:192.168.48.7
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:257149
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:2705ce53eb2c7778f207f7626280ca0964dc87b1
master_replid2:8acb89aa40f8d7edc254eaed3ac197d08b808e82
master_repl_offset:257149
second_repl_offset:60757
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:60757
repl_backlog_histlen:196393
是否可以添加到docker-compose.yml以使从站可写?
非常感谢你!
最佳答案
查看redis.conf文件,有以下选项:replica-read-only yes
。
您需要更改该值以允许在奴隶上写东西。
You can configure a replica instance to accept writes or not. Writing against a replica instance may be useful to store some ephemeral data (because data written on a replica 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 replicas are read-only. Note: read only replicas 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 replica 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 replicas using 'rename-command' to shadow all the administrative / dangerous commands.
关于redis - docker-compose中的Redis主从设置-只读从,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57716303/