美文网首页
搭建redis服务并实现持久化存储、订阅和复制功能

搭建redis服务并实现持久化存储、订阅和复制功能

作者: 任总 | 来源:发表于2018-11-17 22:27 被阅读3次

一、Redis的持久化:

1、RDB逻辑:

  • snapshotting, 二进制格式;按事先定制的策略,周期性地将数据从内存同步至磁盘;数据文件默认为dump.rdb;
    客户端显式使用SAVE或BGSAVE命令来手动启动快照保存机制;
  • SAVE:同步,即在主线程中保存快照,此时会阻塞所有客户端请求,慎用;
  • BGSAVE:异步,后台同步;

RDB相关的配置:

*save <seconds> <changes>

save 900 1
save 300 10
save 60 10000 
表示:三个策略满足其中任意一个均会触发SNAPSHOTTING操作;900s内至少有一个key有变化,300s内至少有10个key有变化,60s内至少有1W个key发生变化;

stop-writes-on-bgsave-error yes
dump操作出现错误时,是否禁止新的写入操作请求;

rdbcompression yes  快照要不要压缩
rdbchecksum yes 对rdb文件做恢复校验

dbfilename dump.rdb:指定rdb文件名
*dir /var/lib/redis:rdb文件的存储路径,放在一个高性能硬盘上

2、AOF逻辑:

Append Only File, fsync重放同步
  • 记录每次写操作至指定的文件尾部实现的持久化;当redis重启时,可通过重新执行文件中的命令在内存中重建出数据库;
BGREWRITEAOF:AOF文件重写;
  • 不会读取正在使用AOF文件,而是通过将内存中的数据以命令的方式保存至临时文件中,完成之后替换原来的AOF文件;

AOF相关的配置

*appendonly no   是否启用AOF
appendfilename "appendonly.aof"  使用文件名称

*appendfsync   同步设置
Redis supports three different modes:
no:redis不执行主动同步操作,而是内核决定OS进行;
everysec:每秒一次;
always:每语句一次,所有都同步磁盘,压力过大;

no-appendfsync-on-rewrite no
是否在后台执行aof重写期间不调用fsync,默认为no,表示调用;

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
上述两个条件同时满足时,方会触发重写AOF;与上次aof文件大小相比,其增长量超过100%,且大小不少于64MB;

aof-load-truncated yes 系统意外终止,重启后是否根据内存中数据进行重构
  • 注意:持久机制本身不能取代备份;应该制订备份策略,对redis库定期备份;

3、RDB与AOF同时启用:

(1) BGSAVE和BGREWRITEAOF不会同时进行;
(2) Redis服务器启动时用持久化的数据文件恢复数据,会优先使用AOF;

二、redis使用

1、程序环境

配置文件:/etc/redis.conf
主程序:/usr/bin/redis-server
监听在:tcp6379端口
客户端:/usr/bin/redis-cli
Unit File:/usr/lib/systemd/system/redis.service
数据目录:/var/lib/redis

2、安装redis

[root@redis-25 ~]# yum install epel-release -y
[root@redis-25 ~]# yum install redis -y
[root@redis-25 ~]# systemctl start redis.service
[root@redis-25 ~]# ss -tnl
State       Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN      0      128                  127.0.0.1:6379  
#客户端命令                                                    
[root@redis-25 ~]# redis-cli -h localhost -p 6379
#使用哪个库,默认是0号库
localhost:6379> select 0
OK
localhost:6379> select 1
OK

#帮助文件
localhost:6379> help @string

3、创建和查询

#创建一个键值name是键 Tom是值
localhost:6379> set name tom
OK

#查询键、得到值
localhost:6379> get name
"tom"

#在name中的值后面添加一个字符串
localhost:6379> append name brown
(integer) 8


#查询
localhost:6379> get name
"tombrown"

#查询字符长度
localhost:6379> strlen name
(integer) 8

4、计数器

