1. 下载安装包
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
2. 编译安装
# 指定内存库为libc,或者使用jmelloc
make -j4 MALLOC=libc
# 安装时指定目标文件目录
make PREFIX=/data/base/redis-6.2.12 install
3. 新建配置文件redis.conf并启动redis-server
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
4.启动redis-server并查看进程状态
./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
5.redis-cli客户端使用
登录redis
[root@redis1 bin]# ./redis-cli -h localhost -p 6379
localhost:6379> auth 123456
OK
6.redis 集群搭建(cluster模式)
6.1集群规划
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 |
6.2 修改redis配置
将编译好的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
6.3 分发redis目录 ,启动并查看redis-server信息
将/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
6.4创建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:6379slots:[0-5460] (5461 slots) master
S: 7d394aaf601421a6ea1de3827d67d546eda91bcb 10.16.60.81:6380replicates de47b58dbc62e6136aca7df33cd8bce17a91a412
M: 21ebb5e282e6727ffc8f5bd546d404406da467f4 10.16.60.82:6379slots:[5461-10922] (5462 slots) master
S: 350656e9823e2d84d0e763faca90e7e499d04fce 10.16.60.82:6380replicates b61869f33aaf9459a6e4fb13580fb6beacc1711b
M: de47b58dbc62e6136aca7df33cd8bce17a91a412 10.16.60.83:6379slots:[10923-16383] (5461 slots) master
S: 69ecc2dd284e397968a029bc89b97260951450fe 10.16.60.83:6380replicates 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:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 350656e9823e2d84d0e763faca90e7e499d04fce 10.16.60.82:6380slots: (0 slots) slavereplicates b61869f33aaf9459a6e4fb13580fb6beacc1711b
M: 21ebb5e282e6727ffc8f5bd546d404406da467f4 10.16.60.82:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
M: de47b58dbc62e6136aca7df33cd8bce17a91a412 10.16.60.83:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
S: 69ecc2dd284e397968a029bc89b97260951450fe 10.16.60.83:6380slots: (0 slots) slavereplicates 21ebb5e282e6727ffc8f5bd546d404406da467f4
S: 7d394aaf601421a6ea1de3827d67d546eda91bcb 10.16.60.81:6380slots: (0 slots) slavereplicates de47b58dbc62e6136aca7df33cd8bce17a91a412
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
6.5 redis-cli 连接集群查看集群信息
[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
6.6 验证数据
在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"
6.7 springboot配置redis集群信息
spring:redis:database: 0 # redis集群只能配置0#host: 10.16.60.81password: 123456cluster: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: 3timeout: 60000 #连接超时时间(毫秒)# Lettuce配置lettuce:# Lettuce连接池配置pool:# 最大激活连接数max-active: 200# 在连接池耗尽时,在抛出异常之前连接分配应阻塞的最长时间max-wait: 20000# 最小空闲连接数min-idle: 10
7.问题汇总
7.1 Node 10.16.60.82:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
原因是该节点中默认生成的配置或历史存储数据不一致导致的,清除对应节点的dump.rdb、nodes.conf文件,重启之后即可。
7.2 redis 10.16.60.82:6379 Could not connect to Redis No route to host
原因可能是防火墙没有关闭
systemctl stop firewalld
systemctl disable firewalld
7.3 ERR SELECT is not allowed in cluster mode
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 。