环境
centos7 redis5.0.14
一、下载 redis-5.0.14.tar.gz ----
解压gz包 可以变更文件名称 mv redis-5.0.14 redis
tar -xvf redis-5.0.14.tar.gz
进入redis
执行make 命令
二、可能问题
如果报错提示gcc不存在 删除当前redis 目录重新解压
然后执行
yum install gcc-c++
继续执行make 命令会提示使用 make test命令
make test 命令如果报错 tcl 错误 提示You need tcl 8.5 or newer in order to run the Redis test
那么需要下载安装tcl
下载 tcl8611-src.zip
https://jaist.dl.sourceforge.net/project/tcl/Tcl/8.6.11/tcl8611-src.zip
在服务器上unzip 解压文件
进入/home/soft/tcl8.6.11/unix 目录
分别执行如下命令
./configure
make //时间稍长
make install
三、开始安装
执行命令
make test //时间比较长 如下
[ok]: XSETID cannot SETID on non-existent key [43/50 done]: integration/replication-4 (145 seconds) [ok]: Empty stream can be rewrite into AOF correctly [ok]: Stream can be rewrite into AOF correctly after XDEL lastid [44/50 done]: unit/type/stream (182 seconds) [ok]: PFCOUNT multiple-keys merge returns cardinality of union #1 [ok]: GEOADD + GEORANGE randomized test [45/50 done]: unit/geo (102 seconds) [ok]: PFCOUNT multiple-keys merge returns cardinality of union #2 [ok]: Client output buffer hard limit is enforced [ok]: PFDEBUG GETREG returns the HyperLogLog raw registers [ok]: PFADD / PFCOUNT cache invalidation works ......... All tests passed without errors! Cleanup: may take some time... OK make[1]: g&;e<
继续执行
make install //会安装到当前目录
Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: g&;e<
make PREFIX=/opt/redis install PREFIX 可以指定redis安装目录,之后方便删除 生成目录/opt/redis/bin
四、启动redis
./bin/redis-server& ./redis.conf 后台启动方式 也可以在配置文件中添加 daemonize yes 配置让程序后台启动
五、redis—cluster 集群配置
redis.config配置
#绑定服务器IP地址 bind 127.0.0.1 #绑定端口号,必须修改,以此来区分Redis实例 port 7001 #后台运行 daemonize yes #修改pid进程文件名,以端口号命名 pidfile /opt/redis/redis-7001.pid #修改日志文件名称,以端口号为目录来区分 logfile /opt/redis/7001/redis.log #修改数据文件存放地址,以端口号为目录名来区分 dir /opt/redis/7001/ #启用集群 cluster-enabled yes #配置每个节点的配置文件,同样以端口号为名称 cluster-config-file nodes-7001.conf #配置集群节点的超时时间,可改可不改 cluster-node-timeout 15000 #启动AOF增量持久化策略 appendonly yes #发生改变就记录日志 appendfsync always
因为是自己用得,所以关闭防火墙
systemctl status firewalld.service 查看防火墙状态 systemctl stop firewalld.service 关闭防火墙 systemctl disable firewalld.service 永久关闭防火墙
分别启动所有节点
redis-server /opt/redis/700x/redis.conf
分配插槽
redis-cli --cluster create 192.168.0.91:7001 192.168.0.91:7002 192.168.0.92:7001 192.168.0.92:7002 192.168.0.93:7001 192.168.0.93:7002 --cluster-replicas 1
replicas 1 代表一主一从 三组分配
问题一、这里可能会出现Could not connect to Redis at 192.168.0.91:7001: Connection refused 因为我们绑定的是127.0.0.1 需要添加本地ip绑定 bind 192.168.0.91 每个配置文件都需要进行对应的修改。 问题二、Waiting for the cluster to join .................. 1.防火墙问题 700x 1700x 两个端口 2.原来ip绑定的问题。全改成本机ip就可以了,记得清理下原来的数据 登录到每个客户端,执行 flushall、 cluster reset,重启实例之前你要删除以下文件: rm -rf nodes.conf // cluster-config-file rm -rf dump.rdb // dbfilename rm -rf appendonly.aof // appendfilename
成功后
[OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
六、测试
redis-cli -h 192.168.0.91 -p 7001 -c 使用cluster方式连接到服务 -c 192.168.0.91:7001> cluster nodes 查看节点 73ae7e4681a4086ada97a83c00a7f683d6a56045 192.168.0.91:7002@17002 slave 12c361604b2e0f2d0d3548dfb4ffab406e60d31b 0 1678423224227 5 connected 02bb3e64798fa4cbc0759c06e77bfbc469272ae0 192.168.0.92:7001@17001 master - 0 1678423226262 3 connected 5461-10922 242de7ae5aa9d0041485de03d409c508867280a4 192.168.0.93:7002@17002 slave 02bb3e64798fa4cbc0759c06e77bfbc469272ae0 0 1678423225000 6 connected 12c361604b2e0f2d0d3548dfb4ffab406e60d31b 192.168.0.93:7001@17001 master - 0 1678423224000 5 connected 10923-16383 fc807d27fb622f9e7b62f6b6e0db26b72ff3d247 192.168.0.92:7002@17002 slave ad3eb3c5635f54ca130f789a87fcc4d052eef7e1 0 1678423225245 4 connected ad3eb3c5635f54ca130f789a87fcc4d052eef7e1 192.168.0.91:7001@17001 myself,master - 0 1678423225000 1 connected 0-5460
192.168.0.91:7001> set name zhang -> Redirected to slot [5798] located at 192.168.0.92:7001 //可以看到,数据存储到了 92服务的7001服务上 OK 192.168.0.92:7001> get name "zhang"
乐享:知识积累,快乐无限。