天天看點

Redis學習——01.redis安裝

下載下傳

tar -xzvf redis-.tar.gz
cd redis-
make
make install
           

執行完之後會在/usr/local/bin下生成redis相關的可執行檔案

➜  ~ cd /usr/local/bin
➜  bin git:(master) ll -h redis*
-rwxr-xr-x   mahaiyuan  admin    K     : redis-benchmark
-rwxr-xr-x   mahaiyuan  admin    K     : redis-check-aof
-rwxr-xr-x   mahaiyuan  admin   M     : redis-check-rdb
-rwxr-xr-x   mahaiyuan  admin   K     : redis-cli
lrwxr-xr-x   mahaiyuan  admin    B     : redis-sentinel -> redis-server
-rwxr-xr-x   mahaiyuan  admin   M     : redis-server
           
  • redis-server redis伺服器
  • redis-cli Redis指令行用戶端
  • redis-benchmark Redis性能測試工具
  • redis-check-aof AOF檔案修複工具

啟動

背景啟動

redis-server &
[] 
:C  Aug :: # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
:M  Aug :: * Increased maximum number of open files to  (it was originally set to ).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis  (/)  bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 23870
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

:M  Aug :: # Server started, Redis version 3.2.10
:M  Aug :: * DB loaded from disk:  seconds
:M  Aug :: * The server is now ready to accept connections on port 
           

停止redis

➜  ~ redis-cli shutdown
23870:M 01 Aug 23:00:38.226 # User requested shutdown...
23870:M 01 Aug 23:00:38.226 * Saving the final RDB snapshot before exiting.
23870:M 01 Aug 23:00:38.227 * DB saved on disk
23870:M 01 Aug 23:00:38.227 # Redis is now ready to exit, bye bye...
[1]  + 23870 done       redis-server
           

用戶端使用

➜  ~ redis-cli
127.0.0.1:6379>
           

or

➜  ~ redis-cli -h  -p 
:>
           

自定義啟動和停止腳本

➜  redis ll -h
total 
-rw-r--r--@  mahaiyuan  staff    K     : redis.conf
-rwxr-xr-x   mahaiyuan  staff   K     : redis.sh
           

redis.sh内容如下

#!/bin/bash
path=$(cd "$(dirname "$0")"; pwd)
cd $path

CONFIG_FILE=./redis.conf

# check pid exists
psid=
checkpid() {
   redisps=`ps -ef | grep redis-server | grep `

   if [ -n "$redisps" ]; then
      psid=`echo $redisps | awk '{print $2}'`
   else
      psid=
   fi
}
## 啟動redis操作
start() {
   checkpid

   if [ $psid -ne  ]; then
      echo "================================"
      echo "warn: redis already started! (pid=$psid)"
      echo "================================"
   else
      echo -n "Starting redis ..."
      /usr/local/bin/redis-server $CONFIG_FILE &

      checkpid
      if [ $psid -ne  ]; then
         echo "(pid=$psid) [Success]"
      else
         echo "[Failed]"
      fi
   fi
}

## 停止redis操作
stop() {
   checkpid
   if [ $psid -ne  ]; then
      echo -n "Stopping redis ...(pid=$psid) "
      ## shutdown gracefully first
      /usr/local/bin/redis-cli SHUTDOWN
      count=
    while [ $count -lt  ]
    do
      echo "Sleep 3s to wait server stopping..."
      sleep 
      checkpid
      if [ $psid -eq  ]; then
        echo "Redis stopped."
          break
      fi
      count=`expr $count + `
    done

    checkpid
    if [ $psid -ne  ]; then
        echo "Shutdown gracefully failed, kill -9."
        kill - $psid
      if [ $? -eq  ]; then
         echo "[Success]"
      else
         echo "[Failed]"
      fi
    fi
   else
      echo "================================"
      echo "warn: Redis is not running"
      echo "================================"
   fi
}

case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
  *)
     echo "Usage: $0 {start|stop}"
     exit 
esac
exit 
           

繼續閱讀