1. 一些概念
1.1 NoSQL
NoSQL数据库:Not Only SQL。非传统关系型数据库。特点:数据模型灵活,可扩展性高(设计为分布式),高性能(内存存储,数据结构和算法高效),高可用(数据复制,分片等)。适用场景:实时数据分析,大数据存储和分析(分布式),内容系统(文档型数据库),社交网络和推荐系统(图数据库)
NoSQL数据库的类型
-
键值对:Redis,Mencache
-
文档型:MongoDB,ES
-
列式存储数据库:Cassandra,Clickhouse,HBase
-
图数据库:Neo4j
1.2 OLTP与OLAP
在线事务处理(OLTP):面向交易的处理系统,它是一种对数据库进行实时读写操作的系统,主要用于处理日常的业务交易。如MySQL(关系型),OceanBase(分布式数据库),Redis(内存数据库)
在线分析处理(OLAP):用于支持复杂分析和决策制定的系统,它主要对大量的历史数据进行汇总、分析和挖掘。如SSAS(关系型),EssBase(多为数据库),Clinckhouse(列式存储)。
1.3 Redis特点
AICD支持:redis所有操作是原子的,支持持久化。
数据丰富:值类型:string,list,set,sortedset,hash,sorted hash,Bitmaps,HyperLogLogs,Geospatial,Pub/Sub,Streams,Modules。
性能:支持每秒数十万次的读写操作(读的速度是 110000次/s,写的速度是 81000次/s)。单线程,事务驱动来处理并发请求。
特性:持久化(RDB,AOF);分布式特性(主从复制,哨兵模式,集群模式);支持发布订阅(pub/sub)——可作为消息队列:通知——分布式缓存更新,敏感操作审计;key过期——实时系统;支持lua脚本实现高级功能
应用场景:缓存(内存数据库,高性能读写);消息队列(支持pub/sub);会话管理(保存用户会话信息);分布式锁(原子性)
2. 基本操作
2.1 登录
2.1.1 创建redis容器
# 使用默认配置启动
docker run -d --name redis0 -p 6379:6379 redis:latest
# 挂载配置文件启动:redis-server /usr/local/etc/redis/redis.conf 表示在容器内使用配置文件启动redis服务端
docker run -d --name redis -p 6379:6379 -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf redis:latest redis-server /usr/local/etc/redis/redis.conf
2.1.2 redis-cli登录
连接
# 直接连接
docker exec -it redis0 redis-cli
# 先进入容器,再连接redis服务端
docker exec -it redis0 /bin/bash
redis-cli -h host -p port
检测服务是否运行
ping # 返回PONG 响应证明redis-server正常
ping "hello" # 带参数的ping命令,返回"hello"。快速检测连接状态、服务器可用性,测量网络延迟以及验证协议兼容性
身份验证/ACL
redis 6开始引入ACL作为身份验证方式。每个用户有自己的用户名和密码,每个用户被分配了一定的权限。default用户在被修改之前具备全部权限(相当于root用户),要确保default用户的安全性。也常专门创建一个admin用户以管理其他用户。
# 查看所有用户信息
ACL LIST # default: 默认用户,on: 该用户处于启用状态,nopass: 无需密码就能登录,~*: 可以访问所有键,+@all: 拥有所有命令的执行权限
"user default on nopass sanitize-payload ~* &* +@all"
# 查看指定用户信息
ACL GETUSER default
# 创建新用户
ACL SETUSER newuser on >password ~* +get +set
# 删除新用户
ACL DELUSER newuser
# 以特定用户身份登录redis
redis-cli -u redis://username:password@127.0.0.1:6379
传统密码验证模式:没有用户名,客户端执行auth成功后就可执行redis指令。
# redis.conf中通过配置项requirepass来设置密码
requirepass yourpassword
# 连接后验证
auth password
2.2 配置
配置文件:redis官方镜像中没有默认的redis.conf,如果需要挂载自定义的配置文件,注意要挂在到容器中的指定位置:/usr/local/etc/redis/redis.conf
redis.conf模板文件:https://redis.io/docs/latest/operate/oss_and_stack/management/config/
配置参数参考:https://www.runoob.com/redis/redis-conf.html
# 获取所有配置项
CONFIG GET *
# 获取配置项loglevel
CONFIG GET loglevel
# 设置配置项
CONFIG SET loglevel "notice"
2.3 Redis服务端管理
服务端统计信息含义参考:https://redis.io/docs/latest/commands/info/
# 查看服务端统计信息:Server,Clients,Memory,Persistence,Stats,Replication,CPU,Keyspace
INFO
2.4 补充
2.4.1 迭代查询key
用于迭代数据库中所有的key,一次迭代返回下次迭代起始游标值n1和本次迭代的所有key。
# 格式
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
# 用法
scan 0 # 第一次迭代,返回游标值,n1
scan n1 # 第二次迭代,返回游标值,n2
scan n2 # 第三次迭代
# 指定期望返回n个扫描结果,实际返回结果数量可能不等于n
scan <cursor> n
# 用于批量删除
scan <cursor> match '<match_pattern>'
2.4.2 批量删除
redis-cli -a "passwd" --scan --pattern '<pattern>' | xargs redis-cli -a "passwd" del
2.4.3 其他常用命令
# 查看帮助
help cmd
# Returns all key names that match a pattern.
KEYS pattern
# Deletes one or more keys
DEL key [key ...]
# Determines whether one or more keys exist
EXISTS key [key ...]
# Sets the expiration time of a key in seconds
EXPIRE key seconds [NX|XX|GT|LT]
# Returns the expiration time in seconds of a key.
TTL key
3. 基本数据类型
多元素类型支持的最大元素个数可以查redis官方文档ai助手:https://redis.io/chat?q=max+elements+of+zset&page=1。提示词:The maximum number of elements in a sorted set/set/list/hash
3.1 String
二进制安全:Redis 在处理字符串时,能够准确无误地存储和读取任何二进制数据,而不会对数据进行任何修改或截断。这使得string 可以包含任何数据(文本、图像、音频、视频)。
maxsize:512MB
SET key val
GET key
MSET k1 v1 k2 v2
MGET k1 k2
# Increments the integer value of a key by one. Uses 0 as initial value if the key doesn't exist
incr key
# Increments the integer value of a key by a number. Uses 0 as initial value if the key doesn't exist
INCRBY key increment
# Increment the floating point value of a key by a number. Uses 0 as initial value if the key doesn't exist
INCRBYFLOAT key increment
# Sets the string value and expiration time of a key. Creates the key if it doesn't exist
SETEX key seconds value
# Set the string value of a key only when the key doesn't exist
SETNX key value
3.2 hash
hash表:kv集合,值类型必须是string类型,而不能是hash表类型。可以使用多级哈希表模拟嵌套结构,第一层hash表重
基本用法
HSET key field value [field value ...]
HGET key field
HMSET key field value [field value ...]
HMGET key field [field ...]
HGETALL key
# return all fields of the key
HKEYS key
# return all values in a hash
HVALS key
# Increments the integer value of a field in a hash by a number. Uses 0 as initial value if the field doesn't exist
HINCRBY key field increment
# Sets the value of a field in a hash only when the field doesn't exist
HSETNX key field value
模拟嵌套hash表
# 创建外层哈希表
HSET outer_hash nested_key nested_hash_key
# 创建内层哈希表
HSET nested_hash_key subfield1 subvalue1
HSET nested_hash_key subfield2 subvalue2
# 从外层到内层访问
HGET outer_hash nested_key # 访问外层hash表,得到nested_hash_key
HGET nested_hash_key subfield1 # 访问内层hash表
3.3 List
string列表,头部/左边插入或者尾部/右边插入
LPUSHLPUSH key element [element ...]
RPUSHLPUSH key element [element ...]
# Returns the first elements in a list after removing it. Deletes the list if the last element was popped. count decide how many elements will be popped from the head of the list
LPOP key [count]
RPOP key [count]
LRANGE key start stop
# Removes and returns the first element in a list. Blocks until an element is available otherwise. Deletes the list if the last element was popped.
BLPOP key [key ...] timeout
3.4 Set
无序集合,基于哈希表实现的,插入,删除,查询O(1)
# Adds one or more members to a set. Creates the key if it doesn't exist.
SADD key member [member ...]
# Removes one or more members from a set. Deletes the set if the last member was removed.
SREM key member [member ...]
# Returns the number of members in a set.
SCARD key
# Determines whether a member belongs to a set.
SISMEMBER key member
# Returns all members of a set.
SMEMBERS key
3.5 Sorted Set
string集合,不允许重复值,每个元素配一个score,score可以重复,通过scoore来确定元素偏序。
# Adds one or more members to a sorted set, or updates their scores. Creates the key if it doesn't exist.
ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]
# Removes one or more members from a sorted set. Deletes the sorted set if all members were removed.
ZREM key member [member ...]
# Returns the score of a member in a sorted set.
ZSCORE key member
# Increments the score of a member in a sorted set.
ZINCRBY key increment member
# Returns the number of members in a sorted set.
ZCARD key
# Returns the index of a member in a sorted set ordered by ascending scores.
ZRANK key member [WITHSCORE]
# Returns the index of a member in a sorted set ordered by descending scores.
ZREVRANK key member [WITHSCORE]
# Returns the count of members in a sorted set that have scores within a range.
ZCOUNT key min max
# Returns members in a sorted set within a range of indexes.
ZRANGE key start stop [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
# Returns members in a sorted set within a range of indexes in reverse order.
ZREVRANGE key start stop [WITHSCORES]
# Returns members in a sorted set within a range of scores.
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
# Returns the difference between multiple sorted sets.
ZDIFF numkeys key [key ...] [WITHSCORES]
# Returns the intersect of multiple sorted sets.
ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
# Returns the union of multiple sorted sets.
ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
4. 其他数据类型(待补充)
HyperLogLog
Bitmaps
Geospatial Indexes
Streams
5. 使用案例(待补充)
6. 参考
6.1 系统教程
https://redis.io/about/
https://www.runoob.com/redis/redis-tutorial.html
https://www.geeksforgeeks.org/introduction-to-redis-server/
6.2 特定话题
redis有哪些产品,大概什么作用:ref:https://redis.io/docs/latest/operate/
redis管理。ref: https://redis.io/docs/latest/operate/oss_and_stack/management/