[File Comments] “Redis 部署”
URL
- https://redis.io/download
编译安装所需的依赖环境
// CentOS 7
yum install -y gcc
如果需要编译测试(make test),还需要安装tcl
yum install -y tcl
部署 - CentOS 7
- 下载并解压资源包
# 下载
wget https://download.redis.io/releases/redis-5.0.12.tar.gz
# 解压
tar xvf redis-5.0.12.tar.gz
- 编译并测试
执行这一步时,请先查看"编译选项"部分。
# 进入资源目录
cd redis-5.0.12
# 编译
make MALLOC=libc
# 构建测试
make test
- 安装
要将 Redis 二进制文件安装到 /usr/local/bin 中,只需使用:
make install
如果您希望使用其他路径,则可以使用make PREFIX=/some/other/directory install
。
make install
只会在系统中安装二进制文件,而不会在适当的位置配置初始化脚本和配置文件。
- 使用 utils 脚本配置初始化脚本和配置文件
cd utils
./install_server.sh
注意:install_server.sh
在 Mac OSX 上不起作用,它仅适用于 Linux。
该脚本将询问您几个问题,并将正确运行 Redis 所需的一切设置为后台守护程序,该守护程序将在系统重新引导时再次启动。
-
不使用 utils 脚本配置初始化脚本和配置文件
-
创建相关目录
# 创建配置文件目录 mkdir -p /etc/redis # 创建日志目录 mkdir -p /var/log/redis # 创建数据目录 mkdir -p /var/lib/redis/6379
-
配置 redis 配置文件
# 创建配置文件 cp /path/redis/redis.conf /etc/redis/6379.conf # 修改配置文件,根据需求自行设置 sed -i "s/^port .\+/port 6379/g" /etc/redis/6379.conf sed -i "s/^logfile .\+/logfile \"\/var\/log\/redis\/redis_6379.log\"/g" /etc/redis/6379.conf sed -i "s/^dir .\+/dir \/var\/lib\/redis\/6379/g" /etc/redis/6379.conf sed -i "s/^pidfile .\+/pidfile \/var\/run\/redis_6379.pid/g" /etc/redis/6379.conf sed -i "s/^daemonize no/daemonize yes/g" /etc/redis/6379.conf
-
配置 redis 服务脚本
# 创建服务脚本 cp /path/redis/utils/redis_init_script /etc/init.d/redis_6379 # 修改服务脚本,根据实际环境自行设置 sed -i "s/^REDISPORT=.*/REDISPORT=6379/g" /etc/init.d/redis_6379 sed -i "s/^EXEC=.*/EXEC=\/usr\/local\/bin\/redis-server/g" /etc/init.d/redis_6379 sed -i "s/^CLIEXEC=.*/CLIEXEC=\/usr\/local\/bin\/redis-cli/g" /etc/init.d/redis_6379 sed -i "s/^PIDFILE=.*/PIDFILE=\/var\/run\/redis_6379.pid/g" /etc/init.d/redis_6379 sed -i "s/^CONF=.*/CONF=\"\/etc\/redis\/6379.conf\"/g" /etc/init.d/redis_6379 # 参考此服务模版,配置status/reload命令 cat /path/redis/utils/redis_init_script.tpl # 修改权限 chmod +x /etc/init.d/redis_6379
-
配置并启动 redis 服务
# 加入 chkconfig 系统服务,并设置开机自启 chkconfig --add /etc/init.d/redis_6379 chkconfig --level 345 redis_6379 on # 启动服务 /etc/init.d/redis_6379 start # 使用 systemd 启动服务,也可以自行配置redis.service systemctl daemon-reload systemctl start redis_6379
-
编译选项
- 常见的编译选项
-
要使用 TLS 支持进行构建,您将需要
OpenSSL
开发库(例如 Debian/Ubuntu 上的libssl-dev
)并运行:
make BUILD_TLS=yes
-
要使用 systemd 支持进行构建,您将需要
systemd
开发库(例如 Debian/Ubuntu 上的libsystemd-dev
或CentOS 上的systemd-devel
)并运行:
make USE_SYSTEMD=yes
-
要在 Redis 程序名称后添加后缀,请使用:
make PROG_SUFFIX="-alt"
- 编译时运行测试注意事项
如果已构建 TLS,请在启用 TLS 的情况下运行测试(你将需要安装tcl-tls
):
./utils/gen-test-certs.sh
./runtest --tls
- 修复依赖项或缓存的构建选项的构建问题
Redis 具有一些依赖关系,这些依赖关系包含在 deps 目录中。即使依赖项的源代码中的某些内容发生了更改,make 也不会自动重建依赖项。
当您使用git pull
来更新源代码或以其他任何方式修改依赖关系树中的代码时,请确保使用以下命令来真正清理所有内容并从头开始重建:
make distclean
这将清除:jemalloc,lua,hiredis,linenoise。
同样,如果您强制使用某些构建选项,例如32bit目标,没有C编译器优化(出于调试目的)以及其他类似的构建时间选项,则这些选项将无限期缓存,直到发出make distclean
命令为止。
- 分配器(Allocator)
通过设置 MALLOC 环境变量,可以在构建 Redis 时选择非默认内存分配器。默认情况下,Redis 是针对 libc malloc 编译和链接的,但 jemalloc 是 Linux 系统上的默认值。选择该默认值是因为事实证明,与 libc malloc 相比,jemalloc 的碎片问题更少。
要强制针对 libc malloc 进行编译,请使用:
make MALLOC=libc
要在 Mac OSX 系统上针对 jemalloc 进行编译,请使用:
make MALLOC=jemalloc
- 单调时钟(Monotonic clock)
默认情况下,Redis 将使用 POSIX clock_gettime 函数作为单调时钟源进行构建。在大多数现代系统上,可以使用内部处理器时钟来提高性能。
要构建对处理器内部指令时钟的支持,请使用:
make CFLAGS="-DUSE_PROCESSOR_CLOCK"
常见问题
- gcc/cc:命令未找到
solution:
yum -y install gcc
- 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
solution:
make MALLOC=libc
- You need tcl 8.5 or newer in order to run the Redis test
solution:
yum -y install tcl
- cc: 错误:…/deps/hiredis/libhiredis.a:No such file or directory
cc: 错误:…/deps/lua/src/liblua.a:No such file or directory
cc: 错误:…/deps/linenoise/linenoise.o:No such file or directory
solution:
cd deps
make hiredis lua linenoise
脚本执行记录
[xxx 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
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/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!