Linux安装Redis

葛大爷 互联网 2016-02-24 4591
  1. 安装redis
    [@zjm_46_98 ~]# mkdir /opt/java/redis
    [@zjm_46_98 ~]# cd /opt/java
    [@zjm_46_98 /opt/java]# wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
    [@zjm_46_98 /opt/java]# tar zxvf redis-2.6.14.tar.gz
    [@zjm_46_98 /opt/java]# cd redis-2.6.14
    [@zjm_46_98 /opt/java/redis-2.6.14]# make PREFIX=/opt/java/redis install
  2. 安装redis成功后,你可以在/opt/java/redis/bin的目录看到下面五个文件
    [@zjm_46_98 /opt/java/redis-2.6.14]# ls /opt/java/redis/bin
    redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server
  3. redis服务自动启动
    [@zjm_46_98 /opt/java/redis-2.6.14]# cd /etc/rc.d/init.d/
    [@zjm_46_98 /etc/rc.d/init.d]# cp /opt/java/redis-2.6.14/utils/redis_init_script /etc/rc.d/init.d/redis
    [@zjm_46_98 /etc/rc.d/init.d]# chkconfig --add redis
    service redis does not support chkconfig
    如果出现service redis does not support chkconfig问题,解决办法如下:
    其中
    # chkconfig: 2345 80 90 说明redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是80,关闭的优先级是90
    EXEC:redis服务地址
    CLIEXEC:redis客户端地址 
    $EXEC $CONF &  注意后面的那个“&”,即是将服务转到后面运行的意思,否则启动服务时,Redis服务将 占据在前台,占用了主用户界面,造成其它的命令执行不了。
  4. [@zjm_46_98 /etc/rc.d/init.d]# vim redis
    #!/bin/sh
    # chkconfig: 2345 80 90
    # description: Redis is a persistent key-value database
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.

    REDISPORT=6379
    EXEC=/opt/java/redis/bin/redis-server
    CLIEXEC=/opt/java/redis/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
  5. 将redis配置文件拷贝到/etc/redis/${REDISPORT}.conf
    [@zjm_46_98 /etc/rc.d/init.d]# mkdir /etc/redis
    [@zjm_46_98 /etc/rc.d/init.d]# cp /opt/java/redis-2.6.14/redis.conf /etc/redis/6379.conf
    这样,redis服务脚本指定的CONF就存在了。默认情况下,Redis未启用认证,可以通过开启6379.conf的requirepass 指定一个验证密码
  6. 启动服务并验证
    [@zjm_46_98 /etc/rc.d/init.d]# chkconfig --add redis
    [@zjm_46_98 /etc/rc.d/init.d]# service redis start
    Starting Redis server...
    [@zjm_46_98 /etc/rc.d/init.d]# vi /etc/profile
    export PATH=$PATH:/opt/java/redis/bin
    [@zjm_46_98 /etc/rc.d/init.d]# source /etc/profile
    [@zjm_46_98 /opt/java]# redis-cli
    redis 127.0.0.1:6379> ping
    PONG
    redis 127.0.0.1:6379> quit
  7. Redis 命令参考