Redis 扩展模块 -- RedisJSON 的安装方法

摘要

RedisJSON 简介

  • RedisJSON 是 Redis 官方维护的一个扩展模块,隶属于 Redis Stack,专门用于对JSON数据进行操作。

  • 该模块以 Redis Module 方式加载,可无缝集成到现有 Redis 实例中。

  • Redis8+,RedisJSON 已经内置在 Redis 中,可以在安装redis同时安装全部 Stack 模块。

安装 RedisJSON

  • 虽然Redis CloudDownload Center中提供了所有Redis模块编译后的.so文件,但是并不保证一定兼容,所以最稳妥的方式是通过源码自己编译。

安装时需要科学上网,主要是安装依赖时需要从海外网下载,如果要部署在国内服务器,可能会连接失败。
可以在海外的相同配置的服务器上进行编译,之后将编译好的rejson.so上传到国内服务器即可。

  • 安装依赖

1
2
3
4
5
6
7
8
9
10
sudo dnf install -y \
gcc \
gcc-c++ \
make \
cmake \
autoconf \
automake \
libtool \
pkgconfig \
openssl-devel
  • 编译RedisJSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mkdir -p /usr/local/soft/modules/
cd /usr/local/soft/modules
# clone 代码,这里 --recursive 是为了拉取子模块
git clone --recursive https://github.com/RedisJSON/RedisJSON.git
cd RedisJSON
# 推荐切换到稳定的release版本
git checkout v2.8.16
# 更新子模块,非必须,如果上面 clone 时没有加上 --recursive ,这个步骤就不能省略
git submodule update --init --recursive

# 检查并安装需要的依赖
./sbin/setup
## 输出
# readies version: 7fc8e62
dnf install -q -y ca-certificates
dnf install -q -y wget unzip
/usr/local/soft/modules/RedisJSON/deps/readies/bin/enable-utf8
dnf install -q -y git unzip rsync
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getclang --modern
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getrust
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getcmake --usr
dnf install -q -y which
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getgcc --modern
dir=$(mktemp -d /tmp/tar.XXXXXX); (cd $dir; wget --no-verbose -O tar.tgz http://redismodules.s3.amazonaws.com/readies/gnu/gnu-tar-1.32-x64-centos7.tgz; tar -xzf tar.tgz -C /; ); rm -rf $dir
dnf install -q -y lcov
/usr/bin/python3 /usr/local/soft/modules/RedisJSON/deps/readies/bin/getrmpytools --reinstall --modern
/usr/bin/python3 -m pip install --disable-pip-version-check --user -r /usr/local/soft/modules/RedisJSON/tests/pytest/requirements.txt
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getaws
NO_PY2=1 /usr/local/soft/modules/RedisJSON/deps/readies/bin/getpudb

# RedisJSON 的编译依赖 Rust 工具链,所以编译前需要安装rust,若已经安装则忽略
# 安装 rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 按提示选择默认安装(通常选择 1)

# 让 Rust 生效
source $HOME/.cargo/env
# 检查 rust 版本
rustc --version # rustc 1.92.0 (ded5c06cf 2025-12-08)
cargo --version # cargo 1.92.0 (344c4567c 2025-10-21)


# 编译 RedisJSON
make
# 编译过程未报错说明编译成功,编译后的文件位于 `bin/linux-x64-release/rejson.so`

./sbin/setup 报错

  • 本人使用的是 Amazon Linux 2023(内核 6.1),即 EL9,类似于CentOS 9,第一次运行会报错,大致报错信息如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
./sbin/setup
## 错误信息
……
[FAILED] raven-release.el9.noarch.rpm: Status code: 403 for https://dyn.su/el9/base/x86_64/raven-release.el9.noarch.rpm (IP: 104.21.57.14)
Status code: 403 for https://dyn.su/el9/base/x86_64/raven-release.el9.noarch.rpm (IP: 104.21.57.14)

In /usr/local/soft/modules/RedisJSON/deps/readies/bin/getepel:
346 # xinstall --allowerasing https://dl.fedoraproject.org/pub/epel/epel-release-latest-${EPEL}.noarch.rpm
347 fi
348
349 >>> install_raven
350 install_remi
351 # install_centos_stream_repos
352

command failed: /usr/local/soft/modules/RedisJSON/deps/readies/bin/getepel
command failed: /usr/local/soft/modules/RedisJSON/deps/readies/bin/getclang --modern

In /usr/local/soft/modules/RedisJSON/sbin/setup:
18 python3 -m pip list
19 fi
20
21 >>> $ROOT/sbin/system-setup.py
22 if [[ $VERBOSE == 1 ]]; then
23 python3 -m pip list
24 fi
  • 错误分析与解决方法:

    • 这里实际上是两个错误,第一个错误与安装 RedisBloom 时一样,禁用掉 install_raven 即可,具体参见 Redis 扩展模块 -- RedisBloom 的安装方法 进行修改。

    • 第二个错误还是操作系统的事,在 deps/readies/bin/getclang中有一个方法,其实一看就明白了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    def redhat_compat(self):
    if not self.modern:
    return
    self.run("%s/bin/getepel" % READIES, sudo=True)
    if self.dist in ['centos', 'ol'] and self.os_version[0] >= 8:
    self.install("clang")
    self.install("llvm-toolset")
    else:
    self.install("llvm-toolset-7.0")
    self.cp_to_profile_d("/opt/rh/llvm-toolset-7.0/enable", "llvm-toolset-7.0.sh")

    我使用的机器不是 centos,所以就走else的逻辑了,另外这里即便走了 centos 逻辑,也有问题,就是el9中已经没有 llvm-toolset 了,取而代之的是 llvm,所以需要修改该方法

    • 修改方法如下:
    1
    2
    3
    4
    5
    6
    7
    def redhat_compat(self):
    if not self.modern:
    return
    self.run("%s/bin/getepel" % READIES, sudo=True)
    # 去掉判断,直接安装,前提是 el9,当然这里也可以什么都不写,而是通过命令行安装:dnf install -y clang llvm llvm-devel
    self.install("clang")
    self.install("llvm")

Redis 启用模块

  • 将生成的 rejson.so 拷贝到 redis 的 modules 目录下(非必须),目录不存在则创建

1
2
# 注意 .so 文件需要包含可执行权限
cp bin/linux-x64-release/rejson.so /usr/local/soft/redis-7.4.7/modules/rejson.so
  • 本文采用 loadmodule 加载模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 将 rejson.so 添加到 redis.conf 中,需要重启 redis
loadmodule /usr/local/soft/redis-7.4.7/modules/rejson.so

# 启动redis
redis-server redis.conf

# 登录测试
redis-cli --user admin --pass 123456
# 查看模块
127.0.0.1:6379> info Modules
## 输出
# Modules
module:name=ReJSON,ver=20816,api=1,filters=0,usedby=[],using=[],options=[handle-io-errors]
module:name=bf,ver=20817,api=1,filters=0,usedby=[],using=[],options=[handle-io-errors]

127.0.0.1:6379> MODULE LIST
# 输出
1) 1) "name"
2) "ReJSON"
3) "ver"
4) (integer) 20816
5) "path"
6) "/usr/local/soft/redis-7.4.7/modules/rejson.so"
7) "args"
8) (empty array)
2) 1) "name"
2) "bf"
3) "ver"
4) (integer) 20817
5) "path"
6) "/usr/local/soft/redis-7.4.7/modules/redisbloom.so"
7) "args"
8) (empty array)