Redis-初探

官網 http://redis.io
中文網 http://redis.cn
命令參考 http://redisdoc.cn

Redis(Remote Dictionary Server)是一個開源的key-value內存存儲的NoSQL數據庫,具有非常強悍的讀寫性能,現階段正越來越多地被用于互聯網高并發場景。

1 安裝

1.1 Linux(CentOS 7.4)下yum方式安裝
yum install redis
1.2 手動編譯安裝

下載安裝redis 4.0.9至/usr/local/src目錄

wget -P /usr/local/src http://download.redis.io/releases/redis-4.0.9.tar.gz

解壓至/opt/redis目錄

mkdir /opt/reids && tar xzvf /usr/local/src/redis-4.0.9.tar.gz -C /opt/redis

編譯安裝(需要依賴gcc)

cd /opt/redis/redis-4.0.9 && make MALLOC=libc

復制相關運行文件到/usr/local/bin目錄

make install

[可選]安裝后的測試(需要依賴tcl 8.5+)

make test

2 啟動

2.1 交互模式啟動
[root@VM_0_171_centos ~]# redis-server
23173:C 26 Mar 16:13:02.365 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 23173
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

23173:M 26 Mar 16:13:02.366 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
23173:M 26 Mar 16:13:02.366 # Server started, Redis version 3.2.3
23173:M 26 Mar 16:13:02.366 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
23173:M 26 Mar 16:13:02.366 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
23173:M 26 Mar 16:13:02.366 * DB loaded from disk: 0.000 seconds
23173:M 26 Mar 16:13:02.366 * The server is now ready to accept connections on port 6379

2.2 后臺啟動

step 1:需要修改配置文件/etc/redis.conf

# By default Redis does not run as a daemon. Use 'yes' if you need it.
daemonize yes

step 2:使用配置文件啟動

[root@VM_0_171_centos ~]# redis-server /etc/redis.conf
[root@VM_0_171_centos ~]#

3 客戶端連接

[root@VM_0_171_centos ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

4 停止服務

[root@VM_0_171_centos ~]# redis-shutdown
[root@VM_0_171_centos ~]#

5 其它程序

redis通過yum方式安裝后,所有程序均位于/usr/bin目錄

[root@VM_0_171_centos ~]# ls -l /usr/bin | grep redis
-rwxr-xr-x  1 root root      86832 8月   5 2016 redis-benchmark
-rwxr-xr-x  1 root root      15408 8月   5 2016 redis-check-aof
-rwxr-xr-x  1 root root     975248 8月   5 2016 redis-check-rdb
-rwxr-xr-x  1 root root     173344 8月   5 2016 redis-cli
lrwxrwxrwx  1 root root         12 3月  25 23:41 redis-sentinel -> redis-server
-rwxr-xr-x  1 root root     975248 8月   5 2016 redis-server
-rwxr-xr-x  1 root root        918 8月   5 2016 redis-shutdown
[root@VM_0_171_centos ~]#

其中redis-benchmark可以用來測試redis的性能,官網給出的信息是redis的讀速度:110000次/s,寫速度:81000次/s

[root@VM_0_171_centos ~]# redis-benchmark
====== PING_INLINE ======
  100000 requests completed in 0.69 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
145137.88 requests per second

====== PING_BULK ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153846.16 requests per second

====== SET ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.97% <= 1 milliseconds
100.00% <= 1 milliseconds
153609.83 requests per second

====== GET ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
156250.00 requests per second

====== INCR ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
156006.25 requests per second

====== LPUSH ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153846.16 requests per second

====== RPUSH ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
156985.86 requests per second

====== LPOP ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153374.23 requests per second

====== RPOP ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153374.23 requests per second

====== SADD ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
152671.77 requests per second

====== SPOP ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
154083.20 requests per second

====== LPUSH (needed to benchmark LRANGE) ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153846.16 requests per second

====== LRANGE_100 (first 100 elements) ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.97% <= 1 milliseconds
100.00% <= 1 milliseconds
155279.50 requests per second

====== LRANGE_300 (first 300 elements) ======
  100000 requests completed in 0.64 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.99% <= 1 milliseconds
100.00% <= 1 milliseconds
155763.23 requests per second

====== LRANGE_500 (first 450 elements) ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
154320.98 requests per second

====== LRANGE_600 (first 600 elements) ======
  100000 requests completed in 0.65 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
153374.23 requests per second

====== MSET (10 keys) ======
  100000 requests completed in 0.63 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
159744.41 requests per second


[root@VM_0_171_centos ~]#

6 配置文件

通過yum方式安裝后,redis的配置文件的位置為:/etc/redis.conf,包括前面提到的后臺啟動也是需要通過修改配置文件實現,配置項比較多,需要對redis有進一步了解

6.1 設置密碼

如果要限制連接redis必須使用密碼,需要配置requirepass選項:requriepass <password>

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass 123456

設置密碼后需要重啟服務,再連接redis-cli時會提示需要驗證授權,通過auth <password>命令驗證

[root@VM_0_171_centos ~]# vim /etc/redis.conf
[root@VM_0_171_centos ~]# redis-server /etc/redis.conf
[root@VM_0_171_centos ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

7 幫助

客戶端連接redis后,可以通過help獲取幫助,幫助信息里面給出了3種不同方式獲取幫助,具體用法其它章節會進一步描述

127.0.0.1:6379> help
redis-cli 3.2.3
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

8.常用命令

8.1查找匹配的key(支持通配符?*)

keys pattern

8.2鍵的總數

dbsize

8.3檢查鍵是否存在

exists key

8.4刪除鍵

del key

8.5設置鍵過期時間

expire key seconds 

8.6查看鍵剩余過期時間

# 大于等于0的整數:鍵剩余的過期時間
# -1:鍵未設置過期時間
# -2:鍵不存在
ttl key

8.7鍵的數據類型

type key
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,739評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,634評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,653評論 0 377
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,063評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,835評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,235評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,315評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,459評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,000評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,819評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,004評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,560評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,257評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,676評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,937評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,717評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,003評論 2 374