文章目录
- 1、清空主从复制和哨兵模式留下的一些文件
- 2、appendonly修改回no
- [3、开启daemonize yes](#3、开启daemonize yes)
- [4、protect-mode no](#4、protect-mode no)
- 5、注释掉bind
- 6、制作六个实例的配置文件
- 7、启动六个服务
- 8、将六个服务合并为一个集群
- 9、集群的登录
- 10、集群中录入值
1、清空主从复制和哨兵模式留下的一些文件
bash
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ll
总用量 248
drwxr-xr-x. 2 root root 150 12月 6 2023 bin
-rw-r--r--. 1 root root 89 6月 24 14:16 dump6379.rdb
-rw-r--r--. 1 root root 173 6月 24 14:18 dump6380.rdb
-rw-r--r--. 1 root root 89 6月 24 14:19 dump6381.rdb
-rw-r--r--. 1 root root 89 12月 20 2023 dump.rdb
-rw-r--r--. 1 root root 435 6月 24 16:28 redis_6379.conf
-rw-r--r--. 1 root root 405 6月 24 16:10 redis_6380.conf
-rw-r--r--. 1 root root 435 6月 24 16:10 redis_6381.conf
-rw-r--r--. 1 root root 106547 6月 24 13:59 redis_common.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
-rw-r--r--. 1 root root 464 6月 24 16:10 sentinel.conf
[root@localhost redis]#
1.1、删除以rdb后缀名的文件
bash
[root@localhost redis]# rm -rf *.rdb
[root@localhost redis]# ll
总用量 232
drwxr-xr-x. 2 root root 150 12月 6 2023 bin
-rw-r--r--. 1 root root 435 6月 24 16:28 redis_6379.conf
-rw-r--r--. 1 root root 405 6月 24 16:10 redis_6380.conf
-rw-r--r--. 1 root root 435 6月 24 16:10 redis_6381.conf
-rw-r--r--. 1 root root 106547 6月 24 13:59 redis_common.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
-rw-r--r--. 1 root root 464 6月 24 16:10 sentinel.conf
[root@localhost redis]#
1.2、删除主从复制的配置文件
bash
[root@localhost redis]# rm -rf redis_6*
[root@localhost redis]# ll
总用量 220
drwxr-xr-x. 2 root root 150 12月 6 2023 bin
-rw-r--r--. 1 root root 106547 6月 24 13:59 redis_common.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
-rw-r--r--. 1 root root 464 6月 24 16:10 sentinel.conf
[root@localhost redis]#
bash
[root@localhost redis]# rm -rf redis_common.conf
[root@localhost redis]# ll
总用量 112
drwxr-xr-x. 2 root root 150 12月 6 2023 bin
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
-rw-r--r--. 1 root root 464 6月 24 16:10 sentinel.conf
[root@localhost redis]#
1.3、删除哨兵模式的配置文件
bash
[root@localhost redis]# rm -rf sentinel.conf
[root@localhost redis]# ll
总用量 108
drwxr-xr-x. 2 root root 150 12月 6 2023 bin
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]#
bash
[root@localhost redis]# cd bin/
[root@localhost bin]# ll
总用量 29184
-rw-r--r--. 1 root root 89 12月 6 2023 dump.rdb
-rwxr-xr-x. 1 root root 7363432 12月 6 2023 redis-benchmark
lrwxrwxrwx. 1 root root 12 12月 6 2023 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root 12 12月 6 2023 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root 7650944 12月 6 2023 redis-cli
lrwxrwxrwx. 1 root root 12 12月 6 2023 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 14863400 12月 6 2023 redis-server
[root@localhost bin]# rm -rf dump.rdb
[root@localhost bin]# ll
总用量 29180
-rwxr-xr-x. 1 root root 7363432 12月 6 2023 redis-benchmark
lrwxrwxrwx. 1 root root 12 12月 6 2023 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root 12 12月 6 2023 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root 7650944 12月 6 2023 redis-cli
lrwxrwxrwx. 1 root root 12 12月 6 2023 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 14863400 12月 6 2023 redis-server
[root@localhost bin]#
2、appendonly修改回no
bash
[root@localhost redis]# pwd
/usr/local/redis
[root@localhost redis]# ll
总用量 108
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim redis.conf
3、开启daemonize yes
4、protect-mode no
5、注释掉bind
6、制作六个实例的配置文件
6.1、制作配置文件redis6379.conf
bash
include /usr/local/redis/redis.conf
port 6379
pidfile "/var/run/redis_6379.pid"
dbfilename "dump6379.rdb"
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
bash
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ll
总用量 108
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim redis6379.conf
bash
[root@localhost redis]# ll
总用量 112
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]#
6.2、制作配置文件redis6380.conf
bash
[root@localhost redis]# cp redis6379.conf redis6380.conf
[root@localhost redis]# ll
总用量 116
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 189 6月 25 12:58 redis6380.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim redis6380.conf
6.3、制作配置文件redis6381.conf
bash
[root@localhost redis]# cp redis6379.conf redis6381.conf
[root@localhost redis]# ll
总用量 120
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 189 6月 25 13:03 redis6380.conf
-rw-r--r--. 1 root root 189 6月 25 13:05 redis6381.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim redis6381.conf
6.4、制作配置文件redis6382.conf
bash
[root@localhost redis]# cp redis6379.conf redis6382.conf
[root@localhost redis]# ll
总用量 124
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 189 6月 25 13:03 redis6380.conf
-rw-r--r--. 1 root root 189 6月 25 13:07 redis6381.conf
-rw-r--r--. 1 root root 189 6月 25 13:09 redis6382.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim redis6382.conf
6.5、制作配置文件redis6383.conf
bash
[root@localhost redis]# cp redis6379.conf redis6383.conf
[root@localhost redis]# ll
总用量 128
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 189 6月 25 13:03 redis6380.conf
-rw-r--r--. 1 root root 189 6月 25 13:07 redis6381.conf
-rw-r--r--. 1 root root 189 6月 25 13:10 redis6382.conf
-rw-r--r--. 1 root root 189 6月 25 13:11 redis6383.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim redis6383.conf
6.6、制作配置文件redis6384.conf
bash
[root@localhost redis]# cp redis6379.conf redis6384.conf
[root@localhost redis]# ll
总用量 132
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 189 6月 25 13:03 redis6380.conf
-rw-r--r--. 1 root root 189 6月 25 13:07 redis6381.conf
-rw-r--r--. 1 root root 189 6月 25 13:10 redis6382.conf
-rw-r--r--. 1 root root 189 6月 25 13:12 redis6383.conf
-rw-r--r--. 1 root root 189 6月 25 13:14 redis6384.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim redis6384.conf
6.7、六个配置文件制作完成
bash
[root@localhost redis]# ll
总用量 132
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 189 6月 25 13:03 redis6380.conf
-rw-r--r--. 1 root root 189 6月 25 13:07 redis6381.conf
-rw-r--r--. 1 root root 189 6月 25 13:10 redis6382.conf
-rw-r--r--. 1 root root 189 6月 25 13:12 redis6383.conf
-rw-r--r--. 1 root root 189 6月 25 13:15 redis6384.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# pwd
/usr/local/redis
[root@localhost redis]#
在这里插入代码片
7、启动六个服务
7.1、关闭主从复制启动的三个服务器
bash
[root@localhost ~]# ps -ef | grep redis | grep -v grep
root 6965 1 0 08:05 ? 00:00:33 /usr/local/redis/bin/redis-server *:6380
root 6980 1 0 08:06 ? 00:00:33 /usr/local/redis/bin/redis-server *:6381
root 8318 1 0 10:13 ? 00:00:21 /usr/local/redis/bin/redis-server *:6379
root 9279 9105 0 11:35 pts/5 00:00:00 vim redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6379 shutdown
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6380 shutdown
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6381 shutdown
[root@localhost ~]# ps -ef | grep redis | grep -v grep
root 9279 9105 0 11:35 pts/5 00:00:00 vim redis.conf
[root@localhost ~]#
bash
[root@localhost redis]# /usr/local/redis/bin/redis-server /usr/local/redis/redis6379.conf
[root@localhost redis]# /usr/local/redis/bin/redis-server /usr/local/redis/redis6380.conf
[root@localhost redis]# /usr/local/redis/bin/redis-server /usr/local/redis/redis6381.conf
[root@localhost redis]# /usr/local/redis/bin/redis-server /usr/local/redis/redis6382.conf
[root@localhost redis]# /usr/local/redis/bin/redis-server /usr/local/redis/redis6383.conf
[root@localhost redis]# /usr/local/redis/bin/redis-server /usr/local/redis/redis6384.conf
[root@localhost redis]# ps -ef | grep redis | grep -v grep
root 11403 1 0 14:08 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379 [cluster]
root 11412 1 0 14:09 ? 00:00:00 /usr/local/redis/bin/redis-server *:6380 [cluster]
root 11418 1 0 14:09 ? 00:00:00 /usr/local/redis/bin/redis-server *:6381 [cluster]
root 11424 1 0 14:09 ? 00:00:00 /usr/local/redis/bin/redis-server *:6382 [cluster]
root 11430 1 0 14:09 ? 00:00:00 /usr/local/redis/bin/redis-server *:6383 [cluster]
root 11436 1 0 14:09 ? 00:00:00 /usr/local/redis/bin/redis-server *:6384 [cluster]
[root@localhost redis]#
7.2、所有redis实例启动后,nodes-xxxx.conf文件都生成正常
bash
[root@localhost redis]# ll
总用量 156
drwxr-xr-x. 2 root root 134 6月 24 17:21 bin
-rw-r--r--. 1 root root 114 6月 25 14:08 nodes-6379.conf
-rw-r--r--. 1 root root 114 6月 25 14:09 nodes-6380.conf
-rw-r--r--. 1 root root 114 6月 25 14:09 nodes-6381.conf
-rw-r--r--. 1 root root 114 6月 25 14:09 nodes-6382.conf
-rw-r--r--. 1 root root 114 6月 25 14:09 nodes-6383.conf
-rw-r--r--. 1 root root 114 6月 25 14:09 nodes-6384.conf
-rw-r--r--. 1 root root 189 6月 25 12:48 redis6379.conf
-rw-r--r--. 1 root root 189 6月 25 13:03 redis6380.conf
-rw-r--r--. 1 root root 189 6月 25 13:07 redis6381.conf
-rw-r--r--. 1 root root 189 6月 25 13:10 redis6382.conf
-rw-r--r--. 1 root root 189 6月 25 13:12 redis6383.conf
-rw-r--r--. 1 root root 189 6月 25 13:15 redis6384.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# pwd
/usr/local/redis
[root@localhost redis]#
8、将六个服务合并为一个集群
此处不要用127.0.0.1, 请用真实IP地址 --replicas 1 采用最简单的方式配置集群,一台主机,一台从机,正好三组
8.1、运行如下命令
bash
redis-cli --cluster create --cluster-replicas 1 192.168.74.148:6379 192.168.74.148:6380 192.168.74.148:6381 192.168.74.148:6382 192.168.74.148:6383 192.168.74.148:6384
bash
[root@localhost redis]# /usr/local/redis/bin/redis-cli --cluster create --cluster-replicas 1 192.168.74.148:6379 192.168.74.148:6380 192.168.74.148:6381 192.168.74.148:6382 192.168.74.148:6383 192.168.74.148:6384
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.74.148:6383 to 192.168.74.148:6379
Adding replica 192.168.74.148:6384 to 192.168.74.148:6380
Adding replica 192.168.74.148:6382 to 192.168.74.148:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 192.168.74.148:6379
slots:[0-5460] (5461 slots) master
M: aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 192.168.74.148:6380
slots:[5461-10922] (5462 slots) master
M: 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 192.168.74.148:6381
slots:[10923-16383] (5461 slots) master
S: a569032e711a267a419c99719c3b87683c2fea6c 192.168.74.148:6382
replicates 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8
S: 5a6df4b92983ecfae646e3c7f5f2851ef118f9c0 192.168.74.148:6383
replicates dcb82723f90f0a2ab6af8fb008b5847f5aa0df45
S: f749b8d27581caf20b7a3d339d43621fbffa4962 192.168.74.148:6384
replicates aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9
Can I set the above configuration? (type 'yes' to accept):
bash
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.74.148:6379)
M: dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 192.168.74.148:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 5a6df4b92983ecfae646e3c7f5f2851ef118f9c0 192.168.74.148:6383
slots: (0 slots) slave
replicates dcb82723f90f0a2ab6af8fb008b5847f5aa0df45
M: aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 192.168.74.148:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a569032e711a267a419c99719c3b87683c2fea6c 192.168.74.148:6382
slots: (0 slots) slave
replicates 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8
M: 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 192.168.74.148:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: f749b8d27581caf20b7a3d339d43621fbffa4962 192.168.74.148:6384
slots: (0 slots) slave
replicates aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis]#
9、集群的登录
登录指令添加 -c 代表以集群方式登录
9.1、登录主机6379
bash
[root@localhost redis]# /usr/local/redis/bin/redis-cli -c -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
bash
[root@localhost redis]# /usr/local/redis/bin/redis-cli -c -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set k1 v1
-> Redirected to slot [12706] located at 192.168.74.148:6381
OK
192.168.74.148:6381> set k2 v2
-> Redirected to slot [449] located at 192.168.74.148:6379
OK
192.168.74.148:6379>
bash
192.168.74.148:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.74.148,port=6383,state=online,offset=2306,lag=1
master_failover_state:no-failover
master_replid:ab2c4b92097802299003be0ec17821ed7eb68d27
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2306
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2306
192.168.74.148:6379>
9.2、登录从机6382,只能读不能写
bash
[root@localhost redis]# /usr/local/redis/bin/redis-cli -c -p 6382
127.0.0.1:6382> info replication
# Replication
role:slave
master_host:192.168.74.148
master_port:6381
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_read_repl_offset:2376
slave_repl_offset:2376
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:5af18e242e643710fa639780fb6e4c3b8157846e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2376
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2362
127.0.0.1:6382>
9.3、登录主机6379,查看集群信息
在redis cluster架构下,每个redis要放开两个端口号,比如一个是6379,另外一个就是加10000的端口号,比如16379,16379端口号是用来进行节点间通信的,也就是cluster bus的东西,集群总线。cluster bus的通信,用来进行故障检测,配置更新,故障转移授权。
bash
[root@localhost redis]# /usr/local/redis/bin/redis-cli -c -p 6379
127.0.0.1:6379> CLUSTER NODES
5a6df4b92983ecfae646e3c7f5f2851ef118f9c0 192.168.74.148:6383@16383 slave dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 0 1719298534642 1 connected
aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 192.168.74.148:6380@16380 master - 0 1719298536661 2 connected 5461-10922
a569032e711a267a419c99719c3b87683c2fea6c 192.168.74.148:6382@16382 slave 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 0 1719298534000 3 connected
3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 192.168.74.148:6381@16381 master - 0 1719298533635 3 connected 10923-16383
f749b8d27581caf20b7a3d339d43621fbffa4962 192.168.74.148:6384@16384 slave aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 0 1719298535651 2 connected
dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 192.168.74.148:6379@16379 myself,master - 0 1719298534000 1 connected 0-5460
127.0.0.1:6379>
10、集群中录入值
10.1、不在一个slot下的键值,是不能使用mget,mset等多键操作
bash
[root@localhost redis]# /usr/local/redis/bin/redis-cli -c -h 192.168.74.148 -p 6379
192.168.74.148:6379> CLUSTER NODES
5a6df4b92983ecfae646e3c7f5f2851ef118f9c0 192.168.74.148:6383@16383 slave dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 0 1719298643571 1 connected
aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 192.168.74.148:6380@16380 master - 0 1719298642563 2 connected 5461-10922
a569032e711a267a419c99719c3b87683c2fea6c 192.168.74.148:6382@16382 slave 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 0 1719298641000 3 connected
3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 192.168.74.148:6381@16381 master - 0 1719298643000 3 connected 10923-16383
f749b8d27581caf20b7a3d339d43621fbffa4962 192.168.74.148:6384@16384 slave aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 0 1719298641556 2 connected
dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 192.168.74.148:6379@16379 myself,master - 0 1719298643000 1 connected 0-5460
192.168.74.148:6379> keys *
(empty array)
192.168.74.148:6379> mset k1 v1 k2 v2
(error) CROSSSLOT Keys in request don't hash to the same slot
192.168.74.148:6379> keys *
(empty array)
192.168.74.148:6379>
10.2、可以通过{}来定义组的概念,从而使key中{}内相同内容的键值对放到一个slot中去
bash
192.168.74.148:6379> mset k1{aa} v1 k2{aa} v2
OK
192.168.74.148:6379> mset k3{bc} v3 k4{bc} v4
-> Redirected to slot [12685] located at 192.168.74.148:6381
OK
192.168.74.148:6381> keys *
1) "k3{bc}"
2) "k4{bc}"
3) "k1"
192.168.74.148:6381>