当前最新release版本:redis-5.0.2.tar.gz
- 下载安装包
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
- 解压
tar xzf redis-5.0.2.tar.gz
- 编译
cd redis-5.0.2
make
make install
- 定制配置文件
cp /usr/local/redis-5.0.2/redis.conf /etc/redis/6379.conf
vim /etc/redis/6379.conf
# 使Redis以守护进程模式运行
daemonize yes
# 设置Redis的PID文件位置
pidfile /var/run/redis_6379.pid
# 设置Redis监听的端口号
port 6379
# 设置持久化文件存放位置
dir /var/redis/6379/
# 绑定地址 (远程登陆时,需要注释掉)
# bind 127.0.0.1
# ...
- 初始化脚本
cp /usr/local/redis-5.0.2/utils/redis_init_script /etc/init.d/redis_6379
- 启动和停止Redis (跳过)
/etc/init.d/redis_6379 start
/etc/init.d/redis_6379 stop
# 考虑到 Redis 有可能正在将内存中的数据同步到硬盘中,强行终止 Redis 进程可能会导致数据丢失。
# 正确停止Redis的方式应该是向Redis发送SHUTDOWN命令,方法为:
redis-cli SHUTDOWN
- 设置开机启动
# 初始化脚本文件头部第四行的位置,增加下面两行
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
vim /etc/init.d/redis_6379
# 编辑后如下:
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# 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/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/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脚本
chkconfig /etc/init.d/redis_6379 on
# 启动和停止Redis
service redis_6379 start
service redis_6379 stop
- 配置密码后,上面命令会提示" (error) NOAUTH Authentication required."
# 修改/etc/init.d/redis_6379 文件见下:
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# 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
##### Redis连接密码 ########
REDISPASSWORD=password123
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/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
################### 通过密码连接 ##################
$CLIEXEC -a $REDISPASSWORD -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
网友评论