1.首先就是下载redis-5.0.3.tar.gz
下载地址:http://download.redis.io/releases/redis-5.0.3.tar.gz
官网地址:https://redis.io/
(1)执行:cd /usr/local
(2)执行:wget http://download.redis.io/releases/redis-5.0.3.tar.gz
等待下载完成后,目录 /usr/local 下会有redis-5.0.3.tar.gz文件。
2.解压缩
(1)执行:tar -zxvf redis-5.0.3.tar.gz
或者执行(指定解压目录):tar -zxvf redis-5.0.3.tar.gz -C /usr/local/
执行解压完成后,会出现文件夹 /usr/local/redis-5.0.3/
(2)执行:cd /usr/local/redis-5.0.3
到目录下。
a.没有安装gcc就需要用命令yum install gcc安装一下。查看gcc版本的命令:gcc -v
注意:gcc的版本过低,再执行make时,会出现 error: unrecognized command line option “-flto=auto” 的失败信息。
卸载gcc命令:yum -y remove gcc g++
安装高版本的gcc:
下载地址:http://ftp.gnu.org/gnu/gcc
wget http://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz
解压:tar -xvf gcc-13.2.0.tar.gz
编译和安装:
cd gcc-13.2.0
./contrib/download_prerequisites #下载依赖库
mkdir gcc-temp #建立编译输出目录
./configure --enable-checking=release --enable-languages=c,c++ --disable-multilib #生成makefile文件
make -j4 #编译(j为核心数,速度会更快,这步极为耗时,建议睡前操作)
sudo make install #安装
b.执行:make
(注:如果执行失败看一下错误,如果错误如下,
编译中断。
make[1]: *** [adlist.o] 错误 1
make[1]: 离开目录“/usr/local/redis-stable/src”
make: *** [all] 错误 2
解决办法是:
执行:make MALLOC=libc
如出现下面报错:
which: no python3 in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
cc: ../deps/hdr_histogram/libhdrhistogram.a: No such file or directory
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/usr/local/redis-7.0.7/src'
make: *** [all] Error 2
解决方法:分别进入redis下的deps下的hiredis、lua 运行make
运行完后,再回到redis目录。继续执行:make MALLOC=libc
如果不行,就需要安装python3
)
出现 下面内容,表示make成功:
c.编译完成后执行:make install
作用是将 /usr/local/redis/redis-5.0.3/src/相关redis的执行命令复制到/usr/local/bin目录下
3.配置文件
(1)执行 vi /usr/local/redis/redis-5.0.3/redis.conf
修改:
bind 127.0.0.1 改为 bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-7001.conf
requirepass redis主节点链接的密码,不配置的话,redis是没有密码的
masterauth redis从节点链接的密码,不配置的话,redis是没有密码的
备注:
bind 0.0.0.0 表示不限制链接本机服务的ip地址。任何计算机都可以发起连接。这里要特别注意,我一开始以为这里绑定的ip才能访问本机的6379端口,
结果一修改就启动不了redis服务,后来仔细看了一下配置文件上的说明才明白,原来这个bind的不是来访问的ip,而是本机网卡的物理地址。
daemonize yes 表示允许后台运行
cluster-enabled yes表示开启集群
cluster-node-timeout 15000表示设置超时时间
cluster-config-file node保存集群配置文件
(2)集群配置
执行:cp redis.conf 7001.conf
cp redis.conf 7002.conf
cp redis.conf 7003.conf
cp redis.conf 7004.conf
cp redis.conf 7005.conf
cp redis.conf 7006.conf
修改上述各个conf中的cluster-config-file的值。
修改上述各个conf中的port(监听端口)的值,默认为6379
在Linux服务器下可执行命令:sed -i 's/6379/7001/g' 7001.conf
其中7001为端口号
(3)配置好之后启动redis服务,执行:redis-server 7001.conf
如果出现下图警告信息:
请将“vm.overcommit_memory=1”添加到/etc/sysctl.conf,然后重新启动或运行命令:sysctl vm.overcommit_memory=1
以使其生效。
关闭执行:
redis-cli -h 服务器内网IP -p 监听端口 shutdown
(4)集群
执行:src/redis-cli --cluster create ip:7001 ip:7002 ip:7003 ip:7004 ip:7005 ip:7006 --cluster-replicas 1
如过出现警告信息,
DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user.如下图:
则需要编辑配置文件,执行:vi 7001.conf
然后在里面把 protected-mode yes 改为 protected-mode no
注: (protected-mode:no 表示关闭保护模式,可以外部访问。)
最后,重启redis-sever,执行命令如下:
redis-cli -p 7001 shutdown
redis-server 7001.conf
4.注意事项
Redis的端口分为两种类型,一种是服务端口,例如6379;一种是通讯端口,这个端口随机。所以如出现链接不上的情况,请检查端口是否开放。