天天看点

【学习Redis】- Redis的安装、启动、停止

安装Redis

说明:

操作系统:Centos 6.9

[[email protected] /]# cat /proc/version
Linux version 2.6.32-696.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) ) #1 SMP Tue Mar 21 19:29:05 UTC 201
           

以下几条命令对Redis的源码进行了编译。

[[email protected] ~]# wget http://download.redis.io/redis-stable.tar.gz
           
[[email protected] ~]# tar xzvf redis-stable.tar.gz 
redis-stable/
redis-stable/INSTALL
           
[[email protected] ~]# cd redis-stable
           
[[email protected] redis-stable]# make
           

使用make test命令测试Redis是否编译正确,报出以下错误。

[[email protected] redis-stable]# make test
cd src && make test
make[1]: Entering directory `/root/redis-stable/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/redis-stable/src'
make[1]: Entering directory `/root/redis-stable/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-stable/src'
make: *** [test] Error 2
           

解决方案,安装tcl,下面命令是对tcl进行源码编译安装

[[email protected] ~]# wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz 
           
[[email protected] ~]# tar xzvf tcl8.6.1-src.tar.gz -C /usr/local
           
[[email protected] ~]# cd /usr/local/tcl8.6.1/unix/
           
[[email protected] unix]# ./configure
           
[[email protected] unix]# make
           
[[email protected] unix]# make install
           

再次使用make test命令测试Redis是否编译正确,结果成功。

[[email protected] unix]# cd ~/redis-stable
[[email protected] redis-stable]# make test 
           

最后make install命令进行安装

[[email protected] redis-stable]# make install
           

启动、停止Redis

首先我们了解Redis包含的可执行文件如下图

【学习Redis】- Redis的安装、启动、停止

我们在执行 make install 命令后,这些程序会被复制到 /usr/local/bin 目录内,所以我们在命令行直接输入程序名称即可执行。

直接启动Redis服务器

[[email protected] redis-stable]# cd /usr/local/bin/
[[email protected] bin]# ls
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server  tclsh8.6
[[email protected] bin]# redis-server 
62530:C 26 Feb 23:38:03.190 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
62530:C 26 Feb 23:38:03.191 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=62530, just started
62530:C 26 Feb 23:38:03.191 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
62530:M 26 Feb 23:38:03.194 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 62530
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

62530:M 26 Feb 23:38:03.206 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
           

启动成功,这也说明了我们安装的没有问题。

Redis 服务器默认使用 6379 端口,可通过 --port 参数自定义端口号:

[[email protected] bin]# redis-server --port 6380
           

通过初始化脚本启动Redis

在Linux 系统中可以通过初始化脚本启动Redis,使Redis 能随系统自动运行,在生成环境中推荐使用此方法运行Redis。在Redis 源代码目录的 utils 文件夹中有名为 redis_init_script 的初始化脚本文件,内容如下:

[[email protected] utils]# cat redis_init_script
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

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
[[email protected] utils]# 
           

我们需要配置Redis 的运行方式和持久化文件、日志文件的存储位置等,如下:

配置初始化脚本

将初始化脚本复制到  /etc/init.d 目录中,文件名为redis_端口号,其中端口号表示让Redis监听的端口号,客户端通过该端口号连接Redis。然后修改脚本第12行的REDISPORT变量的值为同样的端口号。

[[email protected] utils]# cp redis_init_script /etc/init.d/redis_6379
[[email protected] utils]# ls /etc/init.d/redis_6379 
/etc/init.d/redis_6379
           

建立需要的文件夹

/etc/redis  存放 Redis 配置文件

/var/redis/端口号    存放Redis 的持久化文件

修改配置文件

将配置文件模板 redis.conf 复制到 /etc/redis  目录中 更名为6379.conf,然后按如下修改

【学习Redis】- Redis的安装、启动、停止

现在就可以使用/etc/init.d/redis_端口号start 来启动Redis了,而后需要执行下面命令使Redis随系统自动启动:

[[email protected] etc]# ln -s /etc/init.d/redis_6379 /etc/rc0.d/k90redis
[[email protected] etc]# ln -s /etc/init.d/redis_6379 /etc/rc6.d/k90redis
[[email protected] etc]# ln -s /etc/init.d/redis_6379 /etc/rc5.d/S90redis
           

停止Redis 

考虑到Redis有可能正将内存中的数据同步到硬盘中,强行终止Redis进程可能导致数据丢失。正确停止Redis方法为

[[email protected] /]# redis-cli SHUTDOWN
           

kill redis进程的PID,Redis可以妥善处理SIGTERM信号。

继续阅读