Redis学习——01.redis安装

来源:互联网 发布:最新网络歌曲下载 编辑:程序博客网 时间:2024/06/05 04:47

下载

tar -xzvf redis-3.2.10.tar.gzcd redis-3.2.10makemake install

执行完之后会在/usr/local/bin下生成redis相关的可执行文件

➜  ~ cd /usr/local/bin➜  bin git:(master) ll -h redis*-rwxr-xr-x  1 mahaiyuan  admin    98K  8  1 22:17 redis-benchmark-rwxr-xr-x  1 mahaiyuan  admin    14K  8  1 22:17 redis-check-aof-rwxr-xr-x  1 mahaiyuan  admin   1.0M  8  1 22:17 redis-check-rdb-rwxr-xr-x  1 mahaiyuan  admin   159K  8  1 22:17 redis-clilrwxr-xr-x  1 mahaiyuan  admin    12B  8  1 22:17 redis-sentinel -> redis-server-rwxr-xr-x  1 mahaiyuan  admin   1.0M  8  1 22:17 redis-server
  • redis-server redis服务器
  • redis-cli Redis命令行客户端
  • redis-benchmark Redis性能测试工具
  • redis-check-aof AOF文件修复工具

启动

后台启动

redis-server &[1] 2387023870:C 01 Aug 22:59:39.866 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf23870:M 01 Aug 22:59:39.868 * Increased maximum number of open files to 10032 (it was originally set to 7168).                _._           _.-``__ ''-._      _.-``    `.  `_.  ''-._           Redis 3.2.10 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._ (    '      ,       .-`  | `,    )     Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379 |    `-._   `._    /     _.-'    |     PID: 23870  `-._    `-._  `-./  _.-'    _.-' |`-._`-._    `-.__.-'    _.-'_.-'| |    `-._`-._        _.-'_.-'    |           http://redis.io  `-._    `-._`-.__.-'_.-'    _.-' |`-._`-._    `-.__.-'    _.-'_.-'| |    `-._`-._        _.-'_.-'    |  `-._    `-._`-.__.-'_.-'    _.-'      `-._    `-.__.-'    _.-'          `-._        _.-'              `-.__.-'23870:M 01 Aug 22:59:39.871 # Server started, Redis version 3.2.1023870:M 01 Aug 22:59:39.872 * DB loaded from disk: 0.001 seconds23870:M 01 Aug 22:59:39.872 * The server is now ready to accept connections on port 6379

停止redis

➜  ~ redis-cli shutdown23870: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 disk23870:M 01 Aug 23:00:38.227 # Redis is now ready to exit, bye bye...[1]  + 23870 done       redis-server

客户端使用

➜  ~ redis-cli127.0.0.1:6379>

or

➜  ~ redis-cli -h 127.0.0.1 -p 6379127.0.0.1:6379>

自定义启动和停止脚本

➜  redis ll -htotal 104-rw-r--r--@ 1 mahaiyuan  staff    46K  8  1 23:07 redis.conf-rwxr-xr-x  1 mahaiyuan  staff   1.6K  8  1 23:24 redis.sh

redis.sh内容如下

#!/bin/bashpath=$(cd "$(dirname "$0")"; pwd)cd $pathCONFIG_FILE=./redis.conf# check pid existspsid=0checkpid() {   redisps=`ps -ef | grep redis-server | grep 6379`   if [ -n "$redisps" ]; then      psid=`echo $redisps | awk '{print $2}'`   else      psid=0   fi}## 启动redis操作start() {   checkpid   if [ $psid -ne 0 ]; 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 0 ]; then         echo "(pid=$psid) [Success]"      else         echo "[Failed]"      fi   fi}## 停止redis操作stop() {   checkpid   if [ $psid -ne 0 ]; then      echo -n "Stopping redis ...(pid=$psid) "      ## shutdown gracefully first      /usr/local/bin/redis-cli SHUTDOWN      count=0    while [ $count -lt 20 ]    do      echo "Sleep 3s to wait server stopping..."      sleep 3      checkpid      if [ $psid -eq 0 ]; then        echo "Redis stopped."          break      fi      count=`expr $count + 1`    done    checkpid    if [ $psid -ne 0 ]; then        echo "Shutdown gracefully failed, kill -9."        kill -9 $psid      if [ $? -eq 0 ]; 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 1esacexit 0