redis介绍
Redis(它的英文全称是Remote Dictionary Server)是一种主要基于内存存储和运行的,能快速响应的键值数据库产品。 在读写响应性能上,基于内存存储的Redis数据库最好的。相对于关系型数据库,Redis的应用范围相对较窄,但在互联网业务环境下的很多大型网站都需要它。因此,我们需要好好学习它 根据Redis官网介绍,Redis是一个开源的基于内存处理的数据结构存储系统,可以作为数据库使用,也可以用于缓存处理和消息传递处理。 它支持的数据结构包括字符串、列表、散列表、集合、带范围查询的有序集合、位图、Hyperloglog和带半径查询的地理空间索引。 Redis提供了内置复制、Lua脚本、LRU驱动事件、事务和不同级别的磁盘持久化功能,并通过哨兵和集群自动分区功能实现高可用。 Redis还提供了原子操作功能,如增加字符串内容,在散列表中增减值操作,在列表中增加一个元素成员,进行集合的交、并、差等运算,或者从有序集合获得排名最高的成员等。 Redis主要在内存中实现对各类数据的运算,以提高数据处理速度,但Redis也提供了隔一段时间转存到磁盘,或者通过命令附加到日志来持久化数据。当然为了提高效率,也可以完全禁用持久化功能。
redis安装
步骤一:打开官网 redis.io/download 步骤二:安装 下载,编译redis源码
wget http://download,redis.io/releases/redis-5.0.7.tar.gz
tar xzf redis-5.0.7.tar.gz
cd redis-5.0.7
make
启动:
./src/redis-server
步骤三:测试客户端连接服务器
./src/redis-cli
127.0.0.1:6379> set user1 tom
OK
127.0.0.1:6379> get user1
"tom"
127.0.0.1:6379>
步骤四:以后台进程方式启动redis 第一步:修改redis.conf文件
daemonize no 修改为 daemonize yes
第二步:指定redis.conf文件启动
./src/redis-server /data/redis-5.0.7/redis.conf
项目连接redis问题
排查ip能不能通
C:\Users\qq>ping 192.168.240.129
正在 Ping 192.168.240.129 具有 32 字节的数据:
来自 192.168.240.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.240.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.240.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.240.129 的回复: 字节=32 时间<1ms TTL=64
192.168.240.129 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
C:\Users\qq>
排查redis的端口是否为通
-DENIED Redis is running in protected mode because protected mode is enabled,
no bind address was specified, no authentication password is requested to clients.
In this mode connections are only accepted from the loopback interface.
If you want to connect from external computers to Redis you may adopt one of the following solutions:
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by
connecting to Redis from the same host the server is running,
however MAKE SURE Redis is not publicly accessible from internet if you do so.
Use CONFIG REWRITE to make this change permanent.
2) Alternatively you can just disable the protected mode by editing the Redis configuration file,
and setting the protected mode option to 'no', and then restarting the server.
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option.
4) Setup a bind address or an authentication password.
NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
遗失对主机的连接。
解决方案
127.0.0.1:6379> config set protected-mode no
OK
127.0.0.1:6379>
如果还是不行,查看虚机的防火墙
systemctl status firewalld 发现当前是dead状态,即防火墙未开启。显示running即已开启了
systemctl start firewalld 执行命令没有任何提示,即开启成功。
防火墙开启,在继续执行命令即可。执行
firewall-cmd --zone=public --add-port=6379/tcp --permanent ,提示success,表示设置成功
如果要关闭防火墙设置,可能通过systemctl stop firewalld这条指令来关闭该功能。
redis存储模式
Redis数据库数据存储模式,基于键值(Key-Value)基本存储原理的基础上,进行细化分类,构建了具有自身特点的数据结构类型。Redis官网提供的数据结构类型已经达到8种。在对数据进行各种命令操作之前,首先需要掌握Redis的数据结构类型的特点。
字符串
字符串内容是二进制安全的,这意味着程序可以把数字、文本、图片、视频等都赋给这个值。
注意:
①键名命名要容易阅读,这样方便系统维护;键名不要过长;太长的键名会影响Redis数据库的执行效率
②值最大的长度不能超过512MB
③键可以用业务:主键id方式增加键的提示信息
列表
列表是由若干插入顺序排序的字符串元素组成的集合。可以理解为多个字符串组成一个集合对象,并按照链表的插入顺序排序,在读写操作时只能从两头开始
注意:
①允许内容重复出现
②由于列表采用链表的技术实现,所以在链表头插入新字符串,速度非常快
③列表可以用于聊天记录、博客评论等无需调整字符串顺序,而又需要快速响应的应用场景
④List的有序排序,指按照插入的顺序排列,而非ASCII顺序排列
集合
集合是指由不重复且无序的字符串元素构成的一个整体,不重复意味着集合中元素都是唯一的。这与列表主要区别之一:无序,则意味着所有字符串值的读写可以是任意的,不存在列表一定要从两头操作的问题,集合对字符串地址的统一管理原理,决定了字符串值之间是无序的,这是与列表的又一个区别。
注意:
一个集合内的字符串值不能重复
一个集合内的字符串值不排序
散列表
散列表可以存储多个键值对的映射,是无序的一种数据集合。键内容必须唯一,不能重复。键内容中间可以采用":"的隔离符号,主要增加可阅读性。键必须为字符串类型,值可以是字符串类型或者数字类型,由于这个特性,Hash特别适用于存储一个对象。将一个对象存储在Hash中会占用更少内存,并且可以方便的存取整个对象。Redis中每个Hash可以存储键值2的32次方减1对(40多亿)
注意:
①键内容的字符串不应太长,以免占用过多内存,影响执行效率
②Hash更适合对小规模数据结构对象的存储及操作
有序集合
有序集合和散列一样都是由键值对构成的集合,主要区别是有序集合根据值进行自动排序,而散列表值不排序;有序集合可以对值直接进行操作,而散列通过键查找来获取值。有序集合的键字符串必须唯一;值可以重复,值必须可以解析为浮点数。
注意:
①有序集合由于采用自动值排序,所以在数据量相对多的情况下,在检索速度上会比散列快
②有序集合支持大量的值更新,这在游戏修改积分等方面具有应用优势
③有序集合的键又叫成员,值又叫分值
位图
位图不是实际的数据类型,而是在String类型上定义的一组面向比特的操作,由于字符串是二进制安全的BLOB,并且他们的最大长度是512MB,他们适合于设置为2的32次方个不同的位。 用位图表示存储信息,可以极大节省内存空间。在512MB的内存空间上可以记录40亿用户ID信息。
HyperLogLog
HyperLogLog是一种概率数据结构,用于对数据集合中的数据进行唯一性统计。统计的结果就是集合的基数,也就是该数据结构不包含统计集合的元素,只记录统计结果值。 如HyperLogLog的统计对象是{1,3,5,7,9,9,10}数据集,那么这个数据集的基数集为{1,3,5,7,9,10},最后统计结果基数为6. HyperLogLog数据结构来统计集合基数,需要内存在12kb内。它的优点是在集合元素越多或存储量越大的情况下,计算基数所需要的存储空间是相对固定的,并且很小。
地理空间
地理空间是一种特殊的有序集合,存储维度、经度、名称、地理空间位置等信息。地理空间有序集合使用一种称为Geohash的技术进行填充。经度和维度的位是交错的,以形成一个独特的52位整数。
redis config详细说明
Redis的配置文件位于Redis安装目录下,文件名为redis.conf(Windows下的文件名为redis.windows.conf)。 可以通过CONFIG命令查看或设置配置项,命令格式如下:
127.0.0.1:6379> config get *
1) "dbfilename"
2) "dump.rdb"
3) "requirepass"
4) ""
5) "masterauth"
6) ""
7) "cluster-announce-ip"
8) ""
9) "unixsocket"
10) ""
11) "logfile"
12) ""
13) "pidfile"
14) ""
15) "slave-announce-ip"
16) ""
17) "replica-announce-ip"
18) ""
19) "maxmemory"
20) "0"
21) "proto-max-bulk-len"
22) "536870912"
23) "client-query-buffer-limit"
24) "1073741824"
25) "maxmemory-samples"
26) "5"
27) "lfu-log-factor"
28) "10"
29) "lfu-decay-time"
30) "1"
31) "timeout"
32) "0"
33) "active-defrag-threshold-lower"
34) "10"
35) "active-defrag-threshold-upper"
36) "100"
37) "active-defrag-ignore-bytes"
38) "104857600"
39) "active-defrag-cycle-min"
40) "5"
41) "active-defrag-cycle-max"
42) "75"
43) "active-defrag-max-scan-fields"
44) "1000"
45) "auto-aof-rewrite-percentage"
46) "100"
47) "auto-aof-rewrite-min-size"
48) "67108864"
49) "hash-max-ziplist-entries"
50) "512"
51) "hash-max-ziplist-value"
52) "64"
53) "stream-node-max-bytes"
54) "4096"
55) "stream-node-max-entries"
56) "100"
57) "list-max-ziplist-size"
58) "-2"
59) "list-compress-depth"
60) "0"
61) "set-max-intset-entries"
62) "512"
63) "zset-max-ziplist-entries"
64) "128"
65) "zset-max-ziplist-value"
66) "64"
67) "hll-sparse-max-bytes"
68) "3000"
69) "lua-time-limit"
70) "5000"
71) "slowlog-log-slower-than"
72) "10000"
73) "latency-monitor-threshold"
74) "0"
75) "slowlog-max-len"
76) "128"
77) "port"
78) "6379"
79) "cluster-announce-port"
80) "0"
81) "cluster-announce-bus-port"
82) "0"
83) "tcp-backlog"
84) "511"
85) "databases"
86) "16"
87) "repl-ping-slave-period"
88) "10"
89) "repl-ping-replica-period"
90) "10"
91) "repl-timeout"
92) "60"
93) "repl-backlog-size"
94) "1048576"
95) "repl-backlog-ttl"
96) "3600"
97) "maxclients"
98) "10000"
99) "watchdog-period"
100) "0"
101) "slave-priority"
102) "100"
103) "replica-priority"
104) "100"
105) "slave-announce-port"
106) "0"
107) "replica-announce-port"
108) "0"
109) "min-slaves-to-write"
110) "0"
111) "min-replicas-to-write"
112) "0"
113) "min-slaves-max-lag"
114) "10"
115) "min-replicas-max-lag"
116) "10"
117) "hz"
118) "10"
119) "cluster-node-timeout"
120) "15000"
121) "cluster-migration-barrier"
122) "1"
123) "cluster-slave-validity-factor"
124) "10"
125) "cluster-replica-validity-factor"
126) "10"
127) "repl-diskless-sync-delay"
128) "5"
129) "tcp-keepalive"
130) "300"
131) "cluster-require-full-coverage"
132) "yes"
133) "cluster-slave-no-failover"
134) "no"
135) "cluster-replica-no-failover"
136) "no"
137) "no-appendfsync-on-rewrite"
138) "no"
139) "slave-serve-stale-data"
140) "yes"
141) "replica-serve-stale-data"
142) "yes"
143) "slave-read-only"
144) "yes"
145) "replica-read-only"
146) "yes"
147) "slave-ignore-maxmemory"
148) "yes"
149) "replica-ignore-maxmemory"
150) "yes"
151) "stop-writes-on-bgsave-error"
152) "yes"
153) "daemonize"
154) "no"
155) "rdbcompression"
156) "yes"
157) "rdbchecksum"
158) "yes"
159) "activerehashing"
160) "yes"
161) "activedefrag"
162) "no"
163) "protected-mode"
164) "no"
165) "repl-disable-tcp-nodelay"
166) "no"
167) "repl-diskless-sync"
168) "no"
169) "aof-rewrite-incremental-fsync"
170) "yes"
171) "rdb-save-incremental-fsync"
172) "yes"
173) "aof-load-truncated"
174) "yes"
175) "aof-use-rdb-preamble"
176) "yes"
177) "lazyfree-lazy-eviction"
178) "no"
179) "lazyfree-lazy-expire"
180) "no"
181) "lazyfree-lazy-server-del"
182) "no"
183) "slave-lazy-flush"
184) "no"
185) "replica-lazy-flush"
186) "no"
187) "dynamic-hz"
188) "yes"
189) "maxmemory-policy"
190) "noeviction"
191) "loglevel"
192) "notice"
193) "supervised"
194) "no"
195) "appendfsync"
196) "everysec"
197) "syslog-facility"
198) "local0"
199) "appendonly"
200) "no"
201) "dir"
202) "/root/redis-5.0.7"
203) "save"
204) "3600 1 300 100 60 10000"
205) "client-output-buffer-limit"
206) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
207) "unixsocketperm"
208) "0"
209) "slaveof"
210) ""
211) "notify-keyspace-events"
212) ""
213) "bind"
214) ""
以上是Redis所有的默认配置参数值。
编辑配置
可以通过修改redis.conf文件或使用CONFIG SET命令来修改配置。CONFIG SET命令的基本语法如下:
CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
参数说明
redis.conf常规配置项说明
redis命令
redis的命令会在以后的几篇文章中详细说明和实战