redis5.0以上基于密碼認(rèn)證的集群cluster方式
本篇基于redis密碼認(rèn)證的集群cluster搭建。
以三主三從進(jìn)行測試,redis選用5.0以上的版本。
1、環(huán)境描述
服務(wù)器:centos7.9 redis:5.0.3 服務(wù)端口:6379,6378,6377,6376,6375,6374
2、下載安裝包
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
3、安裝redis
yum install gcc -y tar xvf redis-5.0.3.tar.gz cd redis-5.0.3 make && make install
4、修改配置文件
拷貝6份同樣的配置文件 mv redis.conf redis6379.conf echo 'redis6378.conf redis6377.conf redis6376.conf redis6375.conf redis6374.conf' | xargs -n 1 cp redis6379.conf
redis6379.conf修改:
69 #bind 127.0.0.1 #注釋掉此行 88 protected-mode no #關(guān)閉保護(hù)模式 136 daemonize yes #后臺啟動 158 pidfile /var/run/redis_6379.pid #啟動進(jìn)程文件 171 logfile "6379.log" #方便查看日志 253 dbfilename dump6379.rdb #數(shù)據(jù)文件 507 requirepass foobared #密碼認(rèn)證 293 masterauth foobared #主從切換時需要認(rèn)證的主庫密碼 以下是集群配置: 699 appendonly yes #開啟AOF模式 838 cluster-enabled yes #開啟 Redis Cluster 846 cluster-config-file nodes-6379.conf #集群配置文件,每個容器配置改唯一就行 852 cluster-node-timeout 5000 #集群中的節(jié)點能夠失聯(lián)的最大時間,超過這個時間,該節(jié)點就會被認(rèn)為故障
redis6378.conf修改:
69 #bind 127.0.0.1 #注釋掉此行 88 protected-mode no #關(guān)閉保護(hù)模式 136 daemonize yes #后臺啟動 158 pidfile /var/run/redis_6378.pid #啟動進(jìn)程文件 171 logfile "6378.log" #方便查看日志 253 dbfilename dump6378.rdb #數(shù)據(jù)文件 507 requirepass foobared #密碼認(rèn)證 293 masterauth foobared #主從切換時需要認(rèn)證的主庫密碼 以下是集群配置: 699 appendonly yes #開啟AOF模式 838 cluster-enabled yes #開啟 Redis Cluster 846 cluster-config-file nodes-6378.conf #集群配置文件,每個容器配置改唯一就行 852 cluster-node-timeout 5000 #集群中的節(jié)點能夠失聯(lián)的最大時間,超過這個時間,該節(jié)點就會被認(rèn)為故障
redis6377.conf修改:
69 #bind 127.0.0.1 #注釋掉此行 88 protected-mode no #關(guān)閉保護(hù)模式 136 daemonize yes #后臺啟動 158 pidfile /var/run/redis_6377.pid #啟動進(jìn)程文件 171 logfile "6377.log" #方便查看日志 253 dbfilename dump6377.rdb #數(shù)據(jù)文件 507 requirepass foobared #密碼認(rèn)證 293 masterauth foobared #主從切換時需要認(rèn)證的主庫密碼 以下是集群配置: 699 appendonly yes #開啟AOF模式 838 cluster-enabled yes #開啟 Redis Cluster 846 cluster-config-file nodes-6377.conf #集群配置文件,每個容器配置改唯一就行 852 cluster-node-timeout 5000 #集群中的節(jié)點能夠失聯(lián)的最大時間,超過這個時間,該節(jié)點就會被認(rèn)為故障
redis6376.conf修改:
69 #bind 127.0.0.1 #注釋掉此行 88 protected-mode no #關(guān)閉保護(hù)模式 136 daemonize yes #后臺啟動 158 pidfile /var/run/redis_6376.pid #啟動進(jìn)程文件 171 logfile "6376.log" #方便查看日志 253 dbfilename dump6376.rdb #數(shù)據(jù)文件 507 requirepass foobared #密碼認(rèn)證 293 masterauth foobared #主從切換時需要認(rèn)證的主庫密碼 以下是集群配置: 699 appendonly yes #開啟AOF模式 838 cluster-enabled yes #開啟 Redis Cluster 846 cluster-config-file nodes-6376.conf #集群配置文件,每個容器配置改唯一就行 852 cluster-node-timeout 5000 #集群中的節(jié)點能夠失聯(lián)的最大時間,超過這個時間,該節(jié)點就會被認(rèn)為故障
redis6375.conf修改:
69 #bind 127.0.0.1 #注釋掉此行 88 protected-mode no #關(guān)閉保護(hù)模式 136 daemonize yes #后臺啟動 158 pidfile /var/run/redis_6375.pid #啟動進(jìn)程文件 171 logfile "6375.log" #方便查看日志 253 dbfilename dump6375.rdb #數(shù)據(jù)文件 507 requirepass foobared #密碼認(rèn)證 293 masterauth foobared #主從切換時需要認(rèn)證的主庫密碼 以下是集群配置: 699 appendonly yes #開啟AOF模式 838 cluster-enabled yes #開啟 Redis Cluster 846 cluster-config-file nodes-6375.conf #集群配置文件,每個容器配置改唯一就行 852 cluster-node-timeout 5000 #集群中的節(jié)點能夠失聯(lián)的最大時間,超過這個時間,該節(jié)點就會被認(rèn)為故障
redis6374.conf修改:
69 #bind 127.0.0.1 #注釋掉此行 88 protected-mode no #關(guān)閉保護(hù)模式 136 daemonize yes #后臺啟動 158 pidfile /var/run/redis_6374.pid #啟動進(jìn)程文件 171 logfile "6374.log" #方便查看日志 253 dbfilename dump6374.rdb #數(shù)據(jù)文件 507 requirepass foobared #密碼認(rèn)證 293 masterauth foobared #主從切換時需要認(rèn)證的主庫密碼 以下是集群配置: 699 appendonly yes #開啟AOF模式 838 cluster-enabled yes #開啟 Redis Cluster 846 cluster-config-file nodes-6374.conf #集群配置文件,每個容器配置改唯一就行 852 cluster-node-timeout 5000 #集群中的節(jié)點能夠失聯(lián)的最大時間,超過這個時間,該節(jié)點就會被認(rèn)為故障
說明:每個配置文件pidfile,logfile,dbfilename,cluster-config-file不同,其余配置均相同。
5、啟動服務(wù)
在redis解壓目錄下依次啟動 redis-server redis6379.conf redis-server redis6378.conf redis-server redis6377.conf redis-server redis6376.conf redis-server redis6375.conf redis-server redis6374.conf
[root@iZuf612i9bshiuw3zzlfe9Z redis-5.0.3]# ps -aux |grep redis root 16956 0.0 0.1 153980 2840 ? Ssl 09:36 0:00 redis-server *:6379 [cluster] root 16965 0.0 0.1 153980 2844 ? Ssl 09:36 0:00 redis-server *:6378 [cluster] root 16972 0.0 0.1 153980 2848 ? Ssl 09:36 0:00 redis-server *:6377 [cluster] root 16980 0.0 0.1 153980 2848 ? Ssl 09:36 0:00 redis-server *:6376 [cluster] root 16994 0.0 0.1 153980 2848 ? Ssl 09:36 0:00 redis-server *:6375 [cluster] root 17003 0.0 0.1 153980 2852 ? Ssl 09:36 0:00 redis-server *:6374 [cluster] root 17015 0.0 0.0 112808 964 pts/0 S+ 09:36 0:00 grep --color=auto redis
6、創(chuàng)建集群
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6378 127.0.0.1:6377 127.0.0.1:6376 127.0.0.1:6375 127.0.0.1:6374 --cluster-replicas 1 -a foobared
7、數(shù)據(jù)驗證
#登錄 redis-cli -p 端口-a foobared -c
說明:-c的目的是為解決“(error) MOVED 5798”報錯
[root@iZuf612i9bshiuw3zzlfe9Z redis-5.0.3]# redis-cli -p 6379 -a foobared Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> set name mike (error) MOVED 5798 127.0.0.1:6378 127.0.0.1:6379> exit [root@iZuf612i9bshiuw3zzlfe9Z redis-5.0.3]# redis-cli -p 6379 -a foobared -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> set name mike -> Redirected to slot [5798] located at 127.0.0.1:6378 OK
《數(shù)據(jù)驗證》驗證如下:
《主從切換驗證》驗證如下:
6374為6378的從,現(xiàn)在停掉6378進(jìn)行測試
切換之前:
切換之后:
到此,redis集群基于5.0以上版本密碼認(rèn)證搭建?;赿ocker的redis集群的搭建可參考:基于docker的redis集群搭建
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Redis中什么是Big?Key(大key)問題?如何解決Big?Key問題?
大key并不是指key的值很大,而是key對應(yīng)的value很大,下面這篇文章主要給大家介紹了Redis中什么是Big?Key(大key)問題?如何解決Big?Key問題的相關(guān)資料,需要的朋友可以參考下2023-03-03淺談一下Redis的數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了淺談一下Redis的數(shù)據(jù)結(jié)構(gòu),簡單字符串結(jié)構(gòu)被用于存儲redis的key對象和String類型的value對象,其中的free和len字段可以輕松的使得在該字符串被修改時判斷是否需要擴(kuò)容,需要的朋友可以參考下2023-08-08Redis事務(wù)機制與Springboot項目中的使用方式
Redis事務(wù)機制允許將多個命令打包在一起,作為一個原子操作來執(zhí)行,開啟事務(wù)使用MULTI命令,執(zhí)行事務(wù)使用EXEC命令,取消事務(wù)使用DISCARD命令,監(jiān)視一個或多個鍵使用WATCH命令,Redis事務(wù)的核心思想是將多個命令放入一個隊列中2025-03-03