基於redis集群設定密碼的例項

2022-09-26 12:54:26 字數 3332 閱讀 6540

注意事項:

1.如果是使用redis-trib.rb工具構建集群,集群構建完成前不要配置密碼,集群構建完畢再通過config set + config rewrite命令逐個機器設定密碼

2.如果對集群設定密碼,那麼requirepass和masterauth都需要設定,否則發生主從切換時,就會遇到授權問題,可以模擬並觀察日誌

3.各個節點的密碼都必須一致,否則redirected就會失敗

config set masterauth abc

config set requirepass abc

config rewrite

測試版本:

redis3.0.7

測試環境:

centos7

集群規模:

3主3從

集群構建成功前的redis配置:

port 8000

cluster-enabled yes

cluster-config-file "nodes-8000.conf"

cluster-node-timeout 15000

dir "/opt/redisdata"

appendonly yes

appendfilename "appendonly-8000.aof"

logfile "/opt/redisd程式設計客棧ata/8000.log"

daemonize yes

pidfile "/var/run/redis-8000.pid"

dbfilename "dump-8000.rdb"

cluster-require-full-coverage no

集群構建成功後的redis配置:

port 8004

cluster-enabled yes

cluster-config-file "nodes-8004.conf"

cluster-node-timeout 15000

dir "/opt/redisdata"

appendonly yes

appendfilename "appendonly-8004.aof"

logfile "/opt/redisdata/8004.log"

daemonize yes

pidfile "/var/run/redis-8www.cppcns.com004.pid"

dbfilename www.cppcns.com"dump-8004.rdb"

cluster-require-full-coverage no

# generated by config rewrite

requirepass "abc"

masterauth "abc"

測試:1.8002是主,8005是8002的從,現在要kill掉8002模擬主從切換

2.檢視8005的日誌,8005最終被選舉成新的master

21227:s 14 jun 07:28:49.343 # connection with master lost.

21227:s 14 jun 07:28:49.343 * caching the disconnected master state.

21227:s 14 jun 07:28:49.433 * connecting to master 127.0.0.1:8002

21227:s 14 jun 07:28:49.433 * master sl**e sync started

21227:s 14 jun 07:28:49.434 # error condition on socket for sync: connection refused

21227:s 14 jun 07:28:50.437 * connecting to master 127.0.0.1:8002

21227:s 14 jun 07:29:06.205 # start of election delayed for 564 milliseconds (rank #0, offset 337).

21227:s 14 jun 07:29:06.507 * connecting to master 127.0.0.1:8002

21227:s 14 jun 07:29:06.507 * master sl**e sync started

21227:s 14 jun 07:29:06.507 # error condition on socket for sync: connection refused

21227:s 14 jun 07:29:06.808 # starting a failover election for epoch 10.

21227:s 14 jun 07:29:06.810 # failover election won: i'm the new master.

21227:s 14 jun 07:29:06.810 # configepoch set to 10 after successful failover

21227:m 14 jun 07:29:06.810 * discarding previously cached master state.

3.再次啟動8002,檢視8005的日誌,8002已經變成8005的sl**e

21227:m 14 jun 07:32:53.511 * sl**e 127.0.0.1:8002 asks for synchronization

21227:m 14 jun 07:32:53.511 * full resync requested by sl**e 127.0.0.1:8002

21227:m 14 jun 07:32:53.511 * starting bgs**e for sync with target: disk

21227:m 14 jun 07:32:53.511 * background s**ing started by pid 21613

21613:c 14 jun 07:32:53.513 * db s**ed on disk

21613:c 14 jun 07:32:53.513 * rdb: 0 mb of memory used by copy-on-write

21227:m 14 jun 07:32:53.612 * background s**ing terminated with success

21227:m bwduokkuom14 jun 07:32:53.612 * synchronization with sl**e 127.0.0.1:8002 succeeded

bwduokkuom4.簡單測試了get set命令,正常

本文標題: 基於redis集群設定密碼的例項

本文位址:

redis集群密碼設定

1 密碼設定 推薦 方式一 修改所有redis集群中的redis.conf檔案加入 masterauth passwd123 requirepass passwd123說明 這種方式需要重新啟動各節點 方式二 進入各個例項進行設定 redis cli c p 7000 config set mast...

redis集群密碼設定

1 密碼設定 推薦 方式一 修改所有redis集群中的redis.conf檔案加入 masterauth passwd123 requirepass passwd123 說明 這種方式需要重新啟動各節點 方式二 進入各個例項進行設定 redis cli c p 7000 config set mas...

redis集群設定密碼

1 密碼設定 推薦 masterauth passwd123 requirepass passwd123 說明 這種方式需要重新啟動各節點 方式二 進入各個例項進行設定 redis cli c p 7000 config set masterauth passwd123 config set req...