Configure Redis under Centos7 and start the machine

  • 2020-05-15 02:31:18
  • OfStack

This article mainly introduces the configuration of Redis under Centos7 and starts it up. It has a definite reference value, and those who are interested can refer to it for 1.

Recently, I needed to use Redis cache when I was doing my homework. Since Redis needs to be restarted every time I restart the server, it is really annoying. So I have this blog post.

There are only two steps:

1. Set daemonize in redis.conf to yes, and make sure that the daemon is enabled.

2. Write startup scripts

The basic principle is:

When the system is started up, the following scripts will be loaded /etc/ init. d/ To boot a new program from scratch, simply add a custom starter script to the directory and set the rules.

In this case, we create a new redis script under /etc/ init.d /, which will be loaded and executed at startup.


vim /etc/init.d/redis

Add the following to the script:


# chkconfig: 2345 10 90 
#redis The service must be at the run level 2 . 3 . 4 . 5 The following is turned on or off. The priority of startup is 90 , the priority of closing is 10 . 
# description: Start and Stop redis  

PATH=/usr/local/bin:/sbin:/usr/bin:/bin 
export PATH 
REDISPORT=6379 # The port number, which is the default, needs to be changed if it is not the default when you install it 
EXEC=/usr/local/redis/bin/redis-server #redis-server Start script location if you forget to use it find or whereis find   
REDIS_CLI=/usr/redisbin/redis-cli #redis-cli The client startup script location you can use if you forget find or whereis find   

PIDFILE=/run/redis.pid  # You can use this as well find or whereis find 
CONF="/usr/local/redis/etc/redis.conf" #redis.conf Configuration file location if you forget can be used find or whereis find 
AUTH="1234" 

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

Save and exit after writing

Set executable permissions:


chmod 755 redis

Startup test:


/etc/init.d/redis start

Successful startup will prompt the following information:


Starting Redis server...
Redis is running...

Use the redis-cli test:


[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> exit

Set to boot automatically:


chkconfig redis on

Shutdown and restart test:


reboot

After starting up, you can use redis-cli test or ps-ef | grep redis to see if redis is running


Related articles: