一、初始化环境
- 创建redis运行时的用户和组
$ groupadd redis
$ useradd -g redis redis -s /sbin/nologin -M
- 初始化数据目录
$ rm -rf /data/redis
$ mkdir -pv /data/redis
$ chown -R redis:redis /data/redis
- 初始化日志目录
$ mkdir -pv /data/logs/redis
$ chown -R redis:redis /data/logs/redis
- 初始化redis家目录
$ mkdir -pv /usr/local/redis/{bin,etc}
二、安装
- 下载redis源码包
$ cd ~
$ wget http://10.0.16.133/src/x86_64/redis/redis-6.0.8.tar.gz
- 安装编译时依赖
$ yum install -y gcc clang make
- 编译
$ tar -zxvf redis-6.0.8.tar.gz
$ cd redis-6.0.8
$ CC=clang make
- 安装
$ install -p -D -m 0755 src/redis-benchmark /usr/local/redis/bin/
$ install -p -D -m 0755 src/redis-check-aof /usr/local/redis/bin/
$ install -p -D -m 0755 src/redis-check-rdb /usr/local/redis/bin/
$ install -p -D -m 0755 src/redis-cli /usr/local/redis/bin/
$ install -p -D -m 0755 src/redis-sentinel /usr/local/redis/bin/
$ install -p -D -m 0755 src/redis-server /usr/local/redis/bin/
$ install -p -D -o redis redis.conf /usr/local/redis/etc/
$ ln -svf /usr/local/redis/bin/redis-cli /usr/bin/redis-cli
三、配置
- 生成redis访问密码
$ export REDIS_PASS=$(head -c 1000 /dev/urandom | tr -dc a-zA-Z0-9 | head -c 16)
- 修改配置
$ export REDIS_HOME=/usr/local/redis
$ sed -i s@'^# requirepass .*'@"requirepass $REDIS_PASS"@ $REDIS_HOME/etc/redis.conf
$ sed -i s@'^logfile .*'@"logfile /data/logs/redis/redis.log"@ $REDIS_HOME/etc/redis.conf
$ sed -i s@'^dir .*'@"dir /data/redis"@ $REDIS_HOME/etc/redis.conf
$ sed -i s@'^pidfile .*'@"pidfile /data/redis/redis_6379.pid"@ $REDIS_HOME/etc/redis.conf
$ sed -i s@'^bind .*'@"bind 0.0.0.0"@ $REDIS_HOME/etc/redis.conf
四、创建redis-shutdown脚本
- 创建文件
/usr/local/redis/bin/redis-shutdown
,内容如下
#!/bin/bash
#
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/redis/bin/redis-cli
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=redis
fi
CONFIG_FILE="/etc/$SERVICE_NAME.conf"
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`
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
PORT=${PORT:-6379}
else
PORT=${PORT:-26739}
fi
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
if [ -e "$SOCK" ] ; then
$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
- 为脚本添加执行权限
$ chmod +x /usr/local/redis/bin/redis-shutdown
五、系统服务
- 创建系统服务文件
$ cat >/usr/lib/systemd/system/redis.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 /usr/local/redis/etc/redis.conf --supervised systemd
ExecStop=/usr/local/redis/bin/redis-shutdown
Type=simple
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=10240
[Install]
WantedBy=multi-user.target
EOF
- 重载systemd
$ systemctl daemon-reload
- 启动redis
$ systemctl start redis
- 添加到开机自启动
$ systemctl enable redis
六、验证
$ redis-cli -a $REDIS_PASS
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info
# Server
redis_version:6.0.8
redis_git_sha1:00000000
redis_git_dirty:0
....
标签:bin,教程,local,redis,usr,install,安装,REDIS
From: https://blog.51cto.com/u_64214/9361764