1. 安装依赖
➜ yum install -y gcc gcc-c++ kernel-devel
2. 下载源码包
# 推荐在这个目录存放各个软件的源码
➜ cd /usr/local/src
# 下载指定版本
➜ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
# 下载最新稳定版
➜ wget http://download.redis.io/redis-stable.tar.gz
# 查看源码具体版本
➜ cat redis-stable/src/version.h
3. 编译安装
➜ tar zxvf redis-5.0.5.tar.gz
➜ cd redis-5.0.5
➜ make
# 安装到指定目录下
➜ make PREFIX=/usr/local/redis install
# 拷贝默认配置文件到指定目录
➜ mkdir /usr/local/redis/etc/
➜ cp redis.conf /usr/local/redis/etc/
# 拷贝可执行程序到系统目录下
➜ cd /usr/local/redis/bin/
➜ cp redis-benchmark redis-cli redis-server /usr/bin/
# 配置环境变量
➜ echo 'export PATH="$PATH:/usr/local/redis/bin"' >> /etc/profile
➜ source /etc/profile
# 验证
➜ redis-cli -v
redis-cli 5.0.5
4. 修改默认配置文件
这里只是一些推荐的常用基本配置,详细的参见配置文件中对各个配置项的说明,或者参照这里 https://www.runoob.com/redis/redis-conf.html
➜ vi /usr/local/redis/etc/redis_6379.conf
# 开启守护进程(后台)方式运行
daemonize yes
# 进程文件
pidfile /var/run/redis_6379.pid
# 只允许指定主机连接,默认所有网络
bind 127.0.0.1
# 修改端口号,默认 6379
port 6379
# 客户端闲置多长时间(单位:s)关闭连接
# 默认 0 ,无限制
timeout 300
# 日志级别,默认 notice
loglevel verbose
# 日志记录方式,默认为标准输出(stdout)
# 如果以守护进程运行,并且此处采用标准输出,则日志发送给 /dev/null
logfile stdout
# 设置连接密码
requirepass <密码>
5. 启动服务
基本启动方式
# 以默认配置启动
redis-server
# 指定配置文件
redis-server /usr/local/redis/etc/redis.conf
# 查看更多使用参数
redis-server -h
使用脚本启动
➜ cd redis-5.0.5/utils
➜ cp redis_init_script /etc/init.d/
➜ mv /etc/init.d/redis_init_script /etc/init.d/redis_6379
➜ vim /etc/init.d/redis_6379
#!/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/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis_${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
使用脚本启动服务测试
➜ ./redis_6379 start
Starting Redis server...
19261:C 08 Nov 2019 01:42:46.848 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
19261:C 08 Nov 2019 01:42:46.848 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=19261, just started
19261:C 08 Nov 2019 01:42:46.848 # Configuration loaded
# 检查是否启动成功
➜ ps -ef | grep redis
root 19262 1 0 01:42 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root 19267 19129 0 01:43 pts/0 00:00:00 grep --color=auto redis
# 客户端连接测试
➜ redis-cli
127.0.0.1:6379> KEYS *
(empty list or set)
127.0.0.1:6379> exit
# 停止服务
➜ ./redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[root@cnetos7-localhost init.d]#
6. 加入开机自启动
# 使用 root 用户操作
# 添加到自启动列表
# 这里的 redis_6379 与 /etc/init.d/redis_6379 文件名保持一致
➜ chkconfig --add redis_6379
# 将 2 3 4 5 级别设置为自启动
➜ chkconfig --level 2345 redis_6379 on
# 重启检查自启动是否生效
➜ reboot
也可使用如下 CentOS7+ 命令统一进行管理:
# 查看服务状态
➜ systemctl status redis_6379
● redis_6379.service - LSB: Redis data structure server
Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
Active: active (running) since Mon 2019-11-11 02:21:03 UTC; 3s ago
Docs: man:systemd-sysv-generator(8)
Process: 1042 ExecStop=/etc/rc.d/init.d/redis_6379 stop (code=exited, status=0/SUCCESS)
Process: 1056 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/redis_6379.service
└─1058 /usr/local/redis/bin/redis-server 127.0.0.1:6379
Nov 11 02:21:03 cnetos7-localhost systemd[1]: Starting LSB: Redis data structure server...
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: Starting Redis server...
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # oO0OoO0OoO0Oo Re...0Oo
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # Redis version=5....ted
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # Configuration loaded
Nov 11 02:21:03 cnetos7-localhost systemd[1]: Started LSB: Redis data structure server.
Hint: Some lines were ellipsized, use -l to show in full.
# 启动服务
➜ systemctl start redis_6379
# 关闭服务
➜ systemctl stop redis_6379
```</密码>