Linux下安装Redis

编译安装工作:

#下载redis安装包
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
#解压安装包
tar -zxvf redis-4.0.10.tar.gz -C /usr/local/
#重命名为redis
mv /usr/local/redis-4.0.10 /usr/local/redis
#进入安装目录
cd /usr/local/redis
#编译并安装
make && make install

修改redis.conf配置:

#进入配置文件所在目录
cd /usr/local/redis
#开启守护进程模式(即允许后台运行)
sed -i 's/daemonize\ no/daemonize\ yes/g' redis.conf
#关闭protected-mode模式,使外部网络可以直接访问(生产慎用sed -i 's/protected-mode\ yes/protected-mode\ no/g' redis.conf
#设置redis连接密码(生产建议使用密码)
sed -i 's/#\ requirepass\ foobared/requirepass 123456/g' redis.conf
#对过期事件进行通知发送(可用于token过期提醒)
sed -i 's/notify-keyspace-events\ ""/notify-keyspace-events\ Ex/g' redis.conf

编写Redis启动脚本:

#创建redis脚本
vi /etc/init.d/redisd
#向脚本中写入以下内容:
#!/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/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis.conf"

# 启动服务
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 -a 123456 shutdown 2>/dev/null
        while [ -x /proc/${PID} ]
        do
            echo "Waiting for Redis to shutdown ..."
            sleep 1
        done
        echo "Redis stopped"
    fi
}
# 启动状态
status(){
    if [ -f $PIDFILE ]
    then
        PID=$(cat $PIDFILE)
        echo "$PIDFILE is running,pid=${PID}"
    else
        echo "$PIDFILE is not running"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        sleep 1s
        start
        ;;
    *)
        echo "Please use start|stop|restart|status as first argument"
        ;;
esac

启动Redis服务(需要赋权限chmod 755 /etc/init.d/redisd):

#设置开机自启动 chkconfig redisd on
#查看开机自启动 chkconfig --list | grep redisd #启动redis服务 service redisd start
#停止redis服务 service redisd stop
#重启redis服务 service redisd restart
#查看redis服务 service redisd status

注:要使远程能够登录访问,还需开启防火墙和配置安全组规则(阿里云服务器),可参考https://www.cnblogs.com/54hsh/p/13355413.html

远程登录验证结果(需要配置protected-mode no):

posted @ 2020-09-17 22:12  聚散彡流沙  阅读(111)  评论(0编辑  收藏  举报