centos7安装redis并设置开机自启动

把redis-3.2.13.tar.gz放到/usr/local下面

解压

tar -zxvf redis-3.2.13.tar.gz

解压得到的文件夹重命名为redis

安装c语言环境

yum install -y gcc

yum install -y gcc-c++

进入redis目录下

cd redis

编译

make

安装

make install

更改配置文件

vim redis.conf

把daemonize no 改成daemonize yes

修改为本机ip地址

bind 192.168.30.11

保存并退出

后台启动redis

nohup ./src/redis-server redis.conf &

查看redis有没有启动成功

ps -ef | grep redis

root       5795      1  0 15:02 ?        00:00:00 ./redis-server 192.168.30.11:6379

root       5806   1676  0 15:05 pts/0    00:00:00 grep --color=auto redis

设置redis开机自启

vim /etc/init.d/redis

#!/bin/bash

# chkconfig:   2345 90 10

# description:  Redis is a persistent key-value database

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379

# 如果不知道redis-server,redis-cli命令在哪里,通过find / -name redis-server/redis-cli 查找

EXEC=/usr/local/redis/src/redis-server

REDIS_CLI=/usr/local/redis/src/redis-cli

PIDFILE=/var/run/redis_6379.pid

CONF="/usr/local/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

                        sleep 2

                       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|force-reload}" >&2

                exit 1

esac

执行/etc/init.d/redis测试是否成功

Starting Redis server...

Redis is running...

设置开机自启

chkconfig redis on

你可能感兴趣的:(redis,数据库,缓存,centos)