图怪兽_4b76540e9ec818573fb04ed70976e8b8_19511.png
upload successful

Centos7中安装Redis6.x

下载和安装

获取安装包

[root@localhost program]# wget https://download.redis.io/releases/redis-6.2.6.tar.gz
--2021-11-16 20:47:56-- https://download.redis.io/releases/redis-6.2.6.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2476542 (2.4M) [application/octet-stream]
Saving to: ‘redis-6.2.6.tar.gz’

100%[================================================================>] 2,476,542 101KB/s in 31s

2021-11-16 20:48:28 (78.0 KB/s) - ‘redis-6.2.6.tar.gz’ saved [2476542/2476542]

解压

[root@localhost program]# tar -zxvf redis-6.2.6.tar.gz 

安装之前的准备

Redis依赖于一下第三方的库。需要提前安装,否则在redis安装过程中会报错。

首先,先升级gcc版本,Redis6安装需要的gcc版本大于5.3,否则也会报错。

# 升级到gcc 9.3:
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
# 需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
# 如果要长期使用gcc 9.3的话:
echo -e "\nsource /opt/rh/devtoolset-9/enable" >>/etc/profile

运行gcc --version查看升级后的版本如下。

[root@localhost redis-6.2.6]# gcc --version
gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

开始安装

进入到redis-6.2.6的解压目录

[root@localhost program]# ls
redis-6.2.6 redis-6.2.6.tar.gz
[root@localhost program]# cd redis-6.2.6/

指向make命令

如果出现下面这个错误,说明

[root@localhost redis-6.2.6]# make
cd src && make all
make[1]: Entering directory `/data/program/redis-6.2.6/src'
CC Makefile.dep
make[1]: Leaving directory `/data/program/redis-6.2.6/src'
make[1]: Entering directory `/data/program/redis-6.2.6/src'
CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:10: fatal error: jemalloc/jemalloc.h: No such file or directory
50 | #include <jemalloc/jemalloc.h>
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/data/program/redis-6.2.6/src'
make: *** [all] Error 2

针对这个错误,可以在Redis的REAdme.MD文档中看到相关说明。

---------

Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

% make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

% make MALLOC=jemalloc

Verbose build
-------------

针对这个说明,很多人会通过下面这个命令去尝试解决。

make MALLOC=libc

虽然能够解决,但是并不是正确的解决办法,正确方法如下:

分别执行下面两个命令,其中make distclean主要是清理上次编译残留文件重新编译。

错误的本质是我们在开始执行make 时遇到了错误(大部分是由于gcc未安装),然后我们安装好了gcc 后,我们再执行make ,这时就出现了jemalloc/jemalloc.h: No such file or directory。这是因为上次的

编译失败,有残留的文件,我们需要清理下,然后重新编译就可以了。

make distclean
make

执行完上述命令后(时间会比较久,大概会在1分钟到2分钟左右),看到如下输出

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/data/program/redis-6.2.6/src'

说明编译过程是没问题了,接下来执行make test进行测试

[root@localhost redis-6.2.6]# make test
cd src && make test
make[1]: Entering directory `/data/program/redis-6.2.6/src'
CC Makefile.dep
make[1]: Leaving directory `/data/program/redis-6.2.6/src'
make[1]: Entering directory `/data/program/redis-6.2.6/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/data/program/redis-6.2.6/src'
make: *** [test] Error 2

从测试结果来看,仍然有错误:You need tcl 8.5 or newer in order to run the Redis test

需要安装tcl,于是执行下面这个指令进行安装。

[root@localhost redis-6.2.6]# yum install tcl

tcl安装结束后,再执行make test(正常情况下,这个过程会持续2分钟左右)

运行结果如下,表示编译全部正常。

\o/ All tests passed without errors!

Cleanup: may take some time... OK
make[1]: Leaving directory `/data/program/redis-6.2.6/src'

通过makemake test编译后,开始执行安装。

执行make install安装,并使用PREFIX指定了安装目录。

[root@localhost redis-6.2.6]# make install PREFIX=/data/program/redis
cd src && make install
make[1]: Entering directory `/data/program/redis-6.2.6/src'

