cd /data/base
wget https://download.redis.io/releases/redis-6.2.12.tar.gz
tar -zxvf redis-6.2.12.tar.gz
# 将redis代码移动到src目录
mv redis-6.2.12 redis-6.2.12-src
# 创建新目录用户存放编译好的redis文件
mkdir redis-6.2.12
# 指定内存库为libc,或者使用jmelloc
make -j4 MALLOC=libc
# 安装时指定目标文件目录
make PREFIX=/data/base/redis-6.2.12 install
cd redis-6.2.12
vim redis.conf
# 后台启动redis
daemonize yes
# 修改pid文件保存目录
pidfile /data/base/redis-6.2.12/redis_6379.pid
# 修改日志文件路径和名称
logfile "redis.log"
# 修改密码
requirepass 123456
# 允许远程登录
bind 0.0.0.0 -::1
./redis-server redis.conf
[root@redis1 bin]# ./redis-server redis.conf
[root@redis1 bin]# ps -ef|grep redis
root 36152 1 0 09:40 ? 00:00:00 ./redis-server 0.0.0.0:6379
root 36303 1566 0 09:40 pts/0 00:00:00 grep --color=auto redis
登录redis
[root@redis1 bin]# ./redis-cli -h localhost -p 6379
localhost:6379> auth 123456
OK
ip |
port |
部署路径 |
10.16.60.81 |
6379 |
/data/base/redis-6379 |
10.16.60.81 |
6380 |
/data/base/redis-6380 |
10.16.60.82 |
6379 |
/data/base/redis-6379 |
10.16.60.82 |
6380 |
/data/base/redis-6380 |
10.16.60.83 |
6379 |
/data/base/redis-6379 |
10.16.60.83 |
6380 |
/data/base/redis-6380 |
将编译好的redis文件目录修改目录名(为了方便,以端口号命名),并复制1份。这样可以在一台机器上启动2个节点。
mv redis-6.2.12 redis-6379
cp -r redis-6379/ redis-6380
分别修改这2个节点的配置
# 后台启动redis
daemonize yes
# 修改pid文件保存目录
pidfile /data/base/redis-6379/redis_6379.pid
port 6379
cluster-enabled yes
# 修改日志文件路径和名称
logfile "redis.log"
# 修改密码
requirepass 123456
# 允许远程登录
bind 0.0.0.0 -::1
# 后台启动redis
daemonize yes
# 修改pid文件保存目录
pidfile /data/base/redis-6380/redis_6380.pid
port 6380
cluster-enabled yes
# 修改日志文件路径和名称
logfile "redis.log"
# 修改密码
requirepass 123456
# 允许远程登录
bind 0.0.0.0 -::1
# 从节点要配置主节点的密码,否则主节点拒绝同步
masterauth 123456
将/data/base/redis-6379和/data/base/redis-6380 这2个目录拷贝到其他2台机器上。
在每台机器上分别进入到这2个节点的目录,启动这2个节点的redis-server。
./redis-server redis.conf
查看redis进程信息
[root@redis1 bin]# ps -ef|grep redis
root 126449 1 0 11:15 ? 00:00:00 ./redis-server 0.0.0.0:6379 [cluster]
root 127165 1 0 11:16 ? 00:00:00 ./redis-server 0.0.0.0:6380 [cluster]
root 127733 1566 0 11:16 pts/0 00:00:00 grep --color=auto redis
在10.16.60.81机器的/data/base/redis-6379目录下执行
./redis-cli -a 123456 --cluster create --cluster-replicas 1 10.16.60.81:6379 10.16.60.81:6380 10.16.60.82:6379 10.16.60.82:6380 10.16.60.83:6379 10.16.60.83:6380
返回如下:
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.16.60.82:6380 to 10.16.60.81:6379
Adding replica 10.16.60.83:6380 to 10.16.60.82:6379
Adding replica 10.16.60.81:6380 to 10.16.60.83:6379
M: b61869f33aaf9459a6e4fb13580fb6beacc1711b 10.16.60.81:6379
slots:[0-5460] (5461 slots) master
S: 7d394aaf601421a6ea1de3827d67d546eda91bcb 10.16.60.81:6380
replicates de47b58dbc62e6136aca7df33cd8bce17a91a412
M: 21ebb5e282e6727ffc8f5bd546d404406da467f4 10.16.60.82:6379
slots:[5461-10922] (5462 slots) master
S: 350656e9823e2d84d0e763faca90e7e499d04fce 10.16.60.82:6380
replicates b61869f33aaf9459a6e4fb13580fb6beacc1711b
M: de47b58dbc62e6136aca7df33cd8bce17a91a412 10.16.60.83:6379
slots:[10923-16383] (5461 slots) master
S: 69ecc2dd284e397968a029bc89b97260951450fe 10.16.60.83:6380
replicates 21ebb5e282e6727ffc8f5bd546d404406da467f4
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 10.16.60.81:6379)
M: b61869f33aaf9459a6e4fb13580fb6beacc1711b 10.16.60.81:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 350656e9823e2d84d0e763faca90e7e499d04fce 10.16.60.82:6380
slots: (0 slots) slave
replicates b61869f33aaf9459a6e4fb13580fb6beacc1711b
M: 21ebb5e282e6727ffc8f5bd546d404406da467f4 10.16.60.82:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: de47b58dbc62e6136aca7df33cd8bce17a91a412 10.16.60.83:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 69ecc2dd284e397968a029bc89b97260951450fe 10.16.60.83:6380
slots: (0 slots) slave
replicates 21ebb5e282e6727ffc8f5bd546d404406da467f4
S: 7d394aaf601421a6ea1de3827d67d546eda91bcb 10.16.60.81:6380
slots: (0 slots) slave
replicates de47b58dbc62e6136aca7df33cd8bce17a91a412
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis1 bin]# ./redis-cli -h 10.16.60.81
10.16.60.81:6379> auth 123456
OK
10.16.60.81:6379> 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:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:389
cluster_stats_messages_pong_sent:422
cluster_stats_messages_sent:811
cluster_stats_messages_ping_received:417
cluster_stats_messages_pong_received:389
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:811
在10.16.60.81上执行
[root@redis1 bin]# ./redis-cli -c -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set user user_1
-> Redirected to slot [5474] located at 10.16.60.82:6379
OK
在10.16.60.82上执行
[root@redis2 bin]# ./redis-cli -c -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> keys *
1) "user"
127.0.0.1:6379> get user
"user_1"
spring:
redis:
database: 0 # redis集群只能配置0
#host: 10.16.60.81
password: 123456
cluster:
nodes: 10.16.60.81:6379,10.16.60.81:6380,10.16.60.82:6379,10.16.60.82:6380,10.16.60.83:6379,10.16.60.83:6380
#max-redirects: 3
timeout: 60000 #连接超时时间(毫秒)
# Lettuce配置
lettuce:
# Lettuce连接池配置
pool:
# 最大激活连接数
max-active: 200
# 在连接池耗尽时,在抛出异常之前连接分配应阻塞的最长时间
max-wait: 20000
# 最小空闲连接数
min-idle: 10
原因是该节点中默认生成的配置或历史存储数据不一致导致的,清除对应节点的dump.rdb、nodes.conf文件,重启之后即可。
原因可能是防火墙没有关闭
systemctl stop firewalld
systemctl disable firewalld
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> select 1
(error) ERR SELECT is not allowed in cluster mode
在redis集群模式下,默认且只能使用0号database库,不允许使用SELECT 操作选择database 。