2
获取Redis包可以在官网获取最新的下载路径]
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
解压、编译
tar xzf redis-3.2.9.tar.gz
cd redis-3.2.9
make
make test
make install

make test时可能会报如下错误:

cd src && make test
make[1]: Entering directory `/root/redis-3.2.0/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-3.2.0/src'
make: *** [test] Error 2
我们安装一下 tcl 支持,然后在进行编译操作
yum install tcl
编译的时间可能会比较长,大家耐心等待一会儿
!!! WARNING The following tests failed:

*** [err]: Server is able to generate a stack trace on selected systems in tests/integration/logging.tcl
expected stack trace not found into log file
Cleanup: may take some time... OK
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-3.2.0/src'
make: *** [test] Error 2

上面的警告只是某个测试没有通过,可以忽略。
编译完成后会在src文件夹中生成redis-serverredis-benchmarkredis-cli
新建一个自己的文件夹,将上面的文件放入其中,并且将redis安装包解压目录下的redis.conf也拷贝入新建文件夹

mkdir /etc/redis 
cp redis-server /etc/redis
cp redis-benchmark /etc/redis
cp redis-cli /etc/redis
cd ..
cp redis.conf /etc/redis
cd /etc/redis

转到目录,执行

redis-server redis.conf

如果出现
redis安装成功
则安装成功,但是这样子每次运行后不能关闭命令窗口,不然进程就会断掉。

将redis做成一个服务

redis-3.2.0/utils/redis_init_script是redis初始化脚本
将其复制到etc服务中

cp redis-3.2.0/utils/redis_init_script /etc/rc.d/init.d/redis
如果这时添加注册服务:
chkconfig --add redis

将报以下错误:

service redis does not support chkconfig

我们需要修改一下启动的脚本:

#!/bin/sh
#chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/etc/redis/redis-server
CLIEXEC=/etc/redis/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
修改的内容:
  1. 添加上面代码中的第二行chkconfig: 2345 80 90内容
  2. 按情况修改7、8行
EXEC=/etc/redis/redis-server
CLIEXEC=/etc/redis/redis-cli
  1. 第20行添加&表示服务在后台运行[这样关闭命令行之后也会继续运行]
    我们看到第11行指定的配置文件是CONF="/etc/redis/${REDISPORT}.conf"以端口为名的conf[这样改变端口时不同端口可以使用不同conf]

所以

cp /etc/redis/redis.conf /etc/redis/6379.conf
执行注册chkconfig --add redis便不会报错了
启动服务service redis start

我是广告

本人的直播课程在 7 月份就要开始了,希望小伙伴们支持一下,现在报名有优惠噢

https://segmentfault.com/l/15...

https://segmentfault.com/l/15...


拿客_三产
2.6k 声望147 粉丝