社区编辑申请
注册/登录
我们一起了解Redis的爱恨情仇
数据库 Redis
Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。

Redis是一个key-value 存储系统,是跨平台的非关系型数据库。

Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。

Redis 通常被称为数据结构服务器,因为值(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。

redis的安装介质在:https://github.com/tporadowski/redis/releases

下载后运行得到一个运行路径。

修改redis.windows-service.conf文件,一个是端口,一个是密码认证

#bind 127.0.0.1             #注释掉这一句,使redis可以外部访问
port 6379 #默认端口,可以改成别的端口
protected-mode yes #修改为yes,开启保护模式,默认是yes
#daemonize no #这一句是注释的,windows版本不支持,默认是no
requirepass 123456 #密码
appendonly yes

进行指定目录下,运行redis

# 启动redis失败 Could not create server TCP listening socket 127.0.0.1:6379: bind: 操作成功

操作办法:

redis命令,设置key,value值

上面提到的字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型,具体操作也不复杂

C:\Program Files\Redis>redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set key1 v1
OK
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> get key
(nil)
127.0.0.1:6379> set name "wbq"
OK
127.0.0.1:6379> get name
"wbq"
127.0.0.1:6379> set name "whp"
OK
127.0.0.1:6379> get name
"whp"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> hmset name field1 "wbq" field2 "whp"
OK
127.0.0.1:6379> hget name field1
"wbq"
127.0.0.1:6379> hget name field2
"whp"
127.0.0.1:6379> lpush alist redis
(integer) 1
127.0.0.1:6379> lpush alist db2
(integer) 2
127.0.0.1:6379> lpush alist mysql
(integer) 3
127.0.0.1:6379> lrange alist 0 2
1) "mysql"
2) "db2"
3) "redis"
127.0.0.1:6379> lrange alist 0 1
1) "mysql"
2) "db2"
127.0.0.1:6379> sadd aset set1
(integer) 1
127.0.0.1:6379> sadd aset set2
(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
127.0.0.1:6379> sadd aset set3
(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
127.0.0.1:6379> sadd aset set3 [member ...]

又报错了,(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

大意就是说无法持久化写入磁盘,进行配置

127.0.0.1:6379> config set stop-writes-on-bgsave-error no
OK
127.0.0.1:6379> sadd aset set2
(integer) 1
127.0.0.1:6379> sadd aset set3
(integer) 1
127.0.0.1:6379> sadd aset set4
(integer) 1
127.0.0.1:6379> sadd aset set2
(integer) 0
127.0.0.1:6379> smembers aset
1) "set4"
2) "set3"
3) "set1"
4) "set2"
127.0.0.1:6379>

查看服务端错误日志,大意是说没有写入权限

[34752] 27 Apr 11:50:16.328 # fork operation complete
[34752] 27 Apr 11:50:16.340 # Background saving error
[34752] 27 Apr 11:50:22.046 * 1 changes in 900 seconds. Saving...
[34752] 27 Apr 11:50:22.115 * Background saving started by pid 7944
[7944] 27 Apr 11:50:22.251 # Failed opening the RDB file dump.rdb (in server root dir C:\Program Files\Redis) for saving: 数据无效。
[7944] 27 Apr 11:50:22.253 # rdbSave failed in qfork: Permission denied
[34752] 27 Apr 11:50:22.325 # fork operation complete
[34752] 27 Apr 11:50:22.338 # Background saving error
[34752] 27 Apr 11:50:28.046 * 1 changes in 900 seconds. Saving...
[34752] 27 Apr 11:50:28.112 * Background saving started by pid 20004
[20004] 27 Apr 11:50:28.229 # Failed opening the RDB file dump.rdb (in server root dir C:\Program Files\Redis) for saving: 数据无效。
[20004] 27 Apr 11:50:28.231 # rdbSave failed in qfork: Permission denied

对文件夹的读写权限进行变更后,一切正常

[34752] 27 Apr 11:50:28.326 # fork operation complete
[34752] 27 Apr 11:50:28.343 # Background saving error
[34752] 27 Apr 11:50:34.060 * 1 changes in 900 seconds. Saving...
[34752] 27 Apr 11:50:34.126 * Background saving started by pid 32840
[34752] 27 Apr 11:50:34.339 # fork operation complete
[34752] 27 Apr 11:50:34.383 * Background saving terminated with success

继续进行各类型键值操作。

127.0.0.1:6379> zadd asortedset 0 redis
(integer) 1
127.0.0.1:6379> zadd asortedset 1 db2
(integer) 1
127.0.0.1:6379> zadd asortedset 0 sqlserver
(integer) 1
127.0.0.1:6379> zrangebyscore asortedset 0 5
1) "redis"
2) "sqlserver"
3) "db2"
127.0.0.1:6379>

Redis支持多个数据库,并且每个数据库的数据是隔离的不能共享,并且基于单机才有,如果是集群就没有数据库的概念。

