CentOS Linux release 7.8.2003 (Core)
Redis6.0.1
[root@localhost home]# ls
redis-6.0.1.tar.gz
[root@localhost home]# tar -zxvf redis-6.0.1.tar.gz
[root@localhost home]# cd redis-6.0.1
[root@localhost redis-6.0.1]# make
执行到make时,报错
……
MAKE hiredis
cd hiredis && make static
make[3]: 进入目录“/home/redis-6.0.1/deps/hiredis”
cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb net.c
make[3]: cc:命令未找到
make[3]: *** [net.o] 错误 127
make[3]: 离开目录“/home/redis-6.0.1/deps/hiredis”
make[2]: *** [hiredis] 错误 2
make[2]: 离开目录“/home/redis-6.0.1/deps”
make[1]: [persist-settings] 错误 2 (忽略)
CC adlist.o
/bin/sh: cc: 未找到命令
make[1]: *** [adlist.o] 错误 127
make[1]: 离开目录“/home/redis-6.0.1/src”
make: *** [all] 错误 2
解决办法:安装gcc
yum -y install gcc
再次执行make,又报另一个错
In file included from adlist.c:34:0:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
#include
^
编译中断。
make[1]: *** [adlist.o] 错误 1
make[1]: 离开目录“/home/redis-6.0.1/src”
make: *** [all] 错误 2
解决办法:
[root@localhost redis-6.0.1]# make MALLOC=libc
再次报错:
……
server.c:5117:39: 错误:‘struct redisServer’没有名为‘maxmemory’的成员
if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
^
server.c:5118:176: 错误:‘struct redisServer’没有名为‘maxmemory’的成员
serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);
^
server.c: 在函数‘hasActiveChildProcess’中:
server.c:1476:1: 警告:在有返回值的函数中,控制流程到达函数尾 [-Wreturn-type]
}
^
server.c: 在函数‘allPersistenceDisabled’中:
server.c:1482:1: 警告:在有返回值的函数中,控制流程到达函数尾 [-Wreturn-type]
}
^
server.c: 在函数‘writeCommandsDeniedByDiskError’中:
server.c:3747:1: 警告:在有返回值的函数中,控制流程到达函数尾 [-Wreturn-type]
}
^
server.c: 在函数‘iAmMaster’中:
server.c:4914:1: 警告:在有返回值的函数中,控制流程到达函数尾 [-Wreturn-type]
}
^
make[1]: *** [server.o] 错误 1
make[1]: 离开目录“/home/redis-6.0.1/src”
make: *** [all] 错误 2
解决办法:
# 查看gcc版本是否在5.3以上,centos7.8默认安装4.8.5
[root@localhost redis-6.0.1]# gcc -v
# 升级gcc到5.3及以上,如下
[root@localhost redis-6.0.1]# yum -y install centos-release-scl
[root@localhost redis-6.0.1]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@localhost redis-6.0.1]# scl enable devtoolset-9 bash
# 需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
# 如果要使gcc 9.3的永久生效的话,要将参数写到配置文件里,如下:
[root@localhost redis-6.0.1]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
# 修改完成后,退出shell重新打开就是新版的gcc
[root@localhost ~]# gcc -v
gcc version 9.1.1 20190605 (Red Hat 9.1.1-2) (GCC)
这样环境就没问题了,再接着进行Redis安装
#安装到/home/redis/目录下
[root@localhost redis-6.0.1]# make PREFIX=/home/redis install
# 出现以下提示表示安装成功
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/home/redis-6.0.1/src”
安装成功后,在目标文件夹/home/redis下会出现一个bin文件夹;
新建config用来存储redis配置文件。
[root@localhost redis]# cd /home/redis
[root@localhost redis]# mkdir config
将安装包内的Redis配置文件拷贝到刚刚新建的config文件夹下
[root@localhost redis-6.0.1]# cp redis.conf /home/redis/config/
Redis集群最少3个主节点,同时每个主节点有一个从节点(三主三从),且主从节点最好不要在同一台物理机上(为实现高可用);
此处的6个节点在同一台虚拟机,但是通过6个不同的端口来区分;
Redis集群最小化配置如下:
bind 192.168.3.33
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
#后台运行
daemonize yes
Redis集群各节点公用一套可执行文件,只是配置文件不同,将配置文件复制6份,每个配置文件中的端口不同即可,最终结果:
[root@localhost config]# ls
redis-7000.conf redis-7001.conf redis-7002.conf redis-7003.conf redis-7004.conf redis-7005.conf
[root@localhost config]# cat redis-7001.conf
bind 192.168.3.33
port 7001
cluster-enabled yes
cluster-config-file redis-7001.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
分别启动6个节点:
[root@localhost bin]# cd /home/redis/bin/
[root@localhost bin]# ./redis-server ../config/redis-7000.conf
[root@localhost bin]# ./redis-server ../config/redis-7001.conf
[root@localhost bin]# ./redis-server ../config/redis-7002.conf
[root@localhost bin]# ./redis-server ../config/redis-7003.conf
[root@localhost bin]# ./redis-server ../config/redis-7004.conf
[root@localhost bin]# ./redis-server ../config/redis-7005.conf
#检查一下是否全部正常启动
[root@localhost bin]# ps -ef|grep redis
root 36105 1 0 16:36 ? 00:00:01 ./redis-server 192.168.3.33:7000 [cluster]
root 36167 1 0 16:40 ? 00:00:00 ./redis-server 192.168.3.33:7001 [cluster]
root 36180 1 0 16:40 ? 00:00:00 ./redis-server 192.168.3.33:7002 [cluster]
root 36185 1 0 16:40 ? 00:00:00 ./redis-server 192.168.3.33:7003 [cluster]
root 36190 1 0 16:40 ? 00:00:00 ./redis-server 192.168.3.33:7004 [cluster]
root 36195 1 0 16:40 ? 00:00:00 ./redis-server 192.168.3.33:7005 [cluster]
root 36200 3374 0 16:40 pts/0 00:00:00 grep --color=auto redis
以上6个节点已经启动,但是互相之间并不知道其他节点的存在。现在需要创建集群,注意:集群命令中的前三个会分配成主节点,后三个被分配成从节点,且主从对应关系为1-4,2-5,3-6:
[root@localhost bin]# ./redis-cli --cluster create 192.168.3.33:7000 192.168.3.33:7001 192.168.3.33:7002 192.168.3.33:7003 192.168.3.33:7004 192.168.3.33:7005 --cluster-replicas 1
#执行上一条命令后,集群过程中还需要手动输入一个yes
Can I set the above configuration? (type 'yes' to accept): yes
#出现以下信息即代表集群成功
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
检查集群是否正常,通过redis-cli登录一个节点即可查看:
[root@localhost bin]# ./redis-cli -h 192.168.3.33 -p 7000
192.168.3.33:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
……
192.168.3.33:7000> cluster nodes
e888f195ec036b2aa0251eac722d0a06b74ae39f 192.168.3.33:7001@17001 master - 0 1590137270688 2 connected 5461-10922
cf6eee469583f1d4484fd52a4db6d3be97e85363 192.168.3.33:7002@17002 master - 0 1590137270279 3 connected 10923-16383
dc54e1628131777afd724633f446360cdbaa30d5 192.168.3.33:7003@17003 slave 6929b9eabea205eb1ea87aceaea6943f0cc7b0e5 0 1590137270000 4 connected
aaf914fb4160854d1b6b11aa01eebda872e128c8 192.168.3.33:7004@17004 slave e888f195ec036b2aa0251eac722d0a06b74ae39f 0 1590137271292 5 connected
c14a61b78d4b8e290214e8a8100e3d9bd3af0ef4 192.168.3.33:7005@17005 slave cf6eee469583f1d4484fd52a4db6d3be97e85363 0 1590137271000 6 connected
6929b9eabea205eb1ea87aceaea6943f0cc7b0e5 192.168.3.33:7000@17000 myself,master - 0 1590137270000 1 connected 0-5460
192.168.3.33:7000> exit
从以上信息中可以看出集群状态为“ok”,且7000,7001,7002为master节点,根据其ID也能找到对应的从节点。