- redis
- Remote DIctionary Server
nosql, 键值存储, 可持久存储; 高级的 kv 存储和数据结构存储(列表, 字典, 哈希表, 集合...); 数据集是保存在内存中, 所以访问性能很好; 周期性的将内存中的数据写到磁盘中, 以实现数据的持久性功能; 单线程服务器(只有一个进程或线程); 支持在服务器端运行 Lua 脚本; 支持主从模式; 支持 cluster 技术;
- redis
- KV cache and store:
- in-memory
持久化
主从(借助于 sentinel 实现一定意义上的 HA)
Clustering(分布式)
数据结构服务器:
- string,List,Hash,Set,sorted Set(有序集合),bitmaps,HyperLogLog
- memcached
内存中的 cache, 不提供持久存储能力; 多线程;
丰富的操作: hashs,sets,sorted sets,hyperloglog 等
內建 replication(复制)及 cluster
就地更新操作
支援持久化(磁盘): 避免雪崩效应
memcached 优势:
多线程: 善用多核 CPU; 更少的阻塞操作
更少的内存开销
更少的内存分配压力
可能有更少的内存碎片
存储系统有三类:
RDBMS: 关系型数据库: oracle,db2,postgresql,mysql,sql server
NoSQL: 非关系型: hbase,mongodb,memcached,redis,cassandra
NewSQL: 分布式: aerospike,foundationdb,rethinkdb
----------------------------------------------
redis 安装
http://www.redis.io/
epel 源
- yum install -y redis
- rpm -ql redis
- /etc/rc.d/init.d/redis
- /etc/rc.d/init.d/redis-sentinel
- /etc/redis-sentinel.conf
- /etc/redis.conf
- /etc/security/limits.d/95-redis.conf
- /usr/bin/redis-benchmark
- /usr/bin/redis-check-aof
- /usr/bin/redis-check-rdb
- /usr/bin/redis-cli
- /usr/bin/redis-sentinel
- /usr/bin/redis-server
- /usr/libexec/redis-shutdown
配置文件:
/etc/redis.conf
启动:
- service redis restart
- lsof -i:6379
进入 redis:
redis-cli -h 192.168.1.114
帮助:
查看命令组帮助: help @STRING (help @按 tab 键补全)
查看某命令帮助: help @command help @list
CLIENT LIST: 列出所有的 client
SELECT 1: 进入 1 号数据库(默认是 0)
SET disto centos 设置 disto 的值为 centos
GET disto 查看 disto 的值
append disto rhel 设置 disto 为多个值, 另一只值为 rhel
STRLEN disto 查看 disto 值的长度
SETEN disto centos 该键值不存在时, 才设定值为 centos(若已经存在则该操作未能执行)
SETXX disto centos 当该键值存在时, 设定新的值为 centos(若不存在则该操作未能执行)
keys * 查看所有的 key
INCR
DECR
列表 LIST 操作:
LPUSH:lpush(key, value): 在名称为 key 的 list 头添加一个值为 value 的 元素
RPUSH:rpush(key, value): 在名称为 key 的 list 尾添加一个值为 value 的元素
LPOP: 返回并删除名称为 key 的 list 中的首元素
RPOP: 返回并删除名称为 key 的 list 中的尾元素
LINDEX:lindex(key, index): 返回名称为 key 的 list 中 index 位置的元素
LSET:lset(key, index, value): 给名称为 key 的 list 中 index 位置的元素赋值
llen(key): 返回名称为 key 的 list 的长度
lrem(key, count, value): 删除 count 个 key 的 list 中值为 value 的元素
集合 SET 操作
sadd(key, member): 向名称为 key 的 set 中添加元素 member
sinter(key1, key2,...key N) : 求集合的交集
sunion(key1, (keys)) : 求并集
spop(key) : 随机返回并删除名称为 key 的 set 中一个元素
sismember(key, member) :member 是否是名称为 key 的 set 的元素
srem(key, member) : 删除名称为 key 的 set 中的元素 member
smove(srckey, dstkey, member) : 移到集合元素
scard(key) : 返回名称为 key 的 set 的基数
sinterstore(dstkey, (keys)) : 求交集并将交集保存到 dstkey 的集合
sunionstore(dstkey, (keys)) : 求并集并将并集保存到 dstkey 的集合
sdiff(key1, (keys)) : 求差集
sdiffstore(dstkey, (keys)) : 求差集并将差集保存到 dstkey 的集合
smembers(key) : 返回名称为 key 的 set 的所有元素
srandmember(key) : 随机返回名称为 key 的 set 的一个元素
有序集合 Sorted-sort
zadd: 向集合中添加一个元素
ZADD weekday1 1 mon 2 tue 3 wed
zcard: 返回所有元素个数
zcard weekday1
zrank: 按照索引排序 , 返回索引值
zrank weekday1 tue 查看集合 weekday1 中元素 tue 对应的索引号
zrange
zrange weekday1 0 2 显示集合 weekday1 中的第 1 到第 3 个元素
Hash 操作:
hset(key, field, value): 向名称为 key 的 hash 中添加元素 field
hget(key, field): 返回名称为 key 的 hash 中 field 对应的 value
hmget(key, (fields)): 返回名称为 key 的 hash 中 field i 对应的 value
hmset(key, (fields)): 向名称为 key 的 hash 中添加元素 field
hkeys(key): 返回名称为 key 的 hash 中所有键
hvals(key): 返回名称为 key 的 hash 中所有键对应的 value
hdel(key, field): 删除名称为 key 的 hash 中键为 field 的域
hincrby(key, field, integer): 将名称为 key 的 hash 中 field 的 value 增加 integer
hexists(key, field): 名称为 key 的 hash 中是否存在键为 field 的域
hlen(key): 返回名称为 key 的 hash 中元素个数
hgetall(key): 返回名称为 key 的 hash 中所有的键 (field) 及其对应的 value
#################################################################################
基于认证访问 redis
- vim /etc/redis.conf
- requirepass password
- redis-cli -h 192.168.1.114
- --->AUTH password
清空数据库:
FLUSHDB: 清空当前库
FLUSHALL: 清空所有库
事务: 一组相关的操作, 是一致性的, 要么都执行, 要么都不执行
通过 MULTI,EXEC,WATCH 等命令实现事务功能: 将一个或多个命令归并为一个操作提请服务器按顺序执行的机制; 不支持回滚操作;
MULTI: 启动一个事务
多个命令会放在队列中
EXEC: 执行事务: 一次性将事务中的所有操作执行完成后返回给客户端
WATCH: 乐观锁: 在 EXEC 命令执行之前, 用于监视指定数量的键; 如果监视中的某任意键值数据被修改, 则服务器拒绝执行事务;
connection 相关命令: help @connection
- AUTH password ---Authenticate to the server
- ECHO message ---Echo the given string
- PING [message] ---Ping the server
- QUIT - ---Close the connection
- SELECT index ---Change the selected database for the current connection
Server 相关命令: help @server
- CLIENT SETNAME connection-name ---Set the current connection name
- CLIENT GETNAME - ---Get the current connection name
- CLIENT KILL [ip:port] --- Kill the connection of a client
CONFIG RESETSTAT - ---Reset the stats returned by INFO
- CONFIG REWRITE - ---Rewrite the configuration file with the in memory configuration
- CONFIG SET parameter value ---Set a configuration parameter to the given value
- INFO [section] ---Get information and statistics about the server
- DBSIZE - ---Return the number of keys in the selected database
SAVE - ---Synchronously save the dataset to disk
BGSAVE - ---Asynchronously save the dataset to disk
LASTSAVE - ---Get the UNIX time stamp of the last successful save to disk
SLAVEOF host port ---Make the server a slave of another instance, or promote it as master
SLOWLOG subcommand [argument] ---Manages the Redis slow queries log
发布与订阅(publish/subscribe)
频道: 消息队列
SUBSCRIBE: 订阅一个或多个队列
PUBLISH: 向频道发消息
UNSUBSCRIBE: 退订此前订阅的频道
PSUBSCRIBE: 模式订阅
redis 的持久化:
RDB 和 AOF
RDB:shapshot, 快照, 二进制格式; 周期性地将数据保存至磁盘: 数据文件默认为 dump.rdb;
客户端也可显示使用 SAVE 或 BGSAVE 命令启动快照保存机制:
SAVE: 同步, 在主线程中保存快照, 此时会阻塞所有的客户端请求;
BGSAVE: 异步,
配置文件中:
默认的保存策略:
SAVE 900 1 900 秒内有 1 个值发生变化
SAVE 300 10 300 秒内有 10 个数据发生变化
SAVE 60 10000 60 秒内有 10000 个数据发生变化
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
- dbfilename dump.rdb
- dir /var/lib/redis
- AOf:Append Only File
记录每一次写操作至指定的文件尾部实现持久化; 当 redis 重启时, 可以通过重新执行文件中的命令在内存重建数据库;
BGREWRITEAOF:AOF 文件重写;
不会读取正在使用 AOF 文件, 而通过将内存中的数据已命令的方式保存到临时文件中, 完成之后替换原来的 AOF 文件;
重写过程:
1>redis 主进程通过 fork 创建子进程;
2 > 子进程根据 redis 内存中的数据创建数据库重建命令序列于临时文件中;
3 > 父进程继承 Client 的请求, 并会把这些请求中的写操作继续追加至原来 AOF 文件; 额外的, 这些新的写请求还会被放置于一个缓冲队列中;
4 > 子进程重写完成, 会通知父进程: 父进程把缓冲中的命令写到临时文件中;
5 > 父进程用临时文件替换老的 AOF 文件;
配置文件中:
appendonly no 表示没有开启 AOF
- appendfilename "appendonly.aof"
- # appendfsync always
appendfsync everysec 每秒写一次
- # appendfsync no 系统自行决定
- no-appendfsync-on-rewrite no
- auto-aof-rewrite-percentage 100
- auto-aof-rewrite-min-size 64mb
注: 持久本身不能取代备份, 还应指定备份策略, 对 redis 数据库定期进行备份;
一般 RDB 和 AOF 不能同时启用:
RDB 和 AOF 同时启用时:
BGSAVE 和 BGREWRITEAOF 不会同时执行;
在 redis 服务器启动用于恢复数据时, 会优先使用 AOF;
复制:
特点:
一个 master 可以有多个 slave;
支持链式复制;
master 以非阻塞方式同步数据至 slave;
配置:
在 slave 端:
>SLAVEOF master_ip master_port
注: 如果 master 使用了 requirepass 开启了认证功能, 从服务器要使用 masterauth password 来连入服务请求使用此密码进行认证;
sentinal:
用于管理多个 redis 服务器实现 HA
监控
通知
自动故障转移
流言协议: 投票协议
程序: redis-sentinel
redis-server
过程:
1 > 服务器自身初始化, 运行 redis-server 中专用于 sentinal 功能的代码;
2 > 初始化 sentinel 状态, 根据给定的配置文件, 初始化监控的 master 服务器的列表;
3 > 创建连向 master 的连接;
专用配置文件:/etc/redis-sentinel.conf
# sentinel monitor <master-name> <ip> <redis-port> <quorum 法定票数> 监控主节点(可以有多个)
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds mymaster 30000 master 超时 30000 毫秒认为 down
- # sentinel parallel-syncs <master-name> <numslaves> 允许多少个从服务器向主服务器发出同步请求
- sentinel parallel-syncs mymaster 1
- # sentinel failover-timeout <master-name> <milliseconds> 故障转移: 当主节点出现故障, 将从节点提升为主节点的超时时间(默认 3min)
- sentinel failover-timeout mymaster 180000
主观下线, 客观下线:
主观下线: 一个 sentinel 实例判断出某节点下线;
客观下线: 多个 sentinel 节点协商后判断出某节点下线;
redis
来源: http://www.bubuko.com/infodetail-2659767.html