Redis 使用
(1) 下载安装
(1.1) Linux/Unix
Redis对于Linux是官方支持的,普通安装、使用按照官方指导,5分钟以内就能搞定。详情请参考:Redis官网下载链接 http://redis.io/download
下载安装
wget http://download.redis.io/releases/redis-6.0.5.tar.gz
tar xzf redis-6.0.5.tar.gz
cd redis-6.0.5
sudo make
启动Redis Server
./src/redis-server redis.conf
** 启动Redis Client **
ZBMAC-C02PGMT0F:redis-6.0.5 weikeqin1$ ./src/redis-cli
127.0.0.1:6379>
(1.2) Windows
Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,项目地址是:项目地址
打开以后,可以直接使用浏览器下载,或者Git克隆。
https://github.com/MicrosoftArchive/redis/releases
在 Release 页面中,可以找到 msi 安装文件以及 .zip 文件
下载解压,在解压后的bin目录下有以下这些文件:
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
redis-benchmark.exe #基准测试
redis-check-aof.exe # aof
redis-check-dump.exe # dump
redis-cli.exe # 客户端
redis-server.exe # 服务器
redis.windows.conf # 配置文件
如果你安装的windows没有Administrator账户,请参考这篇文章: Windows 7 启用超级管理员Administrator账户的N种方法
启动脚本如下:
redis-server redis.windows.conf
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命令:
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
可能遇到的问题
sudo make test 时 [exception]: Executing test client: couldn’t execute “src/redis-benchmark”: no such file or directory.
[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
忽略这个错误,对安装没影响,安装完可以用
you need tcl 8.5
you need tcl 8.5
(2) redis基本命令使用
连接数据库
查看数据库信息
不同数据类型的使用
(2.1) 连接
./src/redis-cli -h host -p port -a password
默认16个数据库:0-15,进入redis后默认是0库。不建议使用多个数据库
λ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> auth "DtJwUZaKl"
OK
(2.2) 查看数据库信息
127.0.0.1:6379> info
127.0.0.1:6379> dbsize
(integer) 21
127.0.0.1:6379>
(2.3) string
get key
127.0.0.1:6379>
127.0.0.1:6379> get mykey
(nil)
127.0.0.1:6379> set mykey myvalue
OK
127.0.0.1:6379> get mykey
"myvalue"
127.0.0.1:6379>
可以看到key不存在时返回nil
key存在时返回对应的value
set key
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"
set key ex second
127.0.0.1:6379> set key2 value2 ex 10
OK
127.0.0.1:6379> get key2
"value2"
ten seconds ago
127.0.0.1:6379> get key2
(nil)
127.0.0.1:6379>
可以看到刚设置完 key2是可以查到的,10s后查到的结果为nil
set key px milliseconds
127.0.0.1:6379> set key3 value3 px 10000
OK
127.0.0.1:6379> ttl key3
(integer) 7
127.0.0.1:6379>
127.0.0.1:6379> ttl key3
(integer) 4
127.0.0.1:6379>
127.0.0.1:6379> get key3
(nil)
127.0.0.1:6379>
setnx key value
getrange key start end
127.0.0.1:6379>
127.0.0.1:6379> set key4 "hello redis i am coming"
OK
127.0.0.1:6379>
127.0.0.1:6379> getrange key4 0 3
"hell"
127.0.0.1:6379> getrange key4 0 100
"hello redis i am coming"
127.0.0.1:6379>
127.0.0.1:6379> getrange key4 -3 0
""
127.0.0.1:6379> getrange key4 -3 100
"ing"
127.0.0.1:6379>
127.0.0.1:6379> getrange key4 0 -1
"hello redis i am coming"
127.0.0.1:6379>
127.0.0.1:6379> getrange key4 10 100
"s i am coming"
127.0.0.1:6379>
strlen key
127.0.0.1:6379> set key8 a
OK
127.0.0.1:6379>
127.0.0.1:6379> strlen key8
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> set key9 1
OK
127.0.0.1:6379> strlen key9
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> set key10 中国
OK
127.0.0.1:6379> get key10
"\xe4\xb8\xad\xe5\x9b\xbd"
127.0.0.1:6379>
127.0.0.1:6379> strlen key10
(integer) 6
127.0.0.1:6379>
append key value
127.0.0.1:6379> set key11 value11
OK
127.0.0.1:6379> get key11
"value11"
127.0.0.1:6379> strlen key11
(integer) 7
127.0.0.1:6379>
127.0.0.1:6379> append key11 hello
(integer) 12
127.0.0.1:6379> strlen key11
(integer) 12
127.0.0.1:6379>
decr key
127.0.0.1:6379>
127.0.0.1:6379> set key19 "19"
OK
127.0.0.1:6379> decr key19
(integer) 18
127.0.0.1:6379>
127.0.0.1:6379> set key20 20
OK
127.0.0.1:6379> decr key20
(integer) 19
127.0.0.1:6379>
127.0.0.1:6379> decr key20
(integer) 18
127.0.0.1:6379>
127.0.0.1:6379> set key21 12345678901234567890
OK
127.0.0.1:6379> decr key21
(error) ERR value is not an integer or out of range
127.0.0.1:6379>
decrby key
127.0.0.1:6379> set key21 21
OK
127.0.0.1:6379> decrby key21 5
(integer) 16
127.0.0.1:6379> decrby key21 6
(integer) 10
127.0.0.1:6379>
incr key
127.0.0.1:6379> set key26 26
OK
127.0.0.1:6379> incr key26
(integer) 27
127.0.0.1:6379>
127.0.0.1:6379> incr key26
(integer) 28
127.0.0.1:6379>
incrby key
127.0.0.1:6379> set key27 27
OK
127.0.0.1:6379> incrby key27 10
(integer) 37
127.0.0.1:6379>
incrbufloat key
127.0.0.1:6379> set key30 20
OK
127.0.0.1:6379> incrbyfloat key30 0.1
"20.1"
127.0.0.1:6379>
127.0.0.1:6379> incrbyfloat key30 -5
"15.1"
127.0.0.1:6379> incrbyfloat key30 2.0e2
"215.10000000000000001"
127.0.0.1:6379>
127.0.0.1:6379> set key31 5.0e3
OK
127.0.0.1:6379> incrbyfloat key31 2.0e2
"5200"
127.0.0.1:6379>
mget key [key…]
127.0.0.1:6379>
127.0.0.1:6379> set key100 100
OK
127.0.0.1:6379> set key101 101
OK
127.0.0.1:6379> mget key100 key101
1) "100"
2) "101"
127.0.0.1:6379>
127.0.0.1:6379> mget key100 key101 t
1) "100"
2) "101"
3) (nil)
127.0.0.1:6379>
mset key value [key value…]
127.0.0.1:6379> mset key110 110 key111 111 key112 112
OK
127.0.0.1:6379> get key110
"110"
127.0.0.1:6379> get key111
"111"
127.0.0.1:6379> get key112
"112"
127.0.0.1:6379> mget key110 key111 key112
1) "110"
2) "111"
3) "112"
127.0.0.1:6379>
setbit key offset value
setbit是操作位运算的
二进制 十进制 十六进制 图形
0100 0000 64 40 @
0100 0001 65 41 A
0100 0010 66 42 B
0100 0011 67 43 C
0100 0100 68 44 D
0110 0000 96 60 `
0110 0001 97 61 a
0110 0010 98 62 b
0110 0011 99 63 c
127.0.0.1:6379> setbit mybit 1 1
(integer) 0
127.0.0.1:6379> get mybit
"@"
127.0.0.1:6379>
127.0.0.1:6379> setbit mybit 2 1
(integer) 0
127.0.0.1:6379> get mybit
"`"
127.0.0.1:6379>
127.0.0.1:6379> setbit mybit 7 1
(integer) 0
127.0.0.1:6379> get mybit
"a"
127.0.0.1:6379>
(2.4) list
lpush key element [element…]
往列表头插入数据
Insert all the specified values at the head of the list stored at key.
Time complexity: O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments.
127.0.0.1:6379> lpush list1 hello
(integer) 1
127.0.0.1:6379> lpush list1 redis
(integer) 2
127.0.0.1:6379>
127.0.0.1:6379> lrange list1 0 -1
1) "redis"
2) "hello"
127.0.0.1:6379>
127.0.0.1:6379> lpush list1 world
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
1) "world"
2) "redis"
3) "hello"
127.0.0.1:6379>
rpush key element [element…]
127.0.0.1:6379> rpush list2 hello
(integer) 1
127.0.0.1:6379> rpush list2 redis
(integer) 2
127.0.0.1:6379> rpush list2 world
(integer) 3
127.0.0.1:6379>
127.0.0.1:6379> lrange list2 0 -1
1) "hello"
2) "redis"
3) "world"
127.0.0.1:6379>
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] redis-documentation
[2] redis-commands
[3] Windows下安装并设置Redis
[4] 在windows上部署使用Redis
[5] Linux安装Redis单机版
[6] Redis常用命令
[7] 2018整理最全的50道Redis面试题
[8] Mac环境下安装Redis
[9] mac安装redis
[10] Redis如何通过本机客户端访问远程服务器端