1. 版本选择
对于 Redis 安装的两个要求:
- 去官网下载:https://redis.io
- 下载 6-12 月的版本
2.x 非常老
3.x 主流 redis-cluster
4.x 混合持久化
5.x 最新稳定版本 新增加了流处理类型
Redis 使用标准版标记进行版本控制:major.minor.patchlevel。偶数的版本号表示稳定的版本,例如 1.2、2.0、2.2、2.4、2.6、2.8,奇数的版本号用来表示非标准版本,例如 2.9.x 是非稳定版本,它的稳定版本是 3.0
2. 目录规划
/usr/local/src 软件下载目录
/usr/loca/redis 命令安装目录
/opt/redis/6379/{etc,logs,pid} 安装目录,日志目录,pid目录,配置目录
/data/redis/6379/ 数据目录
3. 命令安装
cd /usr/loca/src/
wget https://download.redis.io/releases/redis-5.0.7.tar.gz
# 解压软件包
tar xvf redis-5.0.7.tar.gz
cd redis-5.0.7
# 编译安装
make PREFIX=/usr/local/redis install
# 设置环境变量
export PATH="/usr/local/redis/bin:$PATH"
echo 'export PATH="/usr/local/redis/bin:$PATH"' >> /etc/profile
4. 配置文件
# 创建 Redis 实例目录
mkdir -p /data/redis/6379 /opt/redis/6379/{etc,pid,logs}
# 编写配置文件
cat > /opt/redis/6379/etc/6379.conf << EOF
daemonize yes
bind 127.0.0.1 $(ip a ls eth0|awk -F '[ /]+' 'NR==3{print $3}')
port 6379
pidfile /opt/redis/6379/pid/6379.pid
logfile /opt/redis/6379/logs/6379.log
EOF
5. 启动命令
redis-server /opt/redis/6379/conf/6379.conf
6. 检查服务
ps aux|grep redis
netstat -nlutp|grep 6379
7. 终端连接
~]# redis-cli
127.0.0.1:6379>
8. 关闭命令
# 方式一:
~]# redis-cli
127.0.0.1:6379> SHUTDOWN
not connected> exit
# 方式二:
~]# redis-cli SHUTDOWN
# 方式三:
kill
pkill
9. system启动
# 创建普通用户 redis
groupadd -g 2003 redis
useradd -u 2003 -g redis -s /sbin/nologin -M -c "Redis Service User" redis
# 目录授权
chown -R redis. /opt/redis/
chown -R redis. /data/redis/
# 创建redis停止脚本
cat > /usr/local/redis/bin/redis-shutdown << 'EOF'
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/redis/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=redis
else
REDIS_DIR="/opt/redis/${SERVICE_NAME}"
fi
# Get the proper config file based on service name
CONFIG_FILE="${REDIS_DIR}/etc/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
PORT=${PORT:-6379}
else
PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
EOF
chmod +x /usr/local/redis/bin/redis-shutdown
# 创建service文件
cat > /usr/lib/systemd/system/redis-6379.service << EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /opt/redis/6379/etc/6379.conf --supervised systemd
ExecStop=/usr/local/redis/bin/redis-shutdown 6379
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start redis-6379.service