Redis 使用

1
2
3
4
5
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
tar xzf redis-4.0.2.tar.gz
cd redis-4.0.2
sudo make test
suto make install

Redis对于Linux是官方支持的,普通安装、使用按照官方指导,5分钟以内就能搞定。详情请参考:Redis官网下载链接 http://redis.io/download

但有时候又想在windows下使用redis,可以从redis下载页面看到如下提示(在页面中搜索 “windows”):

1
2
3
Win64   Unofficial  The Redis project does not directly support Windows,   
however the Microsoft Open Tech group develops and maintains
an Windows port targeting Win64.

大意就是 Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,项目地址是:项目地址
打开以后,可以直接使用浏览器下载,或者Git克隆。

https://github.com/MicrosoftArchive/redis/releases

在 Release 页面中,可以找到 msi 安装文件以及 .zip 文件

下载解压,在解压后的bin目录下有以下这些文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
C:\ProfessionSofware\Redis\Redis-x64-3.2.100 的目录

2017-08-15 22:04 <DIR> .
2017-08-15 22:04 <DIR> ..
2016-07-01 16:27 1,024 EventLog.dll
2016-07-01 16:07 12,509 Redis on Windows Release Notes.docx
2016-07-01 16:07 16,727 Redis on Windows.docx
2016-07-01 16:28 409,088 redis-benchmark.exe
2016-07-01 16:28 4,370,432 redis-benchmark.pdb
2016-07-01 16:28 257,024 redis-check-aof.exe
2016-07-01 16:28 3,518,464 redis-check-aof.pdb
2016-07-01 16:28 499,712 redis-cli.exe
2016-07-01 16:28 4,526,080 redis-cli.pdb
2016-07-01 16:28 1,666,560 redis-server.exe
2016-07-01 16:28 7,081,984 redis-server.pdb
2016-07-01 16:07 48,212 redis.windows-service.conf
2016-07-01 16:07 48,201 redis.windows.conf
2016-07-01 09:17 14,265 Windows Service Documentation.docx

1
2
3
4
5
6
redis-benchmark.exe         #基准测试  
redis-check-aof.exe # aof
redis-check-dump.exe # dump
redis-cli.exe # 客户端
redis-server.exe # 服务器
redis.windows.conf # 配置文件

当然,还有一个 RedisService.docx 文件,看似是一些启动和安装服务的说明文档,
但是照着他的指示来,你就会死的很惨,莫名其妙的死了,不知道原因。
[换机器重新测试后已查明,如果不是Administrator用户,就会出各种问题,服务安装以后启动不了等等问题,
应该可以修改服务的属性–>登录用户等选项来修正.]

如果你安装的windows没有Administrator账户,请参考这篇文章:
Windows 7 启用超级管理员Administrator账户的N种方法

启动脚本如下:

1
redis-server  redis.windows.conf

可以将其保存为文件 startup.bat ; 下次就可以直接启动了。

启动,OK,成功.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
WKQ@WKQ-PC C:\ProfessionSofware\Redis\Redis-x64-3.2.100
> redis-server redis.windows.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 9740
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

[9740] 15 Aug 22:05:36.713 # Server started, Redis version 3.2.100
[9740] 15 Aug 22:05:36.713 * The server is now ready to accept connections on port 6379


使用 ctrl c 退出后

[9740] 15 Aug 22:20:37.003 * 1 changes in 900 seconds. Saving...
[9740] 15 Aug 22:20:37.048 * Background saving started by pid 9752
[9740] 15 Aug 22:20:37.148 # fork operation complete
[9740] 15 Aug 22:20:37.148 * Background saving terminated with success
[9740] 15 Aug 22:45:46.762 # User requested shutdown...
[9740] 15 Aug 22:45:46.762 * Saving the final RDB snapshot before exiting.
[9740] 15 Aug 22:45:46.789 * DB saved on disk
[9740] 15 Aug 22:45:46.790 # Redis is now ready to exit, bye bye...

WKQ@WKQ-PC C:\ProfessionSofware\Redis\Redis-x64-3.2.100

然后可以使用自带的客户端工具进行测试。
双击打开 redis-cli.exe , 如果不报错,则连接上了本地服务器,然后测试,比如 set命令,get命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WKQ@WKQ-PC C:\ProfessionSofware\Redis\Redis-x64-3.2.100
> redis-cli
127.0.0.1:6379> help
redis-cli 3.2.100
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit

