Linux 安装 Redis

以 Redis 4.0.10 为例
Redis 官方下载地址

下载、解压、编译

下载

wget http://download.redis.io/releases/redis-4.0.10.tar.gz

解压

tar xzf redis-4.0.10.tar.gz

编译

cd redis-4.0.10
make

启动 Redis

前台启动

src/redis-server

测试 Redis 是否启动

src/redis-cli

如果输出以下内容,表示 Redis 已启动

127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
“bar”

后台启动
修改 Redis 配置文件 redis.conf

vi redis.conf

1
2
3
***
daemonize yes
***

开启外网访问

确保 redis.conf 中 daemonize 为 yes,确保守护进程开启,Redis 可以在后台可以运行

把 Redis 端口放到防火墙计划中

/sbin/iptables -I INPUT -p tcp –dport 6379 -j ACCEPT

修改 Redis 配置文件 redis.conf

vi redis.conf

1
2
3
4
5
***
# bind 127.0.0.1
***
protected-mode no
***

重启 Redis,并指定配置文件

src/redis-cli shutdown
src/redis-server ./redis.conf

设置 Redis 开机启动

在 /etc 下新建 redis 文件夹

mkdir /etc/redis

把安装 Redis 根目录里面的 redis.conf 复制到 /etc/redis/6379.conf

cp redis.conf /etc/redis/6379.conf

复制 Redis 启动脚本
Redis 启动脚本一般在 Redis 根目录的 utils 目录下,如果不知道路径,可以先查看路径

find / -name redis_init_script

复制启动脚本到 /etc/init.d/redis 文件中

cp utils/redis_init_script /etc/init.d/redis

修改启动脚本参数

vi /etc/init.d/redis

指定 Redis 安装路径
EXEC=…
CLIEXEC=…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/redis-4.0.10/src/redis-server
CLIEXEC=/usr/local/redis-4.0.10/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

***

关闭 Redis

service redis stop

设为开机启动

chkconfig redis on

设为开机关闭

chkconfig redis off

启动 Redis

service redis start

检查自启动项列表中没有 redis 这个

chkconfig –list redis

错误处理

如果执行 make 报错,可能缺少某些依赖包

错误1

1
2
3
4
5
6
7
8
9
10
cd src && make all
make[1]: 进入目录“/usr/local/redis-4.0.10/src”
CC Makefile.dep
make[1]: 离开目录“/usr/local/redis-4.0.10/src”
make[1]: 进入目录“/usr/local/redis-4.0.10/src”
CC adlist.o
/bin/sh: cc: 未找到命令
make[1]: *** [adlist.o] 错误 127
make[1]: 离开目录“/usr/local/redis-4.0.10/src”
make: *** [all] 错误 2

以上报错缺少 GCC,则如下命令安装:

yum install gcc

错误2

1
2
3
4
5
6
7
8
9
10
11
cd src && make all
make[1]: 进入目录“/usr/local/redis-4.0.10/src”
CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
#include <jemalloc/jemalloc.h>
^
编译中断。
make[1]: *** [adlist.o] 错误 1
make[1]: 离开目录“/usr/local/redis-4.0.10/src”
make: *** [all] 错误 2

以上报错则如下命令解决:

make MALLOC=libc

If these articles are helpful to you, you can donate comment here.