redis默认只容许本地访问,要使redis能够远程访问能够修改redis.confnode
解决办法:注释掉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>