历史版本2 :Redis运维手册 返回文档
编辑时间: 内容长度:图片数:目录数: 修改原因:

目录:

1. Redis 单机或集群开启远程登陆编辑

注意:集群情况请将每一个节点下的 redis.conf 都按照上文修改

1)请在 redis.conf 文件找到如下配置

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
 bind 127.0.0.1

把 bind 127.0.0.1 前面用注释 # 号注释掉,这里的 bind 指的是只有指定的网段才能远程访问这个 redis。 band localhost 只能本机访问,局域网内计算机不能访问。bind 局域网 IP 只能局域网内 IP 的机器访问, 本地 localhost 都无法访问。注释掉后,就没有这个限制了。

2)将 protected-mode 要设置成 no (默认是设置为 yes 的, 阻止远程访问)

3)如已运行,请重启redis

2. 设置 Redis 远程连接密码编辑

2.1 修改密码配置

请在 redis.conf 文件中取消注释 requirepass foobared,将 foobared 改成任意密码(该文档改成123456),用于验证登录。默认是没有密码的就可以访问的,我们这里最好设置一个密码。

除了在配置文件 redis.conf 中配置验证密码以外,也可以在已经启动的 redis 服务器通过命令行设置密码,但这种方式是临时的,当服务器重启了密码必须重设。命令行设置密码方式如下:

config set requirepass 123456

不知道当前redis服务器是否有设置验证密码,或者忘记了密码是什么,我们可以通过命令行输入命令查看密码,命令如下:

config get requirepass

如果 redis 服务端没有配置密码,会得到 nil,而如果配置了密码,但是 redis 客户端连接 redis 服务端时没有用密码登录验证,会提示:operation not permitted

这时候可以用命令:auth yourpassword 进行验证密码,再执行 config set requirepass,就会显示yourpassword

注意:由于redis并发能力极强,仅仅搞密码,攻击者可能在短期内发送大量猜密码的请求,很容易暴力破解,所以建议密码越长越好,比如20位。(密码在 conf文件里是明文,所以不用担心自己会忘记)

重启redis

./redis-cli -a
./redis-server /usr/fine/redis-3.2.11/redis.conf

2.2 用密码远程连接方法一

尝试登录 redis,发现可以登上,但是执行具体命令是提示操作不允许

redis-cli -h 127.0.0.1 -p 6379  
redis 127.0.0.1:6379>  
redis 127.0.0.1:6379> keys *  
(error) ERR operation not permitted  
redis 127.0.0.1:6379> select 1  
(error) ERR operation not permitted  
redis 127.0.0.1:6379[1]>   

用密码登录并执行具体的命令看到可以成功执行

redis-cli -h 127.0.0.1 -p 6379 -a 123456  
redis 127.0.0.1:6379> keys *  
1) "myset"  
2) "mysortset"  
redis 127.0.0.1:6379> select 1  
OK  
redis 127.0.0.1:6379[1]> config get requirepass  
1) "requirepass"  
2) "123456"  

2.3 用密码远程连接方法二

redis-cli -h 172.0.0.1 -p 6379
输入密码
auth 123456

3. 热更改 Redis 配置编辑

Redis 使用 config 命令,可以对配置项参数热修改,不必重启。

redis最好不要重启,重启一次会引发如下问题: 

  • 如果数据很多(例如几个G),读起来很慢; 

  • 重启风险很大,Redis有内存陷阱 

  • 重启会引发读快照,读AOF文件

config get *   # 获得所有的配置项的key 
127.0.0.1:6379> config get *
 1) "dir"
 2) "/var/lib/redis"
 3) "dbfilename"
 4) "dump.rdb"
 5) "requirepass"
 6) (nil)
 7) "masterauth"
 8) (nil)
 9) "maxmemory"
