Linux-Manual

Redis 安装和配置

如果你用 Spring Data Redis 依赖请注意

如果你用 RedisDesktopManager 客户端请注意

Docker 下安装 Redis

安全情况的几个特殊配置:
bind 127.0.0.1
requirepass adgredis123456
protected-mode yes

免密情况:
bind 0.0.0.0
protected-mode no

其他:
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /data/redis_6379.pid
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

RedisCluster 集群(Docker 方式)

Redis 容器准备

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

创建 Cluster 集群(通过 redis-trib.rb)

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.19.0.2:6379
172.19.0.3:6379
172.19.0.4:6379
Adding replica 172.19.0.5:6379 to 172.19.0.2:6379
Adding replica 172.19.0.6:6379 to 172.19.0.3:6379
Adding replica 172.19.0.7:6379 to 172.19.0.4:6379
M: 9c1c64b18bfc2a0586be2089f13c330787c1f67b 172.19.0.2:6379
   slots:0-5460 (5461 slots) master
M: 35a633853329c9ff25bb93a7ce9192699c2ab6a8 172.19.0.3:6379
   slots:5461-10922 (5462 slots) master
M: 8ea2bfeeeda939abb43e96a95a990bcc55c10389 172.19.0.4:6379
   slots:10923-16383 (5461 slots) master
S: 9cb00acba065120ea96834f4352c72bb50aa37ac 172.19.0.5:6379
   replicates 9c1c64b18bfc2a0586be2089f13c330787c1f67b
S: 8e2a4bb11e97adf28427091a621dbbed66c61001 172.19.0.6:6379
   replicates 35a633853329c9ff25bb93a7ce9192699c2ab6a8
S: 5d0fe968559af3035d8d64ab598f2841e5f3a059 172.19.0.7:6379
   replicates 8ea2bfeeeda939abb43e96a95a990bcc55c10389
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join......
>>> Performing Cluster Check (using node 172.19.0.2:6379)
M: 9c1c64b18bfc2a0586be2089f13c330787c1f67b 172.19.0.2:6379
   slots:0-5460 (5461 slots) master
M: 35a633853329c9ff25bb93a7ce9192699c2ab6a8 172.19.0.3:6379
   slots:5461-10922 (5462 slots) master
M: 8ea2bfeeeda939abb43e96a95a990bcc55c10389 172.19.0.4:6379
   slots:10923-16383 (5461 slots) master
M: 9cb00acba065120ea96834f4352c72bb50aa37ac 172.19.0.5:6379
   slots: (0 slots) master
   replicates 9c1c64b18bfc2a0586be2089f13c330787c1f67b
M: 8e2a4bb11e97adf28427091a621dbbed66c61001 172.19.0.6:6379
   slots: (0 slots) master
   replicates 35a633853329c9ff25bb93a7ce9192699c2ab6a8
M: 5d0fe968559af3035d8d64ab598f2841e5f3a059 172.19.0.7:6379
   slots: (0 slots) master
   replicates 8ea2bfeeeda939abb43e96a95a990bcc55c10389
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Redis 编译安装

Redis-3.0.7 配置

# 是否以后台daemon方式运行,默认是 no,一般我们会改为 yes
daemonize no
pidfile /var/run/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile ""
# 开启数据库的数量,Redis 是有数据库概念的,默认是 16 个,数字从 0 ~ 15
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

Redis-3.2.8 配置

# 默认绑定是:127.0.0.1,这样就只能本机才能连上,为了让所有机子连上,这里需要改为:0.0.0.0
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

设置 Redis 请求密码

Redis 常用命令

把 redis 添加到系统服务中

#!/bin/sh  
#  
# redis - this script starts and stops the redis-server daemon  
#  
# chkconfig:   - 85 15  
# description:  Redis is a persistent key-value database  
# processname: redis-server  
# config:      /usr/local/redis-2.4.X/bin/redis-server  
# config:      /usr/local/ /redis-2.4.X/etc/redis.conf  
# Source function library.  
. /etc/rc.d/init.d/functions  
# Source networking configuration.  
. /etc/sysconfig/network  
# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0  
redis="/usr/local/bin/redis-server" 
prog=$(basename $redis)  
REDIS_CONF_FILE="/etc/redis.conf" 
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis  
lockfile=/var/lock/subsys/redis  
start() {  
    [ -x $redis ] || exit 5  
    [ -f $REDIS_CONF_FILE ] || exit 6  
    echo -n $"Starting $prog: "  
    daemon $redis $REDIS_CONF_FILE  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
    return $retval  
}  
stop() {  
    echo -n $"Stopping $prog: "  
    killproc $prog -QUIT  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
    return $retval  
}  
restart() {  
    stop  
    start  
}  
reload() {  
    echo -n $"Reloading $prog: "  
    killproc $redis -HUP  
    RETVAL=$?  
    echo  
}  
force_reload() {  
    restart  
}  
rh_status() {  
    status $prog  
}  
rh_status_q() {  
    rh_status >/dev/null 2>&1  
}  
case "$1" in  
    start)  
        rh_status_q && exit 0  
        $1  
        ;;  
    stop)  
        rh_status_q || exit 0  
        $1  
        ;;  
    restart|configtest)  
        $1  
        ;;  
    reload)  
        rh_status_q || exit 7  
        $1  
        ;;  
    force-reload)  
        force_reload  
        ;;  
    status)  
        rh_status  
        ;;  
    condrestart|try-restart)  
        rh_status_q || exit 0  
    ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart| reload|orce-reload}"  
        exit 2  
