天天看點

【學習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信号。

繼續閱讀