1. 准备工作
a.集群redis包
下载
b.下载的包中有6个redis节点,端口7001、7002、7003、7004、7005、7006,使用时请注意端口是否占用,密码为123455
c.如果需要修改端口和密码,可以在/data/redis-cluster/7001/etc/redis.conf配置文件中修改
[root@wanwan redis-cluster]# cat /data/redis-cluster/7001/etc/redis.conf
#redis密码
requirepass 123456
#redis端口
port 7001
2. 启动redis
通过start_all.sh脚本批量启动所有redis,通过stop_all.sh脚本批量关闭redis
[root@wanwan redis-cluster]# pwd
/data/redis-cluster
[root@wanwan redis-cluster]# ll
总用量 8
drwxr-xr-x. 5 root root 100 4月 13 05:26 7001
drwxr-xr-x. 5 root root 88 4月 13 05:26 7002
drwxr-xr-x. 5 root root 88 4月 13 05:26 7003
drwxr-xr-x. 5 root root 88 4月 13 05:26 7004
drwxr-xr-x. 5 root root 88 4月 13 05:25 7005
drwxr-xr-x. 5 root root 88 4月 13 05:25 7006
-rwxrwxrwx. 1 root root 545 7月 24 2019 start_all.sh
-rwxrwxr-x. 1 root root 974 4月 12 12:53 stop_all.sh
[root@wanwan redis-cluster]# sh start_all.sh
321746:C 13 Apr 2022 05:26:34.211 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
321746:C 13 Apr 2022 05:26:34.211 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=321746, just started
321746:C 13 Apr 2022 05:26:34.211 # Configuration loaded
321755:C 13 Apr 2022 05:26:39.234 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
321755:C 13 Apr 2022 05:26:39.234 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=321755, just started
321755:C 13 Apr 2022 05:26:39.234 # Configuration loaded
321772:C 13 Apr 2022 05:26:44.256 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
321772:C 13 Apr 2022 05:26:44.256 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=321772, just started
321772:C 13 Apr 2022 05:26:44.256 # Configuration loaded
321781:C 13 Apr 2022 05:26:49.279 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
321781:C 13 Apr 2022 05:26:49.279 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=321781, just started
321781:C 13 Apr 2022 05:26:49.279 # Configuration loaded
321790:C 13 Apr 2022 05:26:54.302 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
321790:C 13 Apr 2022 05:26:54.302 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=321790, just started
321790:C 13 Apr 2022 05:26:54.302 # Configuration loaded
321799:C 13 Apr 2022 05:26:59.325 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
321799:C 13 Apr 2022 05:26:59.325 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=321799, just started
321799:C 13 Apr 2022 05:26:59.325 # Configuration loaded
[root@wanwan redis-cluster]#
3. 查看redis是否启动成功
[root@wanwan redis-cluster]# ps -ef|grep redis
root 321747 1 0 05:26 ? 00:00:00 /data/redis-cluster/7001/bin/redis-server *:7001 [cluster]
root 321756 1 0 05:26 ? 00:00:00 /data/redis-cluster/7002/bin/redis-server *:7002 [cluster]
root 321773 1 0 05:26 ? 00:00:00 /data/redis-cluster/7003/bin/redis-server *:7003 [cluster]
root 321782 1 0 05:26 ? 00:00:00 /data/redis-cluster/7004/bin/redis-server *:7004 [cluster]
root 321791 1 0 05:26 ? 00:00:00 /data/redis-cluster/7005/bin/redis-server *:7005 [cluster]
root 321800 1 0 05:26 ? 00:00:00 /data/redis-cluster/7006/bin/redis-server *:7006 [cluster]
root 321912 320809 0 05:35 pts/0 00:00:00 grep --color=auto redis
[root@wanwan redis-cluster]#
[root@wanwan redis-cluster]# netstat -nltp|grep redis
tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 321747/redis-server
tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 321756/redis-server
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 321773/redis-server
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 321782/redis-server
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 321791/redis-server
tcp 0 0 0.0.0.0:17006 0.0.0.0:* LISTEN 321800/redis-server
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 321747/redis-server
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 321756/redis-server
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 321773/redis-server
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 321782/redis-server
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 321791/redis-server
tcp 0 0 0.0.0.0:7006 0.0.0.0:* LISTEN 321800/redis-server
tcp6 0 0 :::17001 :::* LISTEN 321747/redis-server
tcp6 0 0 :::17002 :::* LISTEN 321756/redis-server
tcp6 0 0 :::17003 :::* LISTEN 321773/redis-server
tcp6 0 0 :::17004 :::* LISTEN 321782/redis-server
tcp6 0 0 :::17005 :::* LISTEN 321791/redis-server
tcp6 0 0 :::17006 :::* LISTEN 321800/redis-server
tcp6 0 0 :::7001 :::* LISTEN 321747/redis-server
tcp6 0 0 :::7002 :::* LISTEN 321756/redis-server
tcp6 0 0 :::7003 :::* LISTEN 321773/redis-server
tcp6 0 0 :::7004 :::* LISTEN 321782/redis-server
tcp6 0 0 :::7005 :::* LISTEN 321791/redis-server
tcp6 0 0 :::7006 :::* LISTEN 321800/redis-server
[root@wanwan redis-cluster]#
4. 创建redis集群
执行命令后会弹出确认,一定要记得输入yes
[root@wanwan redis-cluster]# cd 7001/bin
[root@wanwan bin]# ll
总用量 40668
drwxr-xr-x. 3 root root 75 5月 26 2021 agent_tmp
-rwxr-xr-x. 1 root root 734 7月 23 2019 mkreleasehdr.sh
-rwxr-xr-x. 1 root root 4366592 7月 23 2019 redis-benchmark
-rwxr-xr-x. 1 root root 8111800 7月 23 2019 redis-check-aof
-rwxr-xr-x. 1 root root 8111800 7月 23 2019 redis-check-rdb
-rwxr-xr-x. 1 root root 4806824 7月 23 2019 redis-cli
-rwxr-xr-x. 1 root root 8111800 7月 23 2019 redis-sentinel
-rwxr-xr-x. 1 root root 8111800 7月 23 2019 redis-server
-rwxr-xr-x. 1 root root 3600 7月 23 2019 redis-trib.rb
[root@wanwan bin]# ./redis-cli -a 123456 --cluster create 172.16.17.208:7001 172.16.17.208:7002 172.16.17.208:7003 172.16.17.208:7004 172.16.17.208:7005 172.16.17.208:7006
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 2730
Master[1] -> Slots 2731 - 5460
Master[2] -> Slots 5461 - 8191
Master[3] -> Slots 8192 - 10922
Master[4] -> Slots 10923 - 13652
Master[5] -> Slots 13653 - 16383
M: 1e45ed256292db35537cebd81bfaf115114bd7c0 172.16.17.208:7001
slots:[0-2730] (2731 slots) master
M: b40f67d276487608a1e67f2653743bd2a7ce3c37 172.16.17.208:7002
slots:[2731-5460] (2730 slots) master
M: 96e3f0c09c027da8a2d54d4a26fc6db214ee1a17 172.16.17.208:7003
slots:[5461-8191] (2731 slots) master
M: 7d7444d171d2e8792bf672f6fc4ff20ba8339bb7 172.16.17.208:7004
slots:[8192-10922] (2731 slots) master
M: bcad065c97331730be619a7b54c7c6a51ef3dbfc 172.16.17.208:7005
slots:[10923-13652] (2730 slots) master
M: f7a02c5330b5836ce78280224f40796fcbd92ae1 172.16.17.208:7006
slots:[13653-16383] (2731 slots) master
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 172.16.17.208:7001)
M: 1e45ed256292db35537cebd81bfaf115114bd7c0 172.16.17.208:7001
slots:[0-2730] (2731 slots) master
M: bcad065c97331730be619a7b54c7c6a51ef3dbfc 172.16.17.208:7005
slots:[10923-13652] (2730 slots) master
M: 96e3f0c09c027da8a2d54d4a26fc6db214ee1a17 172.16.17.208:7003
slots:[5461-8191] (2731 slots) master
M: 7d7444d171d2e8792bf672f6fc4ff20ba8339bb7 172.16.17.208:7004
slots:[8192-10922] (2731 slots) master
M: f7a02c5330b5836ce78280224f40796fcbd92ae1 172.16.17.208:7006
slots:[13653-16383] (2731 slots) master
M: b40f67d276487608a1e67f2653743bd2a7ce3c37 172.16.17.208:7002
slots:[2731-5460] (2730 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@wanwan bin]#
5. 连接redis
本机连接不需要输入ip地址,如果是远程连接需要加-h ip
[root@wanwan bin]# ./redis-cli -a 123456 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7001>
6. 查看集群状态
集群创建成功==》cluster_state:ok
集群未创建==》cluster_state:fail
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:6
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:99
cluster_stats_messages_pong_sent:89
cluster_stats_messages_sent:188
cluster_stats_messages_ping_received:84
cluster_stats_messages_pong_received:99
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:188
127.0.0.1:7001>
7. 通过set和get方法插入和查询
尝试redis是否能否正常使用,通过set插入缓存数据,通过get获取缓存数据
127.0.0.1:7001> set key12345 value12345
OK
127.0.0.1:7001> get key12345
"value12345"
127.0.0.1:7001>
总结:
可视化工具
博主留言:如果没有积分下载的朋友可以在评论区留言,我看到了可以私下发出来,csdn上传资源现在默认就是5分;