---
title: "Centos安装Redis"
author: "Ronbay"
date: "2016-03-01T16:55:17Z"
tags: []
categories: ["互联网"]
slug: "centos-redis"
draft: false
lastmod: "2016-03-01T16:55:17Z"
id: 2279812
url: "https://ronbay.iteye.com/blog/2279812"
---

**1、安装需要的支持环境**

 

在安装Redis之前首要先做的是安装Unix的Tcl工具，如果不安装的话后期将无法对Redis进行测试。在后期执行make test的时候返回如下错误信息：You need tcl 8.xuyao de5 or newer in order to run the Redis test，具体的流程为：

cd /usr/local/src  
wget [http://downloads.sourceforge.net/tcl/tcl8.6.3-src.tar.gz](http://downloads.sourceforge.net/tcl/tcl8.6.3-src.tar.gz)  
tar -zxvf tcl8.6.3-src.tar.gz  
cd ​tcl8.6.3/unix/  
./configure  
make  
make install

 

**2、安装redis**

 

安装redis的过程非常的简单，具体教程官网也有。具体如下：[http://redis.io/download](http://redis.io/download)

 

cd /usr/local/src  
wget [http://download.redis.io/releases/redis-2.8.19.tar.gz](http://download.redis.io/releases/redis-2.8.19.tar.gz)  
tar zxvf redis-2.8.19.tar.gz  
cd redis-2.8.19  
make  
make PREFIX=/usr/local/redis install

 

其中PREFIX=/usr/local/redis可以省略，省略情况下redis会默认安装到/usr/local/bin目录下。

**3、测试Redis**

 

cd src  
make test

 

通过以上命令就要可以对redis进行加大的测试。

**4、配置redis**

 

**A、复制并修改配置文档**

 

cp ./redis.conf /usr/local/redis/  
vim /usr/local/redis/redis.conf

 

我只修改了如下两项：

daemonize yes #redis将以守护进程的方式运行，默认为no会暂用你的终端  
timeout 300​ #当 客户端闲置多长时间后关闭连接，如果指定为0，表示关闭该功能

 

更多的配置内容，后续梳理完成后再发布出来。

 

**B、设置自动启动**

 

vim /etc/init.d/redis

 

 

文件中保存如下内容：

#!/bin/sh  
\#  
\# redis        Startup script for Redis Server  
\#  
\# chkconfig: - 80 12  
\# description: Redis is an open source, advanced key-value store.  
\#  
\# processname: redis-server  
\# config: /etc/redis.conf  
\# pidfile: /var/run/redis.pid  
source /etc/init.d/functions  
BIN="/usr/local/redis/bin"  
CONFIG="/usr/local/redis/redis.conf"  
PIDFILE="/var/run/redis.pid"  
\### Read configuration  
\[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"  
RETVAL=0  
prog="redis-server"  
desc="Redis Server"  
start() {  
        if \[ -e $PIDFILE ];then  
             echo "$desc already running...."  
             exit 1  
        fi  
        echo -n $"Starting $desc: "  
        daemon $BIN/$prog $CONFIG  
        RETVAL=$?  
        echo  
        \[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog  
        return $RETVAL  
}  
stop() {  
        echo -n $"Stop $desc: "  
        killproc $prog  
        RETVAL=$?  
        echo  
        \[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE  
        return $RETVAL  
}  
restart() {  
        stop  
        start  
}  
case "$1" in  
  start)  
        start  
        ;;  
  stop)  
        stop  
        ;;  
  restart)  
        restart  
        ;;  
  condrestart)  
        \[ -e /var/lock/subsys/$prog ] && restart  
        RETVAL=$?  
        ;;  
  status)  
        status $prog  
        RETVAL=$?  
        ;;  
   \*)  
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"  
        RETVAL=1  
esac  
exit $RETVAL

 

 

**C、启动或关闭服务**

service redis start  
service redis stop

 

**5、使用redis**

 

\[root@localhost redis]# cd /usr/local/redis/bin  
\[root@localhost bin]# ./redis-cli  
127.0.0.1:6379&gt; set foo bar  
OK  
127.0.0.1:6379&gt; get foo  
"bar"  
127.0.0.1:6379&gt;