Redis ( 5 ) ~ レプリケーション ~

October 01, 2017

Redis のレプリケーション設定

2 台の Redis 用ホストでレプリケーションを実行する方法について。

スレーブ側の設定

設定ファイルに以下を追加する。

slaveof 10.1.11.122 6379

マスター側の設定

設定ファイルで bind が設定されていたら、それをコメントアウトするか、スレーブからの接続は許可するように変更する。
bind が設定されていない場合は protected-mode を no にしなければ接続を受け付けないので注意。

実際にスレーブ起動

2011:S 01 Oct 10:45:56.016 * The server is now ready to accept connections on port 6379
2011:S 01 Oct 10:45:56.016 * Connecting to MASTER 10.1.11.122:6379
2011:S 01 Oct 10:45:56.017 * MASTER <-> SLAVE sync started
2011:S 01 Oct 10:45:56.019 * Non blocking connect for SYNC fired the event.
2011:S 01 Oct 10:45:56.022 * Master replied to PING, replication can continue...
2011:S 01 Oct 10:45:56.027 * Partial resynchronization not possible (no cached master)
2011:S 01 Oct 10:45:56.031 * Full resync from master: 16591a809498364224cba0a93b58f8a2d3ea8637:1
2011:S 01 Oct 10:45:56.963 * MASTER <-> SLAVE sync: receiving 37319090 bytes from master
2011:S 01 Oct 10:45:57.299 * MASTER <-> SLAVE sync: Flushing old data
2011:S 01 Oct 10:46:03.150 * MASTER <-> SLAVE sync: Loading DB in memory
2011:S 01 Oct 10:46:04.333 * MASTER <-> SLAVE sync: Finished with success

(マスター側)

127.0.0.1:6379> SET master-slave-key "hello, slave"
OK

(スレーブ側)

127.0.0.1:6379> GET master-slave-key
"hello, slave"

-> ちゃんとレプリケーションされている。

ちなみに、スレーブ側で書き込みを実行しようとすると…

127.0.0.1:6379> SET slave-key "I am a slave"
(error) READONLY You can't write against a read only slave.

-> ちゃんと拒否される。


 © 2023, Dealing with Ambiguity