Hint: It's a good idea to run 'make test' ;)

INSTALL redis-server
INSTALL redis-benchmark
INSTALL redis-cli
make[1]: Leaving directory `/data/program/redis-6.2.6/src'


把源代码中的redis.conf拷贝到安装目录/data/program/redis/redis.conf中。

[root@localhost redis-6.2.6]# cp redis.conf ../redis/redis.conf

至此,安装过程就结束了!

验证安装结果

执行./redis-server ../redis.conf ,启动Redis Server

[root@localhost bin]# cd /data/program/redis/bin/
[root@localhost bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
[root@localhost bin]# ./redis-server ../redis.conf
10471:C 16 Nov 2021 21:13:21.319 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10471:C 16 Nov 2021 21:13:21.319 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=10471, just started
10471:C 16 Nov 2021 21:13:21.319 # Configuration loaded
10471:M 16 Nov 2021 21:13:21.320 * Increased maximum number of open files to 10032 (it was originally set to 1024).
10471:M 16 Nov 2021 21:13:21.320 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 10471
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

10471:M 16 Nov 2021 21:13:21.322 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
10471:M 16 Nov 2021 21:13:21.322 # Server initialized
10471:M 16 Nov 2021 21:13:21.322 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
10471:M 16 Nov 2021 21:13:21.323 * Ready to accept connections
^C10471:signal-handler (1637068403) Received SIGINT scheduling shutdown...
10471:M 16 Nov 2021 21:13:23.931 # User requested shutdown...
10471:M 16 Nov 2021 21:13:23.931 * Saving the final RDB snapshot before exiting.
10471:M 16 Nov 2021 21:13:23.937 * DB saved on disk

修改/data/program/redis/redis.conf文件中下面两个配置。

# 开启后台守护进程方式运行
daemonize yes
# 关闭保护模式
protected-mode no

添加到系统服务

配置环境变量。

# [root@localhost bin]# vim /etc/profile

export REDIS_HOME=/data/program/redis
export PATH=$PATH:$REDIS_HOME

执行Redis系统服务安装脚本

#[root@localhost utils]# cd /data/program/redis-6.2.6/utils/

./install_server.sh

可能会得到如下错误

[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

解决办法, vim /install_server.sh,将下面代码注释.

#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
#unset _pid_1_exe

再次执行./install_server.sh

下面这个安装过程是交互是的,可以根据自己的实际情况制定相关配置。

[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server
# 默认使用的端口
Please select the redis port for this instance: [6379]
Selecting default: 6379
# 选择redis.conf文件的路径
Please select the redis config file name [/etc/redis/6379.conf] /data/program/redis/redis.conf
# 选择Redis日志存储路径及文件名称(如果指定路径不存在,则需要先创建)
Please select the redis log file name [/var/log/redis_6379.log] /data/program/redis/log/redis.log
# 选择Redis数据存储路径
Please select the data directory for this instance [/var/lib/redis/6379] /data/program/redis/data
# 选择Redis安装目录下的`redis-server`执行文件
Please select the redis executable path [] /data/program/redis/bin/redis-server
Selected config:
Port : 6379
Config file : /data/program/redis/redis.conf
Log file : /data/program/redis/log/redis.log
Data dir : /data/program/redis/data
Executable : /data/program/redis/bin/redis-server
Cli Executable : /data/program/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost utils]#

安装成功后,我们可以使用下面的命令来控制redis,其中redis_7379是上述执行过程中默认生成的服务名称。

# systemctl stop redis_6379
# systemctl status redis_6379
# systemctl start redis_6379

设置自动启动相关命令。

# systemctl enable redis_6379.service #设置开机自启动
# systemctl disable redis_6379.service #停止开机自启动
# systemctl restart redis_6379.service  #重新启动服务