redis安装、设置密码及开机自启动

时间:2022-03-29 15:39:14

一、redis安装

1、tar xzf redis-2.8.3.tar.gz
2、cd redis-2.8.3
3、make
4、编译完成后,在Src目录下,有四个可执行文件redis-server、redis-benchmark、redis-cli和redis.conf。然后拷贝到一个目录下。
mkdir /usr/redis
cp redis-server  /usr/redis
cp redis-benchmark /usr/redis
cp redis-cli  /usr/redis
cp redis.conf  /usr/redis
cd /usr/redis


5、启动Redis服务
./redis-server redis.conf 


以用shutdown来停止Redis实例,具体命令为src/redis-cli shutdown。


二、开机自启动

1、设置Redis.conf中daemonize为yes,确保守护进程开启;
     找到#requirepass foorbared这一行,直接替换掉这行,改为requirepass 新密码

2、编写开机自启动脚本

vi /etc/init.d/redis

脚本内容如下:

#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis

PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/alidata/soft/redis/redis-server
REDIS_CLI=/alidata/soft/redis/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/alidata/soft/redis/redis.conf"
AUTH="1234"

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
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
sleep 2
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac

3、写完后保存退出VI


4、设置权限

chmod 755 redis

5、启动测试

/etc/init.d/redis start

启动成功会提示如下信息:

Starting Redis server...
Redis is running...

6、设置开机自启动

chkconfig redis on

7、关机重启测试

reboot