手动安装redis-3.2.8的详细步骤

CentOS6.7使用yum安装有时候没有比较新的版本,所以手动安装,下面记录一下步骤。

redis

下载最新版本

以3.2.8为例,附上地址:redis-3.2.8.tar.gz

解压,编译,安装redis

  • 解压:tar -zxvf redis-3.2.8.tar.gz
  • 进入目录:cd redis-3.2.8
  • 编译:make && make install
  • 创建相关目录:

    1
    2
    3
    4
    mkdir -p /opt/redis-3.2.8/bin
    mkdir -p /opt/redis-3.2.8/log
    mkdir -p /opt/redis-3.2.8/pid
    mkdir -p /opt/redis-3.2.8/db
  • 将编译后的可执行文件复制到自己的安装目录:ln -s /usr/local/bin/redis-* /opt/redis-3.2.8/bin

  • 复制配置文件到安装目录:cp redis.conf /opt/redis-3.2.8/

配置redis

  • 编辑redis.conf:cd /opt/redis-3.2.8vi redis.conf
    • redis默认只允许本机连接,所以注释掉这行配置就可以远程访问:\# bind 127.0.0.1
    • redis3.0版本增加了保护模式,需要我们设置密码,如果不想设置密码,就关闭保护模式:protected-mode no
    • 设置redis以守护线程方式启动:daemonize yes
    • 配置pid,log,db文件的保存地址:
      1
      2
      3
      pidfile /opt/redis-3.2.8/pid/redis.pid
      logfile /opt/redis-3.2.8/log/redis.log
      dir /opt/redis-3.2.8/db

其他配置就默认即可,有需要再自行修改

  • 编写redis启动脚本:vi /etc/init.d/redis

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    #!/bin/sh
    #
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.
    PATH=/opt/redis-3.2.8/bin:/sbin:/usr/bin:/bin
    REDISPORT=6379
    EXEC=/opt/redis-3.2.8/bin/redis-server
    CLIEXEC=/opt/redis-3.2.8/bin/redis-cli
    PIDFILE=/opt/redis-3.2.8/pid/redis.pid
    CONF="/opt/redis-3.2.8/redis.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
  • 设置服务权限:chmod a+x /etc/init.d/redis

相关使用

  • 启动:service redis start
  • 关闭:service redis stop
  • 查看:ps -ef | grep redisnetstat -anptu | grep 6379