Written By: Xinyao Tian
概述本文档主要描述了 Redis 的生产环境安装及配置方法。
主要步骤 编译及安装 进入 root 用户并上传 Redis 源码安装包查看 Redis 源码安装包的上传情况:
[root@centos-host redis]# pwd/opt/redis[root@ centos-host redis]# ls -l | grep tar-rw-r--r-- 1 root root 3384618 Oct 26 11:24 redis-7.2.2.tar.gz 安装编译器由于我们选择从源码安装 Redis 故需要编译器的配合:
sudo yum install gcc-c++ # 使用sudo yum install gcc-c++时会自动安装/升级gcc及其他依赖的包 解压并运行编译解压 tar 文件并进入解压后的目录
[root@centos-host redis-7.2.2]# pwd/opt/redis/redis-7.2.2[root@centos-host redis-7.2.2]# ls 00-RELEASENOTES CONTRIBUTING.md INSTALL README.md runtest-cluster SECURITY.md testsBUGS COPYING Makefile redis.conf runtest-moduleapi sentinel.conf TLS.mdCODE_OF_CONDUCT.md deps MANIFESTO runtest runtest-sentinel src utils在该路径内,使用 make MALLOC=libc 和 make install 命令从源代码编译并安装。
make MALLOC=libcmake install直接使用 make 命令执行编译会遭遇报错,故需要使用如下命令进行编译。
其原因请见 该博客
确认安装情况待安装完毕后,检视默认安装路径 /usr/local/bin 可以发现已经存在 Redis 相关的命令。
[root@centos-host redis-7.2.2]# ls -l /usr/local/bin | grep redis-rwxr-xr-x 1 root root 1073312 Oct 26 13:38 redis-benchmarklrwxrwxrwx 1 root root 12 Oct 26 13:38 redis-check-aof -> redis-serverlrwxrwxrwx 1 root root 12 Oct 26 13:38 redis-check-rdb -> redis-server-rwxr-xr-x 1 root root 1790760 Oct 26 13:38 redis-clilrwxrwxrwx 1 root root 12 Oct 26 13:38 redis-sentinel -> redis-server-rwxr-xr-x 1 root root 9437552 Oct 26 13:38 redis-server 创建路径及修改配置该部分详见 Redis 官方安装文档
确定 Redis 的监听端口监听端口在后续配置中十分重要,故需要在配置其他事项前先行确定。
此处,我们使用 Redis 的默认端口 6379
创建 Redis 相关路径使用如下命令创建 Redis 相关的配置文件目录与数据目录:
mkdir /data/redis/etc/redismkdir /data/redis/var/redismkdir /data/redis/var/runmkdir /data/redis/var/logmkdir /data/redis/var/6379touch /data/redis/var/log/redis_6379.log 复制 Redis 启动文件至 /etc/init.d 并重命名在 Redis 成功安装完毕后,其安装路径中会出现 util/ 目录。复制其中的 redis_init_script 文件至 /etc/init.d 并重命名。 如下所示:
[root@centos-host redis-7.2.2]# pwd/opt/redis/redis-7.2.2[root@centos-host redis-7.2.2]# sudo cp utils/redis_init_script /etc/init.d/redis_6379修改启动文件内的部分配置项。主要修改其中的 REDISPORT, PIDFILE 和 CONF 配置项内容。
修改完毕后的配置文件内容如下所示:
#!/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 INFOREDISPORT=6379EXEC=/usr/local/bin/redis-serverCLIEXEC=/usr/local/bin/redis-cli# PIDFILE=/var/run/redis_${REDISPORT}.pidPIDFILE=/data/redis/var/run/redis_${REDISPORT}.pid# CONF="/etc/redis/${REDISPORT}.conf"CONF="/data/redis/etc/redis/${REDISPORT}.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONF 复制 Redis 启动文件至相应目录并启动再次进入 Redis 安装目录,并复制配置文件至相应路径:
[root@centos-host redis-7.2.2]# pwd/opt/redis/redis-7.2.2[root@centos-host redis-7.2.2]# sudo cp redis.conf /data/redis/etc/redis/6379.conf修改配置文件中的配置项 vim /data/redis/etc/redis/6379.conf:
# ...# 取消 IP 限制 bind * -::*# 以守护进程的方式启动 Redisdaemonize yes# 服务端口port 6379# PIDFILE 存储位置,用于记录 Daemon 进程号pidfile /data/redis/var/run/redis_6379.pid# 日志文件位置logfile "/data/redis/var/log/redis_6379.log"# 日志文件级别loglevel notice# Redis 运行时的文件存放位置# dir ./dir /data/redis/var/redis/6379# ... 以进程的方式启动 Redis使用如下命令启动 Redis: sudo /etc/init.d/redis_6379 start
[root@centos-host redis]# sudo /etc/init.d/redis_6379 startStarting Redis server...查看进程的运行情况:
[root@centos-host redis]# ps -ef | grep redisroot 170247 162267 0 13:45 pts/0 00:00:00 su - redisuserredisus+ 170248 170247 0 13:45 pts/0 00:00:00 -bashroot 171354 171179 0 13:49 pts/1 00:00:00 su - redisuserredisus+ 171355 171354 0 13:49 pts/1 00:00:00 -bashroot 198536 1 0 15:31 ? 00:00:00 /usr/local/bin/redis-server *:6379root 199166 174495 0 15:33 pts/0 00:00:00 grep --color=auto redis 检测安装情况使用 redis-cli ping 命令查看 Redis 服务是否已经被拉起:
[redisuser@centos-host ~]$ redis-cli pingPONG使用 redis-cli 命令执行一次 save
[redisuser@centos-host ~]$ redis-cli saveOK查看数据文件是否有 dump 文件被创建:
[root@centos-host redis]# ls -l /data/redis/var/redis/6379/dump.rdb -rw-r--r-- 1 root root 88 Oct 26 15:34 /data/redis/var/redis/6379/dump.rdb查看日志文件是否被正确创建:
[redisuser@centos-host ~]$ ls -l /data/redis/var/log/redis_6379.log-rw-r--r-- 1 root root 3782 Oct 26 15:34 /data/redis/var/log/redis_6379.log 创建 Redis 相关 Linux 用户 创建用户创建 uid 为 5052 的 redisuser 用户并设置其用户密码为 123456
useradd -u 5002 redisuserpasswd redisuser赋予新创建的 redisuser 用户 sudo 权限 vim /etc/sudoers
# ...## Allows people in group wheel to run all commands%wheel ALL=(ALL) ALLredisuser ALL=(ALL) ALL# ... 更改路径权限更改 Redis 相关的路径权限:
[root@centos-host redis]# chown -R redisuser:redisuser /data/redis/ 创建命令别名使用 redisuser 用户编辑其配置文件 vim ~/.bash_profile
export REDIS_ETC_DIR=/data/redis/etc/export REDIS_VAR_DIR=/data/redis/var/alias redis-start="/etc/init.d/redis_6379 start"alias redis-stop="/etc/init.d/redis_6379 stop"加载配置文件 source ~/.bash_profile
ln -s /data/redis/etc/ ~/redis-etcln -s /data/redis/var/ ~/redis-var 附加:给 Redis 默认用户 default 添加密码修改配置文件中的配置项 vim /data/redis/etc/redis/6379.conf:
# 添加密码requirepass da28as07 References Redis - Install Redis more properlyCSDN - 异常解决: configure: error: no acceptable C compiler found in $PATHRedis - Install Redis from SourceCSDN - 编译redis的时候出现zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory问题的解决办法