首页 > 代码库 > Ubuntu14.04下安装redis-3.2.0以及开机自启动

Ubuntu14.04下安装redis-3.2.0以及开机自启动

 

去官网下载Redis-3.2.0.tar.gz,将redis-3.2.0.tar.gz放入/opt目录下

解压redis-3.2.0.tar.gz

xiaoyao@xiaoyao-virtual-machine:/opt$ sudo tar -zxvf redis-3.2.0.tar.gz

编译redis

xiaoyao@xiaoyao-virtual-machine:/opt$ cd redis-3.2.0/
xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ sudo make

安装

xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ sudo make install

测试是否安装通过

xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ sudo make test
见到此字样:\o/ All tests passed without errors!表示测试安装通过

如果出现You need tcl 8.5 or newer in order to run the Redis test错误

解决办法

xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ sudo apt-get install tcl8.5

修改配置文件,以便其它机器能连接redis服务

xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ sudo vim /opt/redis-3.2.0/redis.conf

将bind 127.0.0.1改为bind 0.0.0.0

将redis.conf文件拷贝至/etc/redis目录下

xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ sudo mkdir /etc/redis
xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ sudo cp redis.conf /etc/redis/redis.conf

启动redis服务

xiaoyao@xiaoyao-virtual-machine:/opt/redis-3.2.0$ /usr/local/bin/redis-server /etc/redis/redis.conf

进入redis客户端,测试redis

xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ ./redis-cli
127.0.0.1:6379> ping
PONG

通过redisclient软件去连接redis也没有问题。

设置开机自启动

  • 修改redis.conf

    #打开后台运行选项
    daemonize yes
    #设置日志文件路径
    logfile "/var/log/redis.log"
    
  • 编写脚本

    xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ sudo touch /etc/init.d/redis
    xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ sudo vim /etc/init.d/redis
    

    下面是脚本的内容

    #!/bin/sh
    # chkconfig: 2345 10 90
    # description: Start and Stop redis
    
    PATH=/usr/local/bin
    REDISPORT=6379
    EXEC=/usr/local/bin/redis-server
    REDIS_CLI=/usr/local/bin/redis-cli
    PIDFILE=/var/run/redis.pid
    CONF="/etc/redis/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
            if [ "$?"="0" ]
            then
                echo "Redis is running..."
            fi
            ;;
        stop)
            if [ ! -f $PIDFILE ]
            then
                echo "$PIDFILE exists, process is not running."
            else
                PID=$(cat $PIDFILE)
                echo "Stopping..."
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x $PIDFILE ]
                do
                    echo "Waiting for Redis to shutdown..."
                    sleep 1
                done
                echo "Redis stopped"
            fi
            ;;
        restart|force-reload)
            ${0} stop
            ${0} start
            ;;
        *)
            echo "Usage: /etc/init.d/redis {start|stop|restart|fore-reload}"
            exit 1
    esac 
    
  • 添加脚本的执行权限

    xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ sudo chmod +x /etc/init.d/redis
    
  • 设置开机自动启动

    xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ sudo update-rc.d redis defaults
    
  • 使用脚本启动服务

    开启redis
    xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ service redis start
    停止redis
    xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ service redis stop
    重启redis
    xiaoyao@xiaoyao-virtual-machine:/usr/local/bin$ service redis restart
    
  • 将机器关机,重新启动

此时发现redis服务也启动了

 
 

Ubuntu14.04下安装redis-3.2.0以及开机自启动