CentOS安装Redis-5.0.14

 

注:以下所有操作均在CentOS 7.9 x86_64位系统下完成。

 

#准备工作#

在安装Redis之前,请确保已经使用yum安装了以下基础组件:

  • make
  • gcc
  • gcc-c++
  • kernel-devel

 #Redis的安装#

开始下载Redis并进行编译安装:

wget http://download.redis.io/releases/redis-5.0.14.tar.gz
tar -xzf redis-5.0.14.tar.gz
cd redis-5.0.14
make PREFIX=/usr/local/redis-5.0.14 install

mkdir /usr/local/redis/etc/
cp redis.conf /usr/local/redis/etc/

mkdir -p /data/redis/log
ln -s /usr/local/redis-5.0.14 /usr/local/redis

把Redis的sbin目录加入PATH:

# vim /etc/profile

export PATH=$PATH:/usr/local/redis/bin

# source /etc/profile

至此,Redis已经安装完毕。

 #Redis的启动/重启/关闭#

先简单修改下配置文件:

# vim /usr/local/redis-5.0.14/etc/redis.conf

daemonize yes

pidfile /var/run/redis.pid

logfile /data/redis/log/redis_6379.log

requirepass yourpassword

bind 0.0.0.0

为了以后管理上的方便, 我们这里写个启动脚本,以后就可以用service命令来启动,如下:

# vim /etc/init.d/redis

#!/bin/bash
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
   
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/etc/redis.conf"
AUTH="yourpassword"
   
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 does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -p $REDISPORT -a $AUTH SHUTDOWN
                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

设置脚本自启动:

# chmod +x /etc/init.d/redis
# chkconfig redis on

这样子就可以通过service来启动/关闭:

# service redis start
# service redis stop

 

posted @ 2023-06-29 16:13  brishenzhou  阅读(376)  评论(0编辑  收藏  举报