redis安裝及簡單應用


1.系統環境:centos7.2 —— x86_64

[root@salt1 ~]# cat /etc/centos-release

CentOS Linux release 7.2.1511 (Core) 

[root@salt1 ~]# uname -a

Linux salt1 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux



2.安裝redis

先決條件:首先安裝epel-release(epel源)

[root@salt1 ~]# yum -y install epel-release

然後安裝redis

[root@salt1 ~]# yum -y install redis

[root@salt1 ~]# rpm -q redis

redis-2.8.19-2.el7.x86_64

[root@salt1 ~]# redis-

redis-benchmark   redis-check-aof   redis-check-dump  redis-cli         redis-sentinel    redis-server      redis-shutdown 


redis-server:redis服務器的daemon啓動程序

redis-cli: redis命令行客戶端操作工具,當讓也可以用telnet根據其純文本協議來操作;

redis-benchmark:redis性能測試工具,測試redis在你的系統及你的配置下的讀寫性能;

redis-check-aof:對於更新日誌appendonly.conf檢查,是否可用,類似檢查mysql binlog的工具;

redis-check-dump:用於本地數據庫rdb文件的檢查;



3.啓動redis服務

先備份一下redis的配置文件(個人習慣,先備份再操作)

[root@salt1 ~]# cp /etc/redis.conf{,.bak}

redis啓動時最好指定其配置文件,因爲redis幾乎所有的控制都在其配置文件;

redis-server :     redis服務器的daemon啓動程序

/etc/redis.conf :    redis的配置文件

& :           後臺啓動

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

[1] 9462

[root@salt1 ~]# ps -ef | grep redis

root       9462   2767  0 03:59 pts/0    00:00:00 redis-server 127.0.0.1:6379

root       9466   2767  0 03:59 pts/0    00:00:00 grep --color=auto redis

然後再看下日誌

[root@salt1 ~]# tail -30 /var/log/redis/redis.log 

[9462] 11 Aug 03:59:54.037 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                  

           _.-``__ ''-._                                             

      _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                   

 (    '      ,       .-`  | `,    )     Running in stand alone mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379

 |    `-._   `._    /     _.-'    |     PID: 9462

  `-._    `-._  `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io        

  `-._    `-._`-.__.-'_.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                  

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                       

          `-._        _.-'                                           

              `-.__.-'                                               

[9462] 11 Aug 03:59:54.038 # Server started, Redis version 2.8.19

[9462] 11 Aug 03:59:54.039 # 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.

[9462] 11 Aug 03:59:54.039 # 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.

[9462] 11 Aug 03:59:54.040 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

[9462] 11 Aug 03:59:54.040 * The server is now ready to accept connections on port 6379


黃色字體爲:問題

紅色字體爲:解決辦法



黃色字體解釋:

1.後臺保存在低內存下可能會失敗

vm.overcommit_memory 參數

默認值爲0

0:當用戶空間請求更多的內存時,內核嘗試估算出剩餘可用的內存;

1:內核允許超量使用內存,直到用完爲止;主要用於科學計算;

2:內核會使用一個絕不過量使用內存的算法,即系統整個內存地址空間不能超過swap+50%的ram值,50%參數的設定是在overcommit_ratio中設定;

2.啓用了THP(頁面內存透明化)在你的內核具體解釋看 後邊的網址,裏頭解釋的更詳細 http://os.51cto.com/art/201103/249821.htm

3.tcp backlog 設置爲511 不能執行,原因是/proc/sys/net/core/somaxconn的值較低

backlog 是網絡連接過程中,某種狀態的隊列長度,如果併發高,那麼會導致backlog的隊列被佔滿,服務器就會丟掉傳進來的其他連接,然後就會出現客戶點連接失敗的情形;

http://jingyan.baidu.com/article/84b4f565e60f8560f6da3227.html


所以底下就開始執行對應的操作

注意:藍色字體爲需要注意的地方

echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl -p 
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
echo "echo 511 > /proc/sys/net/core/somaxconn" >> /etc/rc.local


下面是一個截圖,需要對比的夥伴可以對一下

wKioL1esWQiSVojLAAAVz69F0L0158.png-wh_50


相信經過上面3行的配置,你的redis不會再報類似的警告了,當然你還需要在redis.conf文件中指定redis允許使用的內存大小(下章介紹),否則你的服務器撐不了兩天



4.關閉redis服務

1)redis-shutdown #默認會保存後關閉

2)redis-cli shutdown save #



5.連接redis

[root@salt1 ~]# redis-cli

127.0.0.1:6379>



6.簡單操作

127.0.0.1:6379> set id 001 #創建key-vlaue

OK

127.0.0.1:6379> get id #查找key

"001"

127.0.0.1:6379> del id #刪除key

(integer) 1

127.0.0.1:6379> get id

(nil)

127.0.0.1:6379> exists id #查詢id是否存在

(integer) 1

127.0.0.1:6379> del id

(integer) 1

127.0.0.1:6379> exists id

(integer) 0

127.0.0.1:6379> keys * #獲取所有key

127.0.0.1:6379> set k1 v1

OK

127.0.0.1:6379> set k2 v2

OK

127.0.0.1:6379> set k3 v3

OK

127.0.0.1:6379> keys * #獲取所有key

1) "k2"

2) "k3"

3) "k1"

127.0.0.1:6379> dbsize #獲取所有key-value數

(integer) 3



redis默認有16個庫,但是無法查看到(庫的多少可以在redis.conf中配置)

127.0.0.1:6379> select 1 #切換到第二個庫(從0開始數)

OK

127.0.0.1:6379[1]> keys *

(empty list or set)

127.0.0.1:6379[1]> set name 123

OK

127.0.0.1:6379[1]> keys *

1) "name"

127.0.0.1:6379[1]> select 0

OK

127.0.0.1:6379> keys *

1) "k2"

2) "k3"

3) "k1"

127.0.0.1:6379> select 15

OK

127.0.0.1:6379> select 16

(error) ERR invalid DB index


好了redis的安裝就先說到這兒了


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章