Redis是一个字典结构的存储服务器,而实际上一个Redis实例提供了多个用来存储数据的字典,客户端可以指定将数据存储在哪个字典中。这与我们熟知的在一个关系数据库实例中可以创建多个数据库类似,所以可以将其中的每个字典都理解成一个独立的数据库。

每个数据库对外都是一个从0开始的递增数字命名,Redis默认支持16个数据库(可以通过配置文件支持更多,无上限),可以通过配置databases来修改这一数字。客户端与Redis建立连接后会自动选择0号数据库,不过可以随时使用SELECT命令更换数据库,如要选择1号数据库:

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get key1
(nil)
127.0.0.1:6379[1]> zrangebyscore asortedset 0 5
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> zrangebyscore asortedset 0 5
1) "redis"
2) "sqlserver"
3) "db2"
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> zrangebyscore asortedset 0 5
(empty list or set)
127.0.0.1:6379[2]>

其实我真正的目的是为了实现一个最近24小时数据的队列,确保数据是持续滚动的

构建一个list列表键值,通过rpush实现24条记录写入,查看数据,持续lpop和rpush可实现一个队列,现在欠缺的是一个元数据了。

127.0.0.1:6379> del  userdaydata:username:user1:data
(integer) 1
127.0.0.1:6379> rpush userdaydata:username:user1:data 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(integer) 24
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
7) "6"
8) "7"
9) "8"
10) "9"
11) "10"
12) "11"
13) "12"
14) "13"
15) "14"
16) "15"
17) "16"
18) "17"
19) "18"
20) "19"
21) "20"
22) "21"
23) "22"
24) "23"
127.0.0.1:6379> lpop userdaydata:username:user1:data
"0"
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
10) "10"
11) "11"
12) "12"
13) "13"
14) "14"
15) "15"
16) "16"
17) "17"
18) "18"
19) "19"
20) "20"
21) "21"
22) "22"
23) "23"
127.0.0.1:6379> rpush userdaydata:username:user1:data 0
(integer) 24
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
10) "10"
11) "11"
12) "12"
13) "13"
14) "14"
15) "15"
16) "16"
17) "17"
18) "18"
19) "19"
20) "20"
21) "21"
22) "22"
23) "23"
24) "0"
127.0.0.1:6379> lpop userdaydata:username:user1:data
"1"
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
9) "10"
10) "11"
11) "12"
12) "13"
13) "14"
14) "15"
15) "16"
16) "17"
17) "18"
18) "19"
19) "20"
20) "21"
21) "22"
22) "23"
23) "0"
127.0.0.1:6379> rpush userdaydata:username:user1:data 1
(integer) 24
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
9) "10"
10) "11"
11) "12"
12) "13"
13) "14"
14) "15"
15) "16"
16) "17"
17) "18"
18) "19"
19) "20"
20) "21"
21) "22"
22) "23"
23) "0"
24) "1"
127.0.0.1:6379>

后文会将如何通过python进行redis操作。

参考1:https://blog.csdn.net/qq_45047809/article/details/112529734

参考2:https://www.runoob.com/redis/redis-tutorial.html

责任编辑:武晓燕 来源: python与大数据分析
相关推荐

2022-04-12 11:15:31

Redis消息队列数据库

2022-03-01 10:45:38

Redis服务器数据

2022-04-02 14:02:23

WindowsRedis 6.xredis

2022-03-31 13:58:37

分布式SpringRedis

2022-04-12 10:36:52

数据库PostgreSQLMongoDB

2022-03-19 12:16:49

Redis高并发系统集群部署

2022-02-25 08:55:19

BitMapRedis面试题

2022-04-25 11:26:16

开发SpringBoot

2022-04-24 14:11:26

病毒僵尸网络网络攻击

2022-03-31 08:21:14

数据库缓存双写数据一致性

2022-03-30 15:06:25

数据传输Harmony源码分析

2022-04-18 18:04:16

锐捷平台

2022-03-04 14:57:50

缓存数据库代码

2022-03-01 11:33:36

企业微信Zabbix监控软件

2022-03-29 11:59:34

梳理标签体系

2022-04-13 09:33:33

疫情物联网IOT

2022-04-18 14:41:40

GitHub开发数据

2022-04-08 16:57:08

戴尔

2020-09-23 07:39:59

SpringBoot项目Redis

2022-05-13 09:47:28

Docker容器

同话题下的热门内容

讲讲Redis各个数据类型的底层数据结构

编辑推荐

几款开源的图形化Redis客户端管理软件推荐Redis内存淘汰策略,看这一篇就够了!Redis 的默认端口为啥是 6379 ?Redis集群的5种使用方式,各自优缺点分析Redis为什么这么快?一文深入了解Redis内存模型!
我收藏的内容
点赞
收藏

51CTO技术栈公众号