1 環境準備
#官網下載 redis 3.2.5版本 wget http://download.redis.io/releases/redis-4.0.1.tar.gz #安裝 C 編譯環境 yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc gcc-c++ 2 安裝 解壓安裝包後,進入文件目錄編譯,編譯結束時,會提示 Hint: It's a good idea to run 'make test' ,建議在安裝前先測試預安裝下,make test預安裝後,遇到錯誤:You need tcl 8.5 or newer in order to run the Redis test ,缺失安裝包tcl,因此須要先安裝這個 安裝包後再次運行 make test,正常後再進行redis安裝。 詳細步驟以下: #解壓二進制包 tar -zvxf /opt/redis-3.2.5 #進入到文件目錄 cd redis-3.2.5 #編譯 make #測試安裝(稍微耗費點時間) make test #可能會提醒須要安裝最新版的tcl #yum install tcl #指定路徑安裝 make PREFIX=/usr/local/redis install
1 #拷貝conf文件到/etc目錄 2 cp /opt/redis/redis-4.0.1/redis.conf /etc/redis.conf 3 4 5 #redis.conf 參數說明 6 7 ################################## NETWORK ##################################### 8 9 #綁定的主機地址 10 bind 127.0.0.1 11 12 #保護模式,是否容許 沒有認證配置的主機或接口鏈接redis,默認是啓動保護模式,則不容許這種狀況 13 protected-mode yes 14 15 #指定redis的監聽端口,默認端口是6379,做者在本身的一篇博文中解釋了爲何選用6379做爲默認端口,由於6379在手機按鍵上MERZ對應的號碼,而MERZ取自意大利歌女Alessia Merz的名字,嗯,你開發的,你說了算。 16 port 6379 17 18 # In high requests-per-second environments you need an high backlog in order 19 # to avoid slow clients connections issues. Note that the Linux kernel 20 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so 21 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog 22 # in order to get the desired effect. 23 24 tcp-backlog 511 25 26 #客戶端連接多長時間後關閉連接,單位是秒,指定爲0,則表示關閉該功能 27 timeout 0 28 29 # A reasonable value for this option is 300 seconds, which is the new 30 # Redis default starting with Redis 3.2.1. 31 tcp-keepalive 300 32 33 ################################# GENERAL ##################################### 34 35 #Redis默認不是以守護進程的方式運行,能夠經過該配置項修改,使用yes啓用守護進程 36 daemonize yes 37 38 # If you run Redis from upstart or systemd, Redis can interact with your 39 # supervision tree. Options: 40 # supervised no - no supervision interaction 41 # supervised upstart - signal upstart by putting Redis into SIGSTOP mode 42 # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET 43 # supervised auto - detect upstart or systemd method based on 44 # UPSTART_JOB or NOTIFY_SOCKET environment variables 45 # Note: these supervision methods only signal "process is ready." 46 # They do not enable continuous liveness pings back to your supervisor. 47 supervised no 48
1 #服務端啓動 2 [root@bogon redis-4.0.1]# cd /usr/local/redis/ 3 [root@bogon redis]# ./bin/redis-server /etc/redis.conf 4 74537:C 13 Aug 18:53:30.774 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5 74537:C 13 Aug 18:53:30.774 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=74537, just started 6 74537:C 13 Aug 18:53:30.774 # Configuration loaded 7 [root@bogon redis]# ps axu | grep redis 8 root 74538 0.6 0.2 145248 2168 ? Ssl 18:53 0:00 ./bin/redis-server 127.0.0.1:6379 9 root 74665 0.0 0.0 112648 968 pts/4 S+ 18:53 0:00 grep --color=auto redis 10 11 #客戶端啓動 12 redis-cli [-h 127.0.0.1] [-p 6379] 13 127.0.0.1:6379> ping 14 PONG 15 16 #存儲鍵值對 17 127.0.0.1:6379> set name imooc 18 OK 19 20 #獲取name對應的value 21 127.0.0.1:6379> get name 22 "imooc" 23 24 #獲取全部keys 25 127.0.0.1:6379> keys * 26 1) "name" 27 28 #刪除keys 29 127.0.0.1:6379> del name 30 (integer) 1 31 127.0.0.1:6379> get name 32 (nil) 33 34 #關閉服務端 35 redis-cli shutdow
redis最多支持16個數據,下標0-15表示第幾個數據庫。默認是在0號數據。切換數據庫能夠經過select dbnumber 來切換,也能夠經過move 來移動key從當前數據到指定的數據庫。python