redis

Redis 4.0 源码编译安装

编译环境:CentOS 7.7

Redis 版本:4.0.9

http://download.redis.io/releases/ 可查看当前 Redis 发布的所有版本的源码包。

1.下载源码编译

$sudo wget http://download.redis.io/releases/redis-4.0.9.tar.gz
$sudo tar -xzvf redis-4.0.9.tar.gz
$cd redis-4.0.9/
$sudo make PREFIX=/usr/local/redis

2.编译过程中遇到的问题

2.1缺少 GCC 环境

cd hiredis && make static

make[3]: Entering directory `/root/redis-4.0.9/deps/hiredis’

gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c

make[3]: gcc: Command not found

make[3]: *** [net.o] Error 127

make[3]: Leaving directory `/root/redis-4.0.9/deps/hiredis’

make[2]: *** [hiredis] Error 2

make[2]: Leaving directory `/root/redis-4.0.9/deps’

make[1]: [persist-settings] Error 2 (ignored)

   CC adlist.o

/bin/sh: cc: command not found

make[1]: *** [adlist.o] Error 127

make[1]: Leaving directory `/root/redis-4.0.9/src’

make: *** [all] Error 2

安装 gcc完成后继续编译:

$sudo yum -y install gcc
$sudo make PREFIX=/usr/local/redis

2.2缺少 jemalloc 环境

In file included from adlist.c:34:0:

zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory

#include <jemalloc/jemalloc.h>

                              ^

compilation terminated.

make[1]: *** [adlist.o] Error 1

make[1]: Leaving directory `/root/redis-4.0.9/src’

make: *** [all] Error 2

在源码解压缩后的文件夹中,README.md 文件提到关于 Redis 内存分配的问题:

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

大概意思是,Redis 在编译时选择内存分配器默认使用 MALLOC 变量,但是系统有没有关于 jemalloc 的类库,于是编译时就报错了。jemalloc 相比 libc 会有更少的碎片化问题,但是最后我还是直接使用了系统已有的 libc,编译安装到指定的文件夹/usr/local/redis4.0

$sudo make PREFIX=/usr/local/redis4.0 MALLOC=libc install

此时 Redis 已完成编译并且安装了。

3.配置Redis

在源码解压缩文件夹中有一个默认的配置,复制该文件到我们安装 Redis 的文件夹中:

$sudo cp redis.conf /usr/local/redis4.0/bin/

修改该配置文件,我们着重调整默认的三个参数:

# 绑定Redis访问的IP地址,不建议使用0.0.0.0而是内网IP
bind 192.168.1.106
# Redis以进程的方式运行,这样关闭终端后也能正常运行
daemonize yes
# 设置Redis的访问密码
requirepass foobared

4.启动Redis

$sudo /usr/local/redis4.0/bin/redis-server /usr/local/redis4.0/bin/redis.conf
86874:C 19 Jul 23:27:19.030 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
86874:C 19 Jul 23:27:19.030 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=86874, just started
86874:C 19 Jul 23:27:19.030 # Configuration loaded

看到以上提示表示 Redis 已正常启动,我们可以通过 redis-cli 尝试访问:

/usr/local/redis4.0/bin/redis-cli -h 192.168.1.106
192.168.1.106:6379>auth foobared
192.168.1.106:6379>keys *

输入密码后即可执行 Redis 相关命令。

发表评论