目录
是一个开源的C语言编写的k-v数据库
应用场景
-
数据缓存
-
session 共享
-
排行榜/计数器
nginx+lua+redis计数自动封ip
-
消息队列
kafka rabbitMQ
快速开始
root@debian:~# redis-cli
127.0.0.1:6379> SET name wanndao
OK
127.0.0.1:6379> SET age 30
OK
127.0.0.1:6379> KEYS *
1) "name"
2) "age"
127.0.0.1:6379> KEYS *
数据类型
-
string
set
SET name wangendao # 30s 后删除 SET name wangendao ex 30 get name
append
127.0.0.1:6379> APPEND name 123 (integer) 12 127.0.0.1:6379> get name "wangendao123"
mset
127.0.0.1:6379> mset name wangendao age 30 OK 127.0.0.1:6379> mget name age 1) "wangendao" 2) "30"
strlen 字符串长度
127.0.0.1:6379> STRLEN name (integer) 9 127.0.0.1:6379> STRLEN age (integer) 2
incr 整数自增
127.0.0.1:6379> INCR age (integer) 31
decr 整数自减
127.0.0.1:6379> DECR age (integer) 30
incrby 指定自增步长
127.0.0.1:6379> INCRBY age 100 (integer) 130
decrby
127.0.0.1:6379> DECRBY age 100 (integer) 30
-
list
LPUSH 和 RPUSH
127.0.0.1:6379> LPUSH user user01 user02 user03 (integer) 3
llen
127.0.0.1:6379> LLEN user (integer) 3
LRANGE
127.0.0.1:6379> LRANGE user 0 3 1) "user03" 2) "user02" 3) "user01"
LPOP 和RPOP
127.0.0.1:6379> LPOP user "user03" 127.0.0.1:6379> RPOP user "user01"
lrem
# 删除列表user中的2个 user02 元素 192.168.122.1:6379> LREM user 2 user02 (integer) 2 # 删除列表user中所有的 user01 192.168.122.1:6379> LREM user 0 user01 (integer) 2 # 删除列表user中 最右侧的user04 192.168.122.1:6379> LREM user -1 user04 (integer) 1
lset
# 修改0号索引位置的值 192.168.122.1:6379> LSET user 0 wangendao OK 192.168.122.1:6379> LRANGE user 0 10 1) "wangendao" 2) "user03"
lindex
192.168.122.1:6379> LINDEX user 0 "wangendao" 192.168.122.1:6379> LINDEX user 1 "user03"
-
set
-
zset
-
hash
hset
127.0.0.1:6379> hset user:1 name wangendao 127.0.0.1:6379> hset user:1 age 30 127.0.0.1:6379> hset user:1 mail XXXX.qq.com
hget
127.0.0.1:6379> hget user:1 name "wangendao" 127.0.0.1:6379> hget user:1 age "30" 127.0.0.1:6379> hget user:1 mail "XXXX.qq.com" 127.0.0.1:6379
hgetall
127.0.0.1:6379> HGETALL user:1 1) "name" 2) "wangendao" 3) "age" 4) "30" 5) "mail" 6) "XXXX.qq.com"
hmset
127.0.0.1:6379> hmset user:1 name wangendao age 30 mail xxx.qq.com OK 127.0.0.1:6379> HMGET user:1 name age mail 1) "wangendao" 2) "30" 3) "xxx.qq.com"
HEXISTS
127.0.0.1:6379> EXISTS user:1 (integer) 1 127.0.0.1:6379> HEXISTS user:1 name (integer) 1 127.0.0.1:6379> HEXISTS user:1 name1 (integer) 0
配置文件
如果通过yum/apt安装配置文件在/etc/redis/redis.conf
bind 0.0.0.0 ::
port 6379
##################
# rdb
dir /var/lib/redis
# /var/lib/redis/dump.rdb
dbfilename dump.rdb
# rdb 压缩
rdbcompression yes
##################
#
pidfile /var/run/redis/redis-server.pid
loglevel notice
logfile /var/log/redis/redis-server.log
# 启用安全机制
protected-mode yes
requirepass root
# 密码是root
#root@debian:~# redis-cli
#127.0.0.1:6379> AUTH root
#OK
#127.0.0.1:6379>
##################
appendfilename "appendonly.aof"
# no 按照文件系统的刷新机制
# always每次变更刷新到磁盘
# everysec 每秒刷新一次到磁盘
appendfsync everysec
通过config get *
查看当前配置
127.0.0.1:6379> CONFIG GET *
动态设置系统参数
127.0.0.1:6379> CONFIG SET loglevel debug
OK
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
databases 16
always-show-logo yes
stop-writes-on-bgsave-error yes
rdbchecksum yes
rdb-del-sync-files no
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
oom-score-adj no
oom-score-adj-values 0 200 800
appendonly no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
主从
RDB (point-in-time snapshot) 定期生成快照
AOF 记录数据库的修改操作,当数据重启时从新执行文件的操作记录
可以同时使用RDB和AOF ,也可关闭持久化
# 900s 内有1次改动操作,就执行 RDB快照
save 900 1
300s 内有10次改动操作,就执行 RDB快照
save 300 10
60s 内有10000次改动操作,就执行 RDB快照
save 60 10000
########
appendfsync everysec 每秒操作同步到磁盘
appendfsync no 使用linux 同步 默认30s
appendfsync always 每次操作同步到磁盘
分片
参考
标签:127.0,name,0.1,redis,6379,user,integer From: https://www.cnblogs.com/wangend/p/16916828.html