Redis的安裝-Redis學習筆記一
1. 什麼是Redis
Redis是Remote Dictionary Server(遠端資料服務)的縮寫
由義大利人 antirez(Salvatore Sanfilippo) 開發的一款 記憶體高速快取資料庫
該軟體使用C語言編寫,它的資料模型為 key-value
它支援豐富的資料結構(型別),比如 String(字串)、list(連結串列) 、 hash(雜湊) 、 set(集合) 、 sortedset(有序集合)。
可持久化(將記憶體中資料寫入硬碟,也可從硬碟寫入記憶體),保證了資料安全。
快取:有兩種型別 :資料快取、頁面快取(smarty)
使用快取減輕資料庫的負載。
在開發網站的時候如果有一些資料在短時間之內不會發生變化,而它們還要被頻繁訪問,為了提高使用者的請求速度和降低網站的負載,
就把這些資料放到一個讀取速度更快的介質上(或者是通過較少的計算量就可以獲得該資料) ,該行為就稱作對該資料的快取。
該介質可以是檔案、資料庫、記憶體,記憶體經常用於資料快取。
快取的兩種形式:
頁面快取經常用在CMS(content manage system)記憶體管理系統裡邊
新聞頁面(內容主體單一、集中)適合做頁面快取
資料快取經常會用在頁面的具體資料裡邊
商品頁面的組成部分有本身的的業務特點,各個部分資料比較獨立,適合給他們分別做“資料快取”
2. redis和 memcache比較
①Redis不僅僅支援簡單的k/v型別的資料,同時還提供list,set,zset,hash等資料結構的儲存。
②Redis支援master-slave(主—從)模式應用。
③Redis支援資料的持久化,可以將記憶體中的資料保持在磁碟中,重啟的時候可以再次載入進行使用。
④Redis單個value的最大限制是1GB, memcached只能儲存1MB的資料
3. 安裝redis
把redis軟體上傳到/hotdata/soft目錄裡邊:
1)解壓Redis壓縮包[[email protected] soft]# tar zxvf redis-2.6.14.tar.gz
2)進入解壓後的資料夾,直接執行編譯安裝make
[[email protected] soft]# cd redis-2.6.14
[[email protected] redis-2.6.14]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README runtest src utils
BUGS COPYING INSTALL MANIFESTO redis.conf sentinel.conf tests
[[email protected] redis-2.6.14]# vim README
[[email protected] redis-2.6.14]# make
3) 可執行make test, 若提示未安裝tcl,則安裝(可跳過)
[[email protected] src]# make test
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1
[[email protected] src]# yum install tcl
在src目錄下的相關檔案解釋:
4)建立/usr/local/redis執行目錄,並給其copy兩個執行檔案:
[[email protected] src]# mkdir /usr/local/redis
[[email protected] src]# cp redis-cli redis-server /usr/local/redis/
給redis執行目錄再copy一個配置檔案
(配置檔案在redis的解壓檔案目錄裡邊)
[[email protected] src]# cd ..
[[email protected] redis-2.6.14]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README runtest src utils
BUGS COPYING INSTALL MANIFESTO redis.conf sentinel.conf tests
[[email protected] redis-2.6.14]# cp redis.conf /usr/local/redis/
5)前臺啟動redis服務(關閉視窗,則Redis停止)
6)設定後臺啟動redis
修改redis.conf配置檔案
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# 將no改為yes,設定為後臺啟動Redis
daemonize yes
攜帶redis.conf配置檔案,在後臺啟動redis服務:
[[email protected] redis]# pwd
/usr/local/redis
[[email protected] redis]# ./redis-server redis.conf
[[email protected] redis]#
檢視是否成功啟動Redis
[[email protected] redis]# ps -A | grep redis
5283 ? 00:00:00 redis-server
到這裡就已經成功安裝了Redis並啟動成功
4. 簡單使用
redis的簡單操作(設定和讀取變數):
[[email protected] redis]# ./redis-cli
redis 127.0.0.1:6379> set name tom
OK
redis 127.0.0.1:6379> set age 11
OK
redis 127.0.0.1:6379> set addr beijing
OK
redis 127.0.0.1:6379> get name
"tom"
redis 127.0.0.1:6379> get age
"11"
redis 127.0.0.1:6379> get addr
"beijing"
redis 127.0.0.1:6379>