Skip to content

Redis Commands

Install

Debian/Ubuntu

  1. Prerequsities

    sudo apt-get update
    sudo apt-get upgrade
    
  2. Installing Redis

    sudo apt-get -y install redis-server
    
    sudo systemctl enable redis-server.service
    
  3. Configure Redis

    1. open

      sudo vim /etc/redis/redis.conf
      

    2. edit

      maxmemory 256mb
      maxmemory-policy allkeys-lru
      

    3. restart
      sudo systemctl restart redis-server.service
      
  4. Test Connection to Redis Server

    $ redis-cli
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379>
    
    redis-cli info
    redis-cli info stats
    redis-cli info server
    

Mac

  1. install with brew

    brew update
    brew install redis
    
  2. start/stop

    brew services start redis
    brew services stop redis
    
  3. start with confi file

    redis-server /usr/local/etc/redis.conf
    
  4. testc

    redis-cli ping
    
  5. location config

    /usr/local/etc/redis.conf
    
  6. uninstall

    brew uninstall redis
    rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
    

Docker

  1. 启动实例

    docker run --name some-redis -d redis
    # or
    # if exist, please rm first.
    docker volume rm redis-data
    # create volume for persistent data.
    docker volume create redis-data
    # start
    docker run -d \
    --name redis-secure \
    --memory=256m \
    --restart unless-stopped \
    -p 6379:6379 \
    -v redis-data:/data \
    redis:latest \
    redis-server --requirepass YourStrongPassword --maxmemory 128mb --maxmemory-policy allkeys-lru  --appendonly yes
    
  2. 持久化

    $ docker run -p 6379:6379 --name some-redis -d redis redis-server --appendonly yes
    
    3. redis-cli

    $ docker run -it --network some-network --rm redis redis-cli -h some-redis
    
  3. 挂载

    $ sudo docker run --name my-first-redis -v /myfirstredis/redis.conf:/usr/local/etc/redis/redis.conf -d redis
    

Commands

  • lua

  • bash-connection
#!/bin/bash

host=$1
port=$2

echo $host: $port
redis-cli -h $host -p $port client list | awk '{print $2}'| cut -d = -f 2| cut -d : -f 1 | sort | uniq -c | sort -rn | awk '{"host " $2 | getline h; print $1 "\t" $2 "\t" h;}'| awk '{print $1 "\t" $2 "\t" $7}'