Redis筆記-單機版安裝

一、幾個相關概念redis

概念 現象描述 規避措施
穿透 經過訪問一個緩存中不存在的key,致使程序必定要在數據庫中執行查詢 將訪問結果進行處理,若是返回是null,也存儲在緩存中,能夠將過時時間設置較短
雪崩 某一個時間段內,大量緩存key同時失效,致使訪問直接查詢數據庫 講不一樣的緩存數據進行分級分類,並進行不通的過時時間設置,例如能夠經過定值+隨機的方式生成過時時間,防止集中失效的的狀況
擊穿 緩存中某個key被訪問的頻率極高,在緩存失效的瞬間,訪問直接訪問數據庫,好比商城的爆款等 能夠將對應的緩存過時時間設置較大甚至是永不過時來規避

 

 

 

 

 

 

 

 

 

 

二、Linux安裝(以CentOS 7.X爲例)數據庫

step一、到官網下載對應的tar安裝包http://download.redis.io/releases截止目前,最新版本已經發布到5.Xcentos

  

  下載:wget http://download.redis.io/releases/redis-5.0.4.tar.gz緩存

  解壓:tar zxvf redis-5.0.4.tar.gz多線程

step二、編譯 app

  cd redis-5.0.4   而後執行make 測試

  編譯完進入src目錄,能夠看到生成對應的文件以下:this

[root@VM_0_14_centos redis-5.0.4]# ll
total 264
-rw-rw-r--  1 root root 99445 Mar 19 00:21 00-RELEASENOTES
-rw-rw-r--  1 root root    53 Mar 19 00:21 BUGS
-rw-rw-r--  1 root root  1894 Mar 19 00:21 CONTRIBUTING
-rw-rw-r--  1 root root  1487 Mar 19 00:21 COPYING
drwxrwxr-x  6 root root  4096 Mar 20 16:00 deps
-rw-rw-r--  1 root root    11 Mar 19 00:21 INSTALL
-rw-rw-r--  1 root root   151 Mar 19 00:21 Makefile
-rw-rw-r--  1 root root  4223 Mar 19 00:21 MANIFESTO
-rw-rw-r--  1 root root 20555 Mar 19 00:21 README.md
-rw-rw-r--  1 root root 62155 Mar 19 00:21 redis.conf
-rwxrwxr-x  1 root root   275 Mar 19 00:21 runtest
-rwxrwxr-x  1 root root   280 Mar 19 00:21 runtest-cluster
-rwxrwxr-x  1 root root   281 Mar 19 00:21 runtest-sentinel
-rw-rw-r--  1 root root  9710 Mar 19 00:21 sentinel.conf
drwxrwxr-x  3 root root  4096 Mar 20 16:01 src
drwxrwxr-x 10 root root  4096 Mar 19 00:21 tests
drwxrwxr-x  8 root root  4096 Mar 19 00:21 utils
[root@VM_0_14_centos redis-5.0.4]# 
[root@VM_0_14_centos redis-5.0.4]# 
[root@VM_0_14_centos redis-5.0.4]# 
[root@VM_0_14_centos redis-5.0.4]# 
[root@VM_0_14_centos redis-5.0.4]# 
[root@VM_0_14_centos redis-5.0.4]# cd src
[root@VM_0_14_centos src]# ls -lrt redis-*
-rwxr-xr-x 1 root root 8100759 Mar 20 16:01 redis-server
-rwxr-xr-x 1 root root 8100759 Mar 20 16:01 redis-sentinel
-rwxr-xr-x 1 root root 4805624 Mar 20 16:01 redis-cli
-rwxr-xr-x 1 root root 4365280 Mar 20 16:01 redis-benchmark
-rwxr-xr-x 1 root root 8100759 Mar 20 16:01 redis-check-rdb
-rwxr-xr-x 1 root root 8100759 Mar 20 16:01 redis-check-aof

step三、新建redis目錄/usr/redis,並將文件copy進去(目前以單節點測試,先能夠不用copy其餘文件)spa

1 [root@VM_0_14_centos /]# mkdir /usr/redis
2 [root@VM_0_14_centos /]# cp /data01/redis-5.0.4/redis.conf /usr/redis/
3 [root@VM_0_14_centos /]# cp /data01/redis-5.0.4/src/redis-server /usr/redis/
4 [root@VM_0_14_centos /]# cp /data01/redis-5.0.4/src/redis-cli /usr/redis/
5 [root@VM_0_14_centos /]# 

