Ubunt安裝Redis,使用內建客戶端進行Redis資料操作
阿新 • • 發佈:2018-11-24
安裝
在終端使用apt-cache policy redis-server可以檢視當前apt-cache的redis版本為4.0.9
可以通過sudo apt-get install redis-server安裝4.0.9版本的redis。
也可以在官網https://redis.io/download檢視最新版本的安裝方式:
發現需要從原始碼編譯安裝,所以需要編譯安裝環境build-essential 。為了測試編譯產生的二進位制檔案,同時需要安裝 tcl 包。
$ sudo apt-get update
$ sudo apt-get install make
$ sudo apt-get install build-essential tcl
$ sudo make
$ sudo make install
閱讀官方文件,編譯後的二進位制檔案redis-server在src目錄中。
$ src/redis-server
可以看見Redis安裝成功。
使用內建客戶端進行Redis資料操作
進入內建客戶端:
$redis-cli
1. string型別:
[email protected]:~$ redis-cli 127.0.0.1:6379> set myfirstkey "Welcome to the big data world." OK 127.0.0.1:6379> get myfirstkey "Welcome to the big data world." 127.0.0.1:6379> set userid 1000 OK 127.0.0.1:6379> incr userid (integer) 1001 127.0.0.1:6379> incr userid (integer) 1002 127.0.0.1:6379> 127.0.0.1:6379> incrby userid 100 (integer) 1102
2. list型別
127.0.0.1:6379> rpush messages "Done." (integer) 1 127.0.0.1:6379> rpush messages "Great,thank you." (integer) 2 127.0.0.1:6379> lpush messages "What's going on with the project?" (integer) 3 127.0.0.1:6379> lrange messages 0 -1 1) "What's going on with the project?" 2) "Done." 3) "Great,thank you."
3. set型別
127.0.0.1:6379> sadd shape "triangle"
(integer) 1
127.0.0.1:6379> sadd shape "rectangle"
(integer) 1
127.0.0.1:6379> sadd shape "circle"
(integer) 1
127.0.0.1:6379> smembers shape
1) "triangle"
2) "rectangle"
3) "circle"
127.0.0.1:6379> sismember shape "circle"
(integer) 1
127.0.0.1:6379> sismember shape "square"
(integer) 0
4 . sorted set型別
127.0.0.1:6379> zadd workers 15 "Thomas"
(integer) 1
127.0.0.1:6379> zadd workers 20 "Johnson"
(integer) 1
127.0.0.1:6379> zadd workers 5 "Michelle"
(integer) 1
127.0.0.1:6379> zrange workers 0 -
(error) ERR value is not an integer or out of range
127.0.0.1:6379> zrange workers 0 -1
1) "Michelle"
2) "Thomas"
3) "Johnson"
127.0.0.1:6379> zrevrange workers 0 -1
1) "Johnson"
2) "Thomas"
3) "Michelle"
5. hash型別
127.0.0.1:6379> hset users:wisdomlife password 123
(integer) 1
127.0.0.1:6379> hget users:wisdomlife password
"123"
127.0.0.1:6379> hmset users:wisdomlife password 123 age 28
OK
127.0.0.1:6379> hmget users:wisdomlife password age
1) "123"
2) "28"
127.0.0.1:6379> hgetall users:wisdomlife
1) "password"
2) "123"
3) "age"
4) "28"