Redis installation and self-starting configuration under Linux

Redis is a memory-based, persistent key-value pair database implemented in C, which is often used as a cache service in distributed services. This article will introduce how to install and configure startup services from scratch under CentOS.

1. Install Redis

The installation of Redis is actually quite simple. The recommended way is to download the source code of redis and install it after compiling it locally.

Enter the download directory of the main folder for the first time and execute wget to download the source code

[zhxilin@localhost ~ ]$ cd download
[zhxilin@localhost download ] $ wget http://download.redis.io/redis-stable.tar.gz

After decompression, move to /usr/redis directory

[zhxilin@localhost 下载]$ tar -zxvf redis-stable.tar.gz 
[zhxilin@localhost 下载]$ su mv redis-stable /usr/redis

Then enter the redis directory, execute the make command, and compile the redis source code

[root@localhost 下载]# cd /usr/redis/
[root@localhost redis]# make

After the compilation is completed, two important programs are generated in the src directory, one is redis-server and the other is redis-cli; then enter the src directory, execute make install, and then these executable programs will be copied to /usr/ In the local/bin directory, since /usr/local/bin is defined under the system environment variable $PATH, the terminal can execute redis-server and redis-cli at any location.

[root@localhost redis]# cd src/
[root@localhost src]# make install

At this point, the installation of redis is complete.

Let's take a look at what the compiled programs do:

redis-server: As the name suggests, the redis service

redis-cli: redis client, which provides a redis client for connecting to the redis service and performing operations such as additions, deletions, changes, and queries

redis-sentinel: monitoring management, notification and instance failover service for redis instances

redis-benchmark: performance testing tool for redis

redis-check-aof: If persisted in AOF mode, it is used for quick repair when an accident occurs

redis-check-rdb: if persisted in RDB mode, it is used for quick repair when an accident occurs

 

After the installation is complete, start redis-server and run redis-cli for testing

[zhxilin@localhost ~]$ redis-server
[zhxilin@localhost ~]$ redis-cli 
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> 

This means that the redis service is working normally. If the redis service is not started, the error Could not connect to Redis at 127.0.0.1:6379: Connection refused will be reported when redis-cli is run.

 

2. Configure self-start

In order for redis-server to run automatically when the system starts, the redis service needs to be run as a daemon. We go back to the /usr/redis/ directory to find a redis.conf file, which is the redis service running When the configuration is loaded, let's first observe the content

[zhxilin@localhost return]$ vi redis.conf

This file is very long, but mostly comments, and we focus on a few of the settings daemonize and pidfile :

The default value of daemonize is false, and the default value of pidfile is pidfile /var/run/redis_6379.pid

The first one indicates whether to daemonize, obviously we have to change it to daemonize yes;

The second means that when the service runs as a daemon process, redis will write the pid to the /var/run/redis_6379.pid file by default . The file exists while the service is running. Once the service stops, the file is automatically deleted, so it can be used with to determine whether redis is running .

Exit after saving.

有了基本配置,redis还需要有一个管理启动、关闭、重启的一个脚本。redis源码里其实已经提供了一个初始化脚本,位置在/usr/redis/utils/redis_init_script

我们来看看这个脚本做了些什么:

复制代码
#!/bin/sh#

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/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
复制代码

脚本中指定了端口、server路径、cli路径、pidfile路径以及conf路径,上述标黄的地方都需要正确配置,多说一句,如果在安装时执行了make install,那么这里的脚本不需要做多大改动,因为make install把server和cli都拷到/usr/local/bin下面了。

另外看到这里conf的路径,我们需要把redis目录下的redis.conf文件拷贝到/etc/redis/6379.conf

[root@localhost utils]# cd /etc
[root@localhost etc]# mkdir redis
[root@localhost etc]# cp /usr/redis/redis.conf /etc/redis/6379.conf 

接着将redis_init_script脚本拷贝到/etc/init.d/redisd

[root@localhost etc]# cp /usr/redis/utils/redis_init_script /etc/init.d/redisd 

在/etc/init.d下的脚本都是可以在系统启动是自动启动的服务,而现在还缺一个系统启动时的配置:

[root@localhost zhxilin]# chkconfig redisd on

然后就会发现报了一个错误:服务 redisd 不支持 chkconfig ?

参考这篇文章, 这是因为我们需要在redis_init_script的开头加一个小改动:

#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key
-value database

至于这里2345 90 10分别代表什么意思,请参考上面的文章链接。

保存完重新拷贝到/etc/init.d/redisd后,再运行chkconfig就完成了。

 

一切就绪之后,可以执行以下命令检验service是否设置成功:

[root@localhost zhxilin]# service redisd start 
[root@localhost zhxilin]# service redisd stop

等价于

[root@localhost zhxilin]# /etc/init.d/redisd start 
[root@localhost zhxilin]# /etc/init.d/redisd stop

 

最后重启一下系统吧,进入系统之后直接运行redis-cli检验redis服务是否已经自动运行了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325960260&siteId=291194637