step四、啓動測試 ./redis-server redis.conf   線程

 1 [root@VM_0_14_centos redis]# 
 2 [root@VM_0_14_centos redis]# ./redis-server redis.conf 
 3 6440:C 20 Mar 2019 16:16:13.047 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
 4 6440:C 20 Mar 2019 16:16:13.047 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=6440, just started
 5 6440:C 20 Mar 2019 16:16:13.047 # Configuration loaded
 6                 _._                                                  
 7            _.-``__ ''-._                                             
 8       _.-``    `.  `_.  ''-._           Redis 5.0.4 (00000000/0) 64 bit
 9   .-`` .-```.  ```\/    _.,_ ''-._                                   
10  (    '      ,       .-`  | `,    )     Running in standalone mode
11  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
12  |    `-._   `._    /     _.-'    |     PID: 6440
13   `-._    `-._  `-./  _.-'    _.-'                                   
14  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
15  |    `-._`-._        _.-'_.-'    |           http://redis.io        
16   `-._    `-._`-.__.-'_.-'    _.-'                                   
17  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
18  |    `-._`-._        _.-'_.-'    |                                  
19   `-._    `-._`-.__.-'_.-'    _.-'                                   
20       `-._    `-.__.-'    _.-'                                       
21           `-._        _.-'                                           
22               `-.__.-'                                               
23 
24 6440:M 20 Mar 2019 16:16:13.048 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
25 6440:M 20 Mar 2019 16:16:13.048 # Server initialized
26 6440:M 20 Mar 2019 16:16:13.048 # 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.
27 6440:M 20 Mar 2019 16:16:13.048 # 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.
28 6440:M 20 Mar 2019 16:16:13.048 * Ready to accept connections

開啓客戶端,並測試:

1 [root@VM_0_14_centos redis]# 
2 [root@VM_0_14_centos redis]# ./redis-cli 
3 127.0.0.1:6379> 
4 127.0.0.1:6379> 
5 127.0.0.1:6379> set name hello
6 OK
7 127.0.0.1:6379> get name
8 "hello"
9 127.0.0.1:6379> 

OK,顯示成功。

重啓:./redis-cli -h 127.0.0.1 -p 6379 shutdown  或  kill -9  進程號

 

三、Redis數據備份模式

  Redis支持RDB和AOF兩種方式實現數據備份,能夠在重啓後實現數據恢復。關於RDB和AOF說明以下:

  RDB:redis默認開啓的數據備份方式,會將數據定時dump到磁盤上,缺點是可能在定時間隔時間內斷電,數據未徹底寫入磁盤,致使數據部分丟失

  AOF:經過記錄操做日誌追加操做日誌的方式寫入文件,在數據恢復時執行日誌分析實現數據恢復,缺點是在大量數據備份時效率略低於RDB

關於RDB和AOF的持久化配置能夠參考redis的配置文件redis.conf配置項:

  RDB持久化配置:  save m n  表示在m秒後,若是有n個key發生變化,則dump內存快照

  

  AOF持久化配置:

  

  appendfsync always     #每次有數據修改發生時都會寫入AOF文件。

       appendfsync everysec  #每秒鐘同步一次,該策略爲AOF的缺省策略。

       appendfsync no            #從不一樣步,高效可是數據不會被持久化。

  說明:AOF默認是沒開啓的,須要將appendonly no的值設置爲yes,重啓後生效

修改後重啓:

  

  測試,set一條記錄:

  

  而後查看appendonly.aof文件,set操做記錄已被追加到aof文件:

  

 

四、開啓遠程訪問

      配置protected-mode no (redis 3.2版本之後),註釋掉bind 地址便可。

五、開啓守護進程

  redis採用的是單進程多線程的模式,當redis.conf中選項daemonize設置成yes時,表明開啓守護進程模式。在該模式下,redis會在後臺運行,並將進程pid號寫入至redis.conf選項pidfile設置的文件中,此時redis將一直在後臺運行

 

關於Redis集羣模式下節再接着說。 

相關文章
相關標籤/搜索