目录
SECURITY安全配置
#############################SECURITY#################################
# Warning: since Redis is pretty fast an outside user can try up to
# 1 million passwords per second against a modern box. This means that you
# should use very strong passwords, otherwise they will be very easy to break.
# Note that because the password is really a shared secret between the client
# and the server, and should not be memorized by any human, the password
# can be easily a long string from /dev/urandom or whatever, so by using a
# long and unguessable password no brute force attack will be possible.
#警告:由于Redis速度非常快,外部用户每秒可以在一个现代化的盒子上尝试多达100万个密码。这意味着你应该使用非常强的密码,否则它们很容易被破解。
请注意,由于密码实际上是客户机和服务器之间的共享秘密,任何人都不应该记住,因此密码可以很容易地从/dev/uradom或其他任何地方变成一个长字符串,因此通过使用长且不可用的密码,就不可能进行暴力攻击。# Redis ACL users are defined in the following format:
#Redis ACL用户的定义格式如下:
#
# user <username> ... acl rules ...
#
# For example:
#
# user worker +@list +@connection ~jobs:* on >ffa9203c493aa99
#
# The special username "default" is used for new connections. If this user
# has the "nopass" rule, then new connections will be immediately authenticated
# as the "default" user without the need of any password provided via the
# AUTH command. Otherwise if the "default" user is not flagged with "nopass"
# the connections will start in not authenticated state, and will require
# AUTH (or the HELLO command AUTH option) in order to be authenticated and
# start to work.
#特殊用户名“default”用于新连接。如果该用户具有“nopass”规则,则新连接将立即作为“默认”用户进行身份验证,而无需通过AUTH命令提供任何密码。否则,如果“默认”用户未标记为“nopass”,则连接将在未验证状态下启动,并需要AUTH(或HELLO命令AUTH选项)才能进行验证并开始工作。
#
# The ACL rules that describe what an user can do are the following:
#描述用户可以做什么的ACL规则如下:
#
# on Enable the user: it is possible to authenticate as this user.
#on 启用用户:可以作为该用户进行身份验证。
# off Disable the user: it's no longer possible to authenticate
#off 禁用用户:无法再进行身份验证
# with this user, however the already authenticated connections
# will still work.
#然而,对于这个用户,已经通过身份验证的连接仍然可以工作。
# +<command> Allow the execution of that command
#+<command> 允许执行该命令
# -<command> Disallow the execution of that command
#-<command> 禁止执行该命令
# +@<category> Allow the execution of all the commands in such category
# with valid categories are like @admin, @set, @sortedset, ...
# and so forth, see the full list in the server.c file where
# the Redis command table is described and defined.
# The special category @all means all the commands, but currently
# present in the server, and that will be loaded in the future
# via modules.
#+@<category> 允许执行此类类别中的所有命令,有效类别包括@admin、@set、@sortedset...以此类推,查看服务器中的完整列表。描述和定义Redis命令表的server.c文件。特殊类别@all表示所有命令,但当前存在于服务器中,将来将通过模块加载这些命令
# +<command>|subcommand Allow a specific subcommand of an otherwise
# disabled command. Note that this form is not
# allowed as negative like -DEBUG|SEGFAULT, but
# only additive starting with "+".
#+<command>|subcommand 允许以其他方式禁用命令的特定子命令。请注意,这种形式不允许像-DEBUG | SEGFAULT一样为负数,而只允许以“+”开头的加法。
# allcommands Alias for +@all. Note that it implies the ability to execute
# all the future commands loaded via the modules system.
#allcommands 别名 +@all。请注意,它意味着能够执行通过模块系统加载的所有未来命令。
# nocommands Alias for -@all.
#nocommands 别名表示 -@all。
# ~<pattern> Add a pattern of keys that can be mentioned as part of
# commands. For instance ~* allows all the keys. The pattern
# is a glob-style pattern like the one of KEYS.
# It is possible to specify multiple patterns.
# ~<pattern> 添加可作为命令一部分提及的按键模式。例如~*允许所有键。该图案是一种类似于键之一的球形图案。可以指定多个模式。
# allkeys Alias for ~*
#allkeys 别名 ~*
# resetkeys Flush the list of allowed keys patterns.
#resetkeys 刷新允许的密钥模式列表。
# ><password> Add this passowrd to the list of valid password for the user.
# For example >mypass will add "mypass" to the list.
# This directive clears the "nopass" flag (see later).
#><password> 将此密码添加到用户的有效密码列表中。例如,mypass会将“mypass”添加到列表中。该指令清除“nopass”标志(见下文)。
# <<password> Remove this password from the list of valid passwords.
#<<password> 从有效密码列表中删除此密码。
# nopass All the set passwords of the user are removed, and the user
# is flagged as requiring no password: it means that every
# password will work against this user. If this directive is
# used for the default user, every new connection will be
# immediately authenticated with the default user without
# any explicit AUTH command required. Note that the "resetpass"
# directive will clear this condition.
#nopass 用户的所有设置密码都将被删除,并且该用户被标记为不需要密码:这意味着每个密码都将针对该用户。如果此指令用于默认用户,则每个新连接都将立即通过默认用户的身份验证,而无需任何明确的AUTH命令。请注意,“resetpass”指令将清除此条件
# resetpass Flush the list of allowed passwords. Moreover removes the
# "nopass" status. After "resetpass" the user has no associated
# passwords and there is no way to authenticate without adding
# some password (or setting it as "nopass" later).
#resetpass刷新允许的密码列表。此外,删除“nopass”状态。在“resetpass”之后,用户没有相关的密码,如果不添加一些密码(或稍后将其设置为“nopass”),就无法进行身份验证
# reset Performs the following actions: resetpass, resetkeys, off,
# -@all. The user returns to the same state it has immediately
# after its creation.
#reset 执行以下操作:resetpass、resetkeys、off、-@all。用户在创建后立即返回到相同的状态。
#
# ACL rules can be specified in any order: for instance you can start with
# passwords, then flags, or key patterns. However note that the additive
# and subtractive rules will CHANGE MEANING depending on the ordering.
#ACL规则可以按任何顺序指定:例如,可以从密码开始,然后是标志或密钥模式。但是请注意,加法和减法规则将根据顺序改变含义
# For instance see the following example:
#例如,请参见以下示例:
#
# user alice on +@all -DEBUG ~* >somepassword
#
# This will allow "alice" to use all the commands with the exception of the
# DEBUG command, since +@all added all the commands to the set of the commands
# alice can use, and later DEBUG was removed. However if we invert the order
# of two ACL rules the result will be different:
#这将允许“alice”使用除调试命令之外的所有命令,因为+@all将所有命令添加到alice可以使用的命令集中,并且后来删除了调试。然而,如果我们颠倒两个ACL规则的顺序,结果将不同:
#
# user alice on -DEBUG +@all ~* >somepassword
#
# Now DEBUG was removed when alice had yet no commands in the set of allowed
# commands, later all the commands are added, so the user will be able to
# execute everything.
#现在,当alice在允许的命令集中还没有任何命令时,DEBUG就被删除了命令,之后添加所有命令,这样用户就可以执行所有操作。
#
# Basically ACL rules are processed left-to-right.
#基本上ACL规则是从左到右处理的。
#
# For more information about ACL configuration please refer to
# the Redis web site at https://redis.io/topics/acl
#有关ACL配置的更多信息,请参阅Redis网站https://redis.io/topics/acl
# ACL LOG
#ACL日志
#
# The ACL Log tracks failed commands and authentication events associated
# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked
# by ACLs. The ACL Log is stored in memory. You can reclaim memory with
# ACL LOG RESET. Define the maximum entry length of the ACL Log below.
acllog-max-len 128
#ACL日志跟踪与ACL关联的失败命令和身份验证事件。ACL日志对于排除ACL阻止的失败命令非常有用。ACL日志存储在内存中。可以通过ACL日志重置来回收内存。在下面定义ACL日志的最大条目长度。acllog最大长度128
# Using an external ACL file
#使用外部ACL文件
#
# Instead of configuring users here in this file, it is possible to use
# a stand-alone file just listing users. The two methods cannot be mixed:
# if you configure users here and at the same time you activate the exteranl
# ACL file, the server will refuse to start.
#不必在此文件中配置用户,可以使用一个单独的文件来列出用户。这两种方法不能混合使用:如果在此处配置用户,同时激活外部ACL文件,服务器将拒绝启动
#
# The format of the external ACL user file is exactly the same as the
# format that is used inside redis.conf to describe users.
#外部ACL用户文件的格式与内部使用的格式redis.conf完全相同来描述用户。
#
# aclfile /etc/redis/users.acl
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatiblity
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#重要提示:从Redis 6开始,“requirepass”只是新ACL系统之上的一个兼容层。选项的效果只是为默认用户设置密码。客户端仍将像往常一样使用AUTH<password>进行身份验证,或者如果遵循新协议,则更明确地使用AUTH default<password>进行身份验证:两者都可以工作。
#
#requirepass foobored
requirepass 123456
# Command renaming (DEPRECATED).
#命令重命名(已弃用)。
#
# ------------------------------------------------------------------------
# WARNING: avoid using this option if possible. Instead use ACLs to remove
# commands from the default user, and put them only in some admin user you
# create for administrative purposes.
#警告:如果可能,请避免使用此选项。而是使用ACL从默认用户中删除命令,并仅将它们放在为管理目的而创建的某个管理用户中。
# ------------------------------------------------------------------------
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
#可以在共享环境中更改危险命令的名称。例如,CONFIG命令可能会被重命名为难以猜测的内容,因此它仍然可以用于内部使用工具,但不可用于普通客户端。
#
# Example:
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possible to completely kill a command by renaming it into
# an empty string:
#通过将命令重命名为空字符串,也可以完全终止命令:
#
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to replicas may cause problems.
#请注意,更改登录到AOF文件或传输到副本的命令的名称可能会导致问题。
总结:
警告:由于Redis速度非常快,外部用户每秒可以在一个现代化的盒子上尝试多达100万个密码。这意味着你应该使用非常强的密码,否则它们很容易被破解。
请注意,由于密码实际上是客户机和服务器之间的共享秘密,任何人都不应该记住,因此密码可以很容易地从/dev/uradom或其他任何地方变成一个长字符串,因此通过使用长且不可用的密码,就不可能进行暴力攻击。
1.Redis ACL用户的定义格式如下:
user <username> ... acl rules ...
例如:user worker +@list +@connection ~jobs:* on >ffa9203c493aa99
#特殊用户名“default”用于新连接。如果该用户具有“nopass”规则,则新连接将立即作为“默认”用户进行身份验证,而无需通过AUTH命令提供任何密码。否则,如果“默认”用户未标记为“nopass”,则连接将在未验证状态下启动,并需要AUTH(或HELLO命令AUTH选项)才能进行验证并开始工作。
2.描述用户可以做什么的ACL规则如下:
启用或禁用用户
on 启用用户:可以作为该用户进行身份验证。
off 禁用用户:无法再进行身份验证
然而,对于这个用户,已经通过身份验证的连接仍然可以工作。
启用或禁用命令
+<command> 允许执行该命令
-<command> 禁止执行该命令
+@<category> 允许用户执行此类类别中的所有命令
-@<category> 禁止用户调用 <category> 分类中的所有命令
+<command>|subcommand 允许以其他方式禁用命令的特定子命令。请注意,这种形式不允许像-DEBUG | SEGFAULT一样为负数,而只允许以“+”开头的加法。
allcommands 别名 +@all。允许调用所有命令。请注意,它意味着能够执行通过模块系统加载的所有未来命令。
nocommands 别名表示 -@all。 禁止调用所有命令
允许或禁止访问某些KEY
~<pattern> 添加可作为命令一部分提及的按键模式。例如~*允许所有键。使用 ~* 与 allkeys 效果相同
resetkeys 刷新允许的密钥模式列表。使用当前模式覆盖所有允许的模式。
为用户配置有效密码
><password> 将此密码添加到用户的有效密码列表中。例如,mypass会将“mypass”添加到列表中。
<<password> 从有效密码列表中删除此密码。
nopass 用户的所有设置密码都将被删除,并且该用户被标记为不需要密码:这意味着每个密码都将针对该用户。如果此指令用于默认用户,则每个新连接都将立即通过默认用户的身份验证,而无需任何明确的AUTH命令。请注意,“resetpass”指令将清除此条件
resetpass刷新允许的密码列表。此外,删除“nopass”状态。在“resetpass”之后,用户没有相关的密码,如果不添加一些密码(或稍后将其设置为“nopass”),就无法进行身份验证
reset 执行以下操作:resetpass、resetkeys、off、-@all。用户在创建后立即返回到相同的状态。
提示:未使用nopass 标记且没有有效密码列表的用户,实际上是无法使用的。因为无法以该用户的身份登录。
3.ACL规则可以按任何顺序指定
例如,可以从密码开始,然后是标志或密钥模式。但是请注意,加法和减法规则将根据顺序改变含义。
user alice on +@all -DEBUG ~* >somepassword
这将允许“alice”使用除调试命令之外的所有命令,因为+@all将所有命令添加到alice可以使用的命令集中,并且后来删除了调试。然而,如果我们颠倒两个ACL规则的顺序,结果将不同:
user alice on -DEBUG +@all ~* >somepassword
现在,当alice在允许的命令集中还没有任何命令时,DEBUG就被删除了命令,之后添加所有命令,这样用户就可以执行所有操作。
基本上ACL规则是从左到右处理的。
有关ACL配置的更多信息,请参阅Redis网站https://redis.io/topics/acl
4.ACL日志
ACL日志跟踪与ACL关联的失败命令和身份验证事件。ACL日志对于排除ACL阻止的失败命令非常有用。ACL日志存储在内存中。可以通过ACL日志重置来回收内存。在下面定义ACL日志的最大条目长度。acllog最大长度128
5.使用外部ACL文件
不必在此文件中配置用户,可以使用一个单独的文件来列出用户。这两种方法不能混合使用:如果在此处配置用户,同时激活外部ACL文件,服务器将拒绝启动。
外部ACL用户文件的格式与内部使用的格式redis.conf完全相同来描述用户。
重要提示:从Redis 6开始,“requirepass”只是新ACL系统之上的一个兼容层。选项的效果只是为默认用户设置密码。客户端仍将像往常一样使用AUTH<password>进行身份验证,或者如果遵循新协议,则更明确地使用AUTH default<password>进行身份验证:两者都可以工作。
requirepass 123456
6.常用命令
acl help 帮助文档
acl list 查看当前活动的acl
acl cat 查看命令类别
acl cat list 显示所有指定类别下的所有命令
acl users 返回所有用户名
acl whoami 返回当前用户名
acl setuser <username> 创建或修改用户
acl getuser <username> 查看用户的acl权限
acl deluser <username> 删除指定用户
auth <username> <password> 切换用户
acl save 持久化
acl load 重载
acl log acl日志
扩展:
Redis是key-value的数据结构,每条数据都是⼀个键值对;键的类型是字符串。
值的类型分为五种:字符串 string、哈希 hash、列表 list、集合 set、有序集合 zset
命令
key
keys * 获取所有的key
select 0 选择第一个库
move myString 1 将当前的数据库key移动到某个数据库,目标库有,则不能移动
flush db 清除指定库
randomkey 随机key
type key 类型
set key1 value1 设置key
get key 获取key
mset key1 value1 key2 value2 key3 value3
mget key1 key2 key3
del key 删除key
exists key 判断是否存在key
expire key 10 10过期
pexpire key 1000 毫秒
persist key 删除过期时间
string
set key value 设置键值
get key 根据键获取值
getrange key 0 -1 字符串分段
getset name new_cxx 设置值,返回旧值
mset key1 key2 批量设置
mget key1 key2 批量获取
setnx key value 不存在就插入(not exists)
setex key time value 过期时间(expire)
expire key seconds 设置过期时间,以秒为单位
ttl key 查看有效时间,以秒为单位
setrange key index value 从index开始替换value
incr age 递增1
incrby age 10 递增
decr age 递减1
decrby age 10 递减
incrbyfloat 增减浮点数
append key value 追加值
strlen 长度
getbit/setbit/bitcount/bitop 位操作
hash
hset key field value 将哈希表 key 中的字段 field 的值设为 value
hsetnx key filed value 只有在字段 field 不存在时,设置哈希表字段的值。
hget key value
例子:hmset myhash name cxx age 25 note "i am notes"
hmget myhash name age note
hgetall key 获取所有的
hexists key value 判断是否存在
hincrby key filed increment 递增increment
hdel key value 删除
hkeys key 获取所有哈希表中的字段key
hvals key 获取哈希表中所有值value
hlen key 获取哈希表中字段的数量
list
lpush key value1 value2.... 左插入
rpush key value1 value2.... 在列表中添加一个或多个值(右插入)
lrange key 0 -1 数据集合
lpop key 移出并获取列表的第一个元素
rpop key 移除列表的最后一个元素,返回值为移除的元素。
llen key 长度
lrem key count value 删除
lindex key index 根据key获取指定索引的值
lset key 2 n 索引设值
ltrim key 0 4 删除key
linsert key before value 在列表的元素前插入元素
linsert key after value 在列表的元素后插入元素
rpoplpush list list2 转移列表的数据
set
sadd key memeber1 memeber2... 向集合添加一个或多个成员
smembers key 数据集合
srem key set1 删除
sismember key set1 判断元素是否在集合中
scard key_name 个数
sdiff | sinter | sunion 操作:集合间运算:差集 | 交集 | 并集
srandmember 随机获取集合中的元素
spop 从集合中弹出一个元素
zset
zadd key 1 one
zadd key 2 two
zadd key 3 three
zincrby key 1 one 增长分数
zscore key two 获取分数
zrange key 0 -1 withscores 范围值
zrangebyscore key 10 25 withscores 指定范围的值
zrangebyscore key 10 25 withscores limit 1 2 分页
Zrevrangebyscore key 10 25 withscores 指定范围的值
zcard key 元素数量
Zcount key 获得指定分数范围内的元素个数
Zrem key one two 删除一个或多个元素
Zremrangebyrank key 0 1 按照排名范围删除元素
Zremrangebyscore key 0 1 按照分数范围删除元素
Zrank key 0 -1 分数最小的元素排名为0
Zrevrank key 0 -1 分数最大的元素排名为0
Zinterstore
zunionstore rank:last_week 7 rank:20150323 rank:20150324 rank:20150325 weights 1 1 1 1 1 1 1