10) "0"
11) "maxmemory-policy"
12) "volatile-lru"
13) "maxmemory-samples"
14) "3"
15) "timeout"
16) "300"
17) "appendonly"
18) "no"
19) "no-appendfsync-on-rewrite"
20) "no"
21) "appendfsync"
22) "everysec"
23) "save"
24) "900 1 300 10 60 10000"
25) "slave-serve-stale-data"
26) "yes"
27) "hash-max-zipmap-entries"
28) "512"
29) "hash-max-zipmap-value"
30) "64"
31) "list-max-ziplist-entries"
32) "512"
33) "list-max-ziplist-value"
34) "64"
35) "set-max-intset-entries"
36) "512"
37) "slowlog-log-slower-than"
38) "10000"
39) "slowlog-max-len"
40) "64"

config set timeout 250    # 改变key的value 
127.0.0.1:6379> config set timeout 250
OK

config get *   # 查看 
127.0.0.1:6379> config get *
 1) "dir"
 2) "/var/lib/redis"
 3) "dbfilename"
 4) "dump.rdb"
 5) "requirepass"
 6) (nil)
 7) "masterauth"
 8) (nil)
 9) "maxmemory"
10) "0"
11) "maxmemory-policy"
12) "volatile-lru"
13) "maxmemory-samples"
14) "3"
15) "timeout"
16) "300"
17) "appendonly"
18) "no"
19) "no-appendfsync-on-rewrite"
20) "no"
21) "appendfsync"
22) "everysec"
23) "save"
24) "9000 10 3000 100 600 100000"
25) "slave-serve-stale-data"
26) "yes"
27) "hash-max-zipmap-entries"
28) "512"
29) "hash-max-zipmap-value"
30) "64"
31) "list-max-ziplist-entries"
32) "512"
33) "list-max-ziplist-value"
34) "64"
35) "set-max-intset-entries"
36) "512"
37) "slowlog-log-slower-than"
38) "10000"
39) "slowlog-max-len"
40) "64"

4. 查看 Redis 的使用情况编辑

info  #查看redis的使用情况
redis 127.0.0.1:6379> info
# Server
redis_version:2.8.13  # Redis 服务器版本
redis_mode:standalone  # 运行模式是单机状态,redis集群时是cluster nodes
os:Linux3.5.0-48-generic x86_64  # 服务器的宿主操作系统
arch_bits:64  # 架构(32 或 64 位)
multiplexing_api:epoll  # Redis 所使用的事件处理机制
tcp_port:6379  #  TCP/IP 监听端口
uptime_in_seconds:11554  # redis启动至今经过的秒数
uptime_in_days:0   # redis启动至今经过的天数

5. Redis 运维编辑

5.1 Redis 外部命令查看info信息

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 -r 100 -i 1 info | grep used_memory_human:
#-a指定密码,-h指定主机,-p指定端口,-r运行这个命令多少次,-i运行这个命令的间隔,
/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep used_memory_rss: | awk -F ":" '{print $2}'
#查看redis使用的物理内存,相除就可以计算出比例
/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379  config set "maxmemory" "8000000000"
#修改redis的最大支持内存
/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep config_file
#查看redis的配置文件在哪

5.1 Redis 内部命令查看info信息

./redis-cli  # 启动客户端
redis 127.0.0.1:6379> time  # 显示服务器时间 , 时间戳(秒), 微秒数
redis 127.0.0.1:6379> dbsize  # 当前数据库的key的数量
redis 127.0.0.1:6379> cluster nodes  # 查看redis集群目前的主从分布和运行情况
redis 127.0.0.1:6379> set fine-1-ha "a"  #设置 fine-1-ha 的值为 a
redis 127.0.0.1:6379> set fine-2-ha "b" #设置 fine-1-ha 的值为 b
redis 127.0.0.1:6379> keys *  # 查询所有 key
redis 127.0.0.1:6379> keys fine* #模糊查询以 fine 为前缀的 key 值
redis 127.0.0.1:6379> keys *ha #模糊查询以 ha 为后缀的 key 值
redis 127.0.0.1:6379> DEL key1  # 清空指定的key,多个之间用空格隔开
redis 127.0.0.1:6379> flushall #清空整个 Redis 服务器的数据,谨慎使用
redis 127.0.0.1:6379> Flushdb  # 清空当前库所有键
redis 127.0.0.1:6379> exit #退出 Redis 客户端

其他运维命令详情可参考redis官网:http://www.redis.cn/commands.html