To set redis-cli perferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc

127.0.0.1:6379> set str teststr
OK
127.0.0.1:6379> get str
"teststr"

127.0.0.1:6379> exit

WKQ@WKQ-PC C:\ProfessionSofware\Redis\Redis-x64-3.2.100

you need tcl 8.5

可能遇到的问题

sudo make test 时 [exception]: Executing test client: couldn’t execute “src/redis-benchmark”: no such file or directory.

1
2
3
4
5
6
[exception]: Executing test client: couldn't execute "src/redis-benchmark": no such file or directory.
couldn't execute "src/redis-benchmark": no such file or directory
while executing
"exec src/redis-benchmark -p $R_port(0) -n 10000000 -r 1000 incr __rand_int__ > /dev/null &"
("uplevel" body line 31)
invoked from within

忽略这个错误,对安装没影响,安装完可以用

使用

连接

redis-cli -h host -p port -a password

select dbIndex
默认16个数据库:0-15,进入redis后默认是0库。不建议使用多个数据库

1
2
3
λ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> select 1
OK
1
2
3
λ redis-cli.exe  -h 127.0.0.1 -p 6379
127.0.0.1:6379> auth "DtJwUZaKl"
OK

string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
127.0.0.1:6379[1]> set key1 value1
OK
127.0.0.1:6379[1]> keys *
1) "key1"
127.0.0.1:6379[1]> set key2 value2
OK
127.0.0.1:6379[1]> keys *
1) "key1"
2) "key2"
127.0.0.1:6379[1]> set key3 value3 key4 value4
(error) ERR syntax error
127.0.0.1:6379[1]> keys *
1) "key1"
2) "key2"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
127.0.0.1:6379[1]> HMSET key1_hash value1_hash
(error) ERR wrong number of arguments for 'hmset' command
127.0.0.1:6379[1]> hmset key1_hash field1 hello filed2 world
OK
127.0.0.1:6379[1]> hget key1_hash filed1
(nil)
127.0.0.1:6379[1]> hget key1_hash field1
"hello"
127.0.0.1:6379[1]> hget key1_hash field2
(nil)
127.0.0.1:6379[1]> hget key1_hash filed2
"world"
127.0.0.1:6379[1]> hmset key1 field1 value1 field2 vaule2
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379[1]> hmset key2_hash field1 value1 field2 vaule2
OK
127.0.0.1:6379[1]> hget key2_hash field1
"value1"
127.0.0.1:6379[1]> hget key2_hash field2
"vaule2"
127.0.0.1:6379[1]> keys *
1) "key2_hash"
2) "key1_hash"
3) "key1"
4) "key2"
127.0.0.1:6379[1]> del key2_hash field1
(integer) 1
127.0.0.1:6379[1]> hget key2_hash field1
(nil)
127.0.0.1:6379[1]> hget key2_hash field2
(nil)
127.0.0.1:6379[1]> hget key1_hash fiel1
(nil)
127.0.0.1:6379[1]> hget key1_hash field1
"hello"
127.0.0.1:6379[1]> hget key1_hash filed2
"world"
127.0.0.1:6379[1]> del key1_hash filed2
(integer) 1
127.0.0.1:6379[1]> hget key1_hash filed1
(nil)
127.0.0.1:6379[1]> hget key1_hash fiield1
(nil)
127.0.0.1:6379[1]> hget key1_hash field1
(nil)
127.0.0.1:6379[1]> keys *
1) "key1"
2) "key2"
127.0.0.1:6379[1]> hset key1_hash field1 value1 field2 value2
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379[1]> hmset key1_hash field1 value1 field2 value2
OK
127.0.0.1:6379[1]> keys *
1) "key1_hash"
2) "key1"
3) "key2"
127.0.0.1:6379[1]> hget key1_hash
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379[1]> hget key1_hash field1
"value1"
127.0.0.1:6379[1]> hget key1_hash field2
"value2"
127.0.0.1:6379[1]> keys *
1) "key1_hash"
2) "key1"
3) "key2"
127.0.0.1:6379[1]> del key1_hash field1
(integer) 1
127.0.0.1:6379[1]> keys *
1) "key1"
2) "key2"
127.0.0.1:6379[1]> hmset key1_hash field1 value1 field2 value2
OK
127.0.0.1:6379[1]> keys *
1) "key1_hash"
2) "key1"
3) "key2"
127.0.0.1:6379[1]> del key1_hash
(integer) 1
127.0.0.1:6379[1]> keys *
1) "key1"
2) "key2"
127.0.0.1:6379[1]> lpush key1_list value1 value2 value3
(integer) 3
127.0.0.1:6379[1]> keys *
1) "key1_list"
2) "key1"
3) "key2"
127.0.0.1:6379[1]> lrange key1_list
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379[1]> lrange key1_list 0 10
1) "value3"
2) "value2"
3) "value1"
127.0.0.1:6379[1]> lpush key1_list value4
(integer) 4
127.0.0.1:6379[1]> lrange key1_list 0 10
1) "value4"
2) "value3"
3) "value2"
4) "value1"
127.0.0.1:6379[1]> lpush key2_list value1 value2 value3
(integer) 3
127.0.0.1:6379[1]> lrange key2_lsit
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379[1]> lrange key2_list
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379[1]> lrange key2_list 0 10
1) "value3"
2) "value2"
3) "value1"
127.0.0.1:6379[1]> keys *
1) "key1_list"
2) "key1"
3) "key2_list"
4) "key2"
127.0.0.1:6379[1]> del key2_list value
(integer) 1
127.0.0.1:6379[1]> keys *
1) "key1_list"
2) "key1"
3) "key2"
127.0.0.1:6379[1]> sadd key1_set value
(integer) 1
127.0.0.1:6379[1]> keys *
1) "key1_list"
2) "key1"
3) "key1_set"
4) "key2"
127.0.0.1:6379[1]> semebers key1_set
(error) ERR unknown command 'semebers'
127.0.0.1:6379[1]> smembers key1_set
1) "value"
127.0.0.1:6379[1]> sadd key1_set value2
(integer) 1
127.0.0.1:6379[1]> smembers key1_set
1) "value2"
2) "value"
127.0.0.1:6379[1]> sadd key1_set value3 value4
(integer) 2
127.0.0.1:6379[1]> smembers key1_set
1) "value4"
2) "value3"
3) "value2"
4) "value"
127.0.0.1:6379[1]> sadd key1_set value value1 value2
(integer) 1
127.0.0.1:6379[1]> smembers key1_set
1) "value4"
2) "value2"
3) "value3"
4) "value"
5) "value1"
127.0.0.1:6379[1]> zadd key1_zset 0 redis
(integer) 1
127.0.0.1:6379[1]> keys *
1) "key2"
2) "key1_set"
3) "key1_zset"
4) "key1_list"
5) "key1"
127.0.0.1:6379[1]> zadd key1_zset 0 mysql
(integer) 1
127.0.0.1:6379[1]> keys *
1) "key2"
2) "key1_set"
3) "key1_zset"
4) "key1_list"
5) "key1"
127.0.0.1:6379[1]> zrangetbyscore key1_set 0 100
(error) ERR unknown command 'zrangetbyscore'
127.0.0.1:6379[1]> zrangetyscore key1_set 0 100
(error) ERR unknown command 'zrangetyscore'
127.0.0.1:6379[1]> zrangebyscore key1_set 0 100
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379[1]> zrangebyscore key1_zset 0 100
1) "mysql"
2) "redis"
127.0.0.1:6379[1]> zadd key1_zset 1 neo4j
(integer) 1
127.0.0.1:6379[1]> zadd key1_zset 1 orientdb
(integer) 1
127.0.0.1:6379[1]> zrangebyscore key1_zset
(error) ERR wrong number of arguments for 'zrangebyscore' command
127.0.0.1:6379[1]> zrangebyscore key1_zset 0 100
1) "mysql"
2) "redis"
3) "neo4j"
4) "orientdb"
127.0.0.1:6379[1]>
127.0.0.1:6379[1]>

References

[1] https://redis.io/
[2] https://redis.io/download
[3] http://blog.csdn.net/renfufei/article/details/38474435
[4] http://keenwon.com/1275.html
[5] http://blog.csdn.net/renfufei/article/details/52876083
[6] Redis常用命令
[7] 2018整理最全的50道Redis面试题
[8] Mac环境下安装Redis
[9] mac安装redis