#计数器、初始值为0
localhost:6379> set count 0
OK

#累计1
localhost:6379> incr count
(integer) 1

#连续累计10
localhost:6379> incrby count 10
(integer) 12

#减去1
localhost:6379> decr count
(integer) 11

#连续减去10
localhost:6379> decrby count 10
(integer) 1

5、定向存储数据

#从左边压入数据
localhost:6379> lpush weekdays Mon Tue
(integer) 2

#查询最左边值
localhost:6379> lindex weekdays 1
"Mon"

#从右边压入数据
localhost:6379> rpush weekdays Wed Thu
(integer) 4

#查询最右边的值
localhost:6379> lindex weekdays 3
"Thu"

#删除最左边的值
localhost:6379> lpop weekdays
"Tue"

#删除最右边的值
localhost:6379> rpop weekdays
"Thu"

#在Web之后插入Fir
localhost:6379> linsert weekdays before Wed Fri
(integer) 3

#查询
localhost:6379> lindex weekdays 1
"Fri"

6、字典

#创建一个字典stu1,元素name ,值tom
localhost:6379> hset stu1 name tom
(integer) 1

#创建一个字典stu1,元素age ,值17
localhost:6379> hset stu1 age 17
(integer) 1

#在stu1字典中,连续创建
localhost:6379> hmset stu1 gender Msle major Kuihua
OK

#查询stu1中的元素
localhost:6379> hkeys stu1
1) "name"
2) "age"
3) "gender"
4) "major"

#查询stu1中的值
localhost:6379> hvals stu1
1) "tome"
2) "17"
3) "Msle"
4) "Kuihua"
#删除stu1中元素major
localhost:6379> hdel stu1 major
(integer) 1
localhost:6379> hkeys stu1
1) "name"
2) "age"
3) "gender"

#查询stu1中的元素
localhost:6379> hlen stu1
(integer) 3

#查询stu1,元素name中的字节
localhost:6379> hstrlen stu1  name
(integer) 4

7、集合

#创建两个集合tom和jerry
localhost:6379> sadd tom lucy lily hanmeimei
(integer) 3
localhost:6379> sadd jerry lucy obama trump
(integer) 3

#两个的交集
localhost:6379> sinter tom jerry
1) "lucy"

#两个的共同集
localhost:6379> sunion tom jerry
1) "lucy"
2) "hanmeimei"
3) "lily"
4) "obama"
5) "trump"

#Tom有的,jerry没有的
localhost:6379> sdiff tom jerry
1) "lily"
2) "hanmeimei"

#jerry有的,tom没有的
localhost:6379> sdiff jerry tom
1) "obama"
2) "trump"

#获取tom的所有元素
localhost:6379> smembers tom
1) "lily"
2) "hanmeimei"
3) "lucy"

#将Tom和jerry的并集存储到friends中
localhost:6379> SUNIONSTORE friends tom jerry
(integer) 5
#查询
localhost:6379> SMEMBERS friends
1) "lucy"
2) "hanmeimei"
3) "lily"
4) "obama"
5) "trump"

#有序集合
#创建colors集合
localhost:6379> ZADD colors 1 red 2 blue 8 green 5 gray
(integer) 4

#查询colors集合中的元素
localhost:6379> ZCARD colors
(integer) 4

#查询colors集合中green元素的值
localhost:6379> ZSCORE colors green
"8"

#查询colors集合中2-6索引中的元素数量
localhost:6379> ZCOUNT colors 2 6
(integer) 2

#查询colors集合0-5索引中的元素加上内部索引编号
localhost:6379> ZRANGE colors 0 5
1) "red"
2) "blue"
3) "gray"
4) "green"

#获取元素索引编号
localhost:6379> ZRANK colors gray
(integer) 2

8、发布订阅

#创建发布订阅频道military,事件caoxian
localhost:6379> PUBLISH military caoxian
(integer) 0

