redis默認只允許本地訪問,要使redis可以遠程訪問可以修改redis.conf
解決辦法:注釋掉bind 127.0.0.1可以使所有的ip訪問redis
若是想指定多個ip訪問,但並不是全部的ip訪問,可以bind
在redis3.2之后,redis增加了protected-mode,在這個模式下,即使注釋掉了bind 127.0.0.1,再訪問redisd時候還是報錯
修改辦法:protected-mode no
綁定IP:
sed -i 's/bind 127.0.0.1/bind 192.168.152.100/' redis.conf
sed -i 's/protected-mode yes/protected-mode no/' redis.conf
sed -i 's/protected-mode yes/protected-mode no/' redis.conf
創建成功:(僅在192.168.152.100上執行)
[root@localhost redis-3.2.1]# /usr/local/redis-3.2.1/src/redis-trib.rb create --replicas 1 \
> 192.168.152.100:7000 \
> 192.168.152.100:7001 \
> 192.168.152.100:7002 \
> \
> 192.168.152.101:7003 \
> 192.168.152.101:7004 \
> 192.168.152.101:7005 \
>
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.152.100:7000
192.168.152.101:7003
192.168.152.100:7001
Adding replica 192.168.152.101:7004 to 192.168.152.100:7000
Adding replica 192.168.152.100:7002 to 192.168.152.101:7003
Adding replica 192.168.152.101:7005 to 192.168.152.100:7001
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
S: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
S: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
S: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 192.168.152.100:7000)
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
M: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
slots: (0 slots) master
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
M: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
slots: (0 slots) master
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
M: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
slots: (0 slots) master
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-3.2.1]#
> 192.168.152.100:7000 \
> 192.168.152.100:7001 \
> 192.168.152.100:7002 \
> \
> 192.168.152.101:7003 \
> 192.168.152.101:7004 \
> 192.168.152.101:7005 \
>
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.152.100:7000
192.168.152.101:7003
192.168.152.100:7001
Adding replica 192.168.152.101:7004 to 192.168.152.100:7000
Adding replica 192.168.152.100:7002 to 192.168.152.101:7003
Adding replica 192.168.152.101:7005 to 192.168.152.100:7001
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
S: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
S: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
S: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 192.168.152.100:7000)
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
M: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
slots: (0 slots) master
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
M: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
slots: (0 slots) master
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
M: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
slots: (0 slots) master
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-3.2.1]#
測試:
redis集群有16383個slot組成,通過分片分布到多個節點上,讀寫都發生在master節點。
redis.conf 將 bind 改為了ip地址,所以 -h 參數不省略。
[root@localhost redis-3.2.1]# redis-cli -h 192.168.152.100 -c -p 7000
192.168.152.100:7000> set hello isok
OK
192.168.152.100:7000> get hello
"isok"
[root@localhost redis-3.2.1]# redis-cli -h 192.168.152.101 -c -p 7003
192.168.152.101:7003> get hello
-> Redirected to slot [866] located at 192.168.152.100:7000
"isok"
192.168.152.100:7000>
192.168.152.100:7000> set hello isok
OK
192.168.152.100:7000> get hello
"isok"
[root@localhost redis-3.2.1]# redis-cli -h 192.168.152.101 -c -p 7003
192.168.152.101:7003> get hello
-> Redirected to slot [866] located at 192.168.152.100:7000
"isok"
192.168.152.100:7000>