esac

Redis 客户端

Redis GUI 管理工具

Redis 主从(主从从)配置

Redis 主从架构

Redis主从从架构

Redis 3 主 2 从 3 哨兵–配置集群+HA(高可用、故障转移)

192.168.1.1

先做 Redis 集群,然后写个 Spring Data Redis 测试是否可以正常使用该集群

Windows 版本的 Redis

Redis Info

server 部分记录了 Redis 服务器的信息,它包含以下域:

redis_version : Redis 服务器版本
redis_git_sha1 : Git SHA1
redis_git_dirty : Git dirty flag
os : Redis 服务器的宿主操作系统
arch_bits : 架构(32 或 64 位)
multiplexing_api : Redis 所使用的事件处理机制
gcc_version : 编译 Redis 时所使用的 GCC 版本
process_id : 服务器进程的 PID
run_id : Redis 服务器的随机标识符(用于 Sentinel 和集群)
tcp_port : TCP/IP 监听端口
uptime_in_seconds : 自 Redis 服务器启动以来,经过的秒数
uptime_in_days : 自 Redis 服务器启动以来,经过的天数
lru_clock : 以分钟为单位进行自增的时钟,用于 LRU 管理
clients 部分记录了已连接客户端的信息,它包含以下域:

connected_clients : 已连接客户端的数量(不包括通过从属服务器连接的客户端)
client_longest_output_list : 当前连接的客户端当中,最长的输出列表
client_longest_input_buf : 当前连接的客户端当中,最大输入缓存
blocked_clients : 正在等待阻塞命令(BLPOP、BRPOP、BRPOPLPUSH)的客户端的数量
memory 部分记录了服务器的内存信息,它包含以下域:

used_memory : 由 Redis 分配器分配的内存总量,以字节(byte)为单位
used_memory_human : 以人类可读的格式返回 Redis 分配的内存总量
used_memory_rss : 从操作系统的角度,返回 Redis 已分配的内存总量(俗称常驻集大小)。这个值和 top 、 ps 等命令的输出一致。
used_memory_peak : Redis 的内存消耗峰值(以字节为单位)
used_memory_peak_human : 以人类可读的格式返回 Redis 的内存消耗峰值
used_memory_lua : Lua 引擎所使用的内存大小(以字节为单位)
mem_fragmentation_ratio : used_memory_rss 和 used_memory 之间的比率
mem_allocator : 在编译时指定的, Redis 所使用的内存分配器。可以是 libc 、 jemalloc 或者 tcmalloc 。
used_memory_rss_human:系统给redis分配的内存(即常驻内存)
used_memory_peak_human : Redis 的内存消耗峰值
used_memory_lua_human : 系统内存大小
expired_keys : 过期的的键数量
evicted_keys : 因为最大内存容量限制而被驱逐(evict)的键数量
used_cpu_sys_children : Redis 后台进程在 内核态 消耗的 CPU
used_cpu_user_children : Redis 后台进程在 用户态 消耗的 CPU

Redis 基准压力测试

PING_INLINE: 62189.05 requests per second
PING_BULK: 68634.18 requests per second
SET: 58241.12 requests per second
GET: 65445.03 requests per second
INCR: 57703.40 requests per second
LPUSH: 61199.51 requests per second
RPUSH: 68119.89 requests per second
LPOP: 58309.04 requests per second
RPOP: 63775.51 requests per second
SADD: 58479.53 requests per second
HSET: 61500.61 requests per second
SPOP: 58241.12 requests per second
LPUSH (needed to benchmark LRANGE): 59523.81 requests per second
LRANGE_100 (first 100 elements): 60350.03 requests per second
LRANGE_300 (first 300 elements): 57636.89 requests per second
LRANGE_500 (first 450 elements): 63251.11 requests per second
LRANGE_600 (first 600 elements): 58479.53 requests per second
MSET (10 keys): 56401.58 requests per second

资料