Redis安装

CentOS7.6安装

  • 下载、解压

wget http://download.redis.io/releases/redis-5.0.3.tar.gz
解压 tar -zxvf redis-5.0.3.tar.gz

  • 编译

sudo yum install gcc gcc-c++
cd redis-5.0.3/ && make
make install (可选步骤,编译后可执行文件在src中,redis无外部依赖,可进入src直接运行。make install是将可执行文件复制到/usr/local/bin中,执行redis不需输入全路径,也可将 /src路径设置到PATH中,效果相同)
make test,测试编译是否正确。

  • 若提示 You need tcl 8.5 or newer in order to run the Redis test,安装tcl后再重试:sudo yum install tcl
  • 提示正确
    图片.png

    我们常用的两个可执行文件是 redis-server 和 redis-cli

启动Redis

  • 直接启动:
  • 运行redis-server即可正常启动,默认端口是6379,可使用 redis-server --port 6380指定端口启动


    图片.png
  • 脚本启动

在redis源码目录中 utils文件夹中有redis_init_script,内容如下:

#!/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="/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的运行方式、持久化文件、日志文件和存储位置等

  • 将初始化脚本 复制到 /etc/init.d/redis_6379,可自定义端口,保证文件名和内容中REDISPORT 参数一致
  • 新建文件夹/etc/redis 放配置、/var/redis/6379 放持久化文件、/var/log/redis 放日志
  • 复制源代码中的redis.conf 到 /etc/redis/6379.conf,修改配置内容:

daemonize yes
pidfile /var/run/redis_6379.conf
port 6379
dir /var/redis/6379
logfile "/var/log/redis/6379.log"

配置好后可使用命令开启服务:

/etc/init.d/redis_6379 start

停止Redis

考虑到redis可能正在将数据同步到硬盘,强行停止Redis进程可能会导致数据丢失,正确停止Redis的方式是向Redis发送SHUTDOWN命令,方法为:

redis-cli SHUTDOWN  [save|nosave]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容