#订阅频道military,获取事件
localhost:6379> SUBSCRIBE military
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "military"
3) (integer) 1
1)“caoxian”

9、认证


#启用认证
[root@redis-25 ~]# vim /etc/redis.conf
### SECURITY ##
# requirepass foobared
 requirepass 123
[root@redis-25 ~]# systemctl restart redis.service
[root@redis-25 ~]# redis-cli
127.0.0.1:6379> GET name
(error) NOAUTH Authentication required. #操作需要认证
 #输入密码认证,才可操作
127.0.0.1:6379> AUTH 123 
OK

10、信息查询


#服务server节点信息
127.0.0.1:6379> INFO server
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:3dc3425a3049d2ef
redis_mode:standalone
os:Linux 3.10.0-693.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:1580
run_id:7d0c8e8ba4115fde87de43c0a9d4bd7a20afca39
tcp_port:6379    #监听端口
uptime_in_seconds:227  #运行时长
uptime_in_days:0    #运行天数
hz:10
lru_clock:14706709
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

#查询内存使用信息
127.0.0.1:6379> INFO memory
# Memory
used_memory:814480    # 已用内存
used_memory_human:795.39K # 已用内存(可辨识单位)
used_memory_rss:5947392  #常住内存集
used_memory_rss_human:5.67M
used_memory_peak:814480
used_memory_peak_human:795.39K
total_system_memory:1023688704
total_system_memory_human:976.27M  #共计可用内存
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0   #最多使用多少,0代表无限制
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:7.30
mem_allocator:jemalloc-3.6.0

#查询cpu使用信息
127.0.0.1:6379> INFO cpu
# CPU
used_cpu_sys:0.58
used_cpu_user:0.25
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

#查询链接的客户端信息
127.0.0.1:6379> CLIENT LIST
id=2 addr=127.0.0.1:58378 fd=5 name= age=650 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client

另外还有事物transactions和链接connect

相关文章

  • 搭建redis服务并实现持久化存储、订阅和复制功能

    一、Redis的持久化: 1、RDB逻辑: snapshotting, 二进制格式;按事先定制的策略,周期性地将数...

  • redis cluster 集群搭建

    redis cluster 搭建 一、redis常用的三种集群模式 主从通过持久化功能,redis保证了即使在服务...

  • Redis主从复制看这篇就够了

    什么是主从复制 持久化保证了即使 redis 服务重启也会丢失数据,因为 redis 服务重启后会将硬盘上持久化的...

  • Redis主从复制看这篇就够了

    什么是主从复制 持久化保证了即使 redis 服务重启也会丢失数据,因为 redis 服务重启后会将硬盘上持久化的...

  • 基于Redis5.0.2的总结随笔

    Redis支持数据持久化,众多数据结构存储,master-slave模式数据备份等多种功能。 Redis持久化 持...

  • 8.Redis复制、哨兵、集群

    Redis复制、哨兵、集群 Redis实现高可用相关的技术,它们包括:持久化、复制、哨兵和集群,其主要作用和解决的...

  • Redis初识

    Redis是什么? 开源 基于键值的存储服务系统 多种数据结构 高性能、功能丰富 Redis的特性 速度快 持久化...

  • Redis入门(特点 应用 数据结构 函数)

    Redis特点(C 实现的) 内存存储,查询速度快,经常用作缓存服务器 支持数据持久化,重启会加载持久化的数据,支...

  • 记录Redis学习

    1.Redis的数据类型 2.Redis的持久化 3.Redis事务 4.Redis发布订阅 5.Redis主从复制

  • Redis-持久化

    Redis-持久化 一、RDB持久化 为了解决服务器进程退出导致数据丢失,Redis提过了RDB持久化功能,可以将...

网友评论

      本文标题:搭建redis服务并实现持久化存储、订阅和复制功能

      本文链接:https://www.haomeiwen.com/subject/fiejxqtx.html