Redis學習(一):CentOS下redis安裝和部署

1.基礎知識 

redis是用C語言開發的一個開源的高性能鍵值對(key-value)數據庫。它經過提供多種鍵值數據類型來適應不一樣場景下的存儲需求,目前爲止redis支持的鍵值數據類型以下字符串、列表(lists)、集合(sets)、有序集合(sorts sets)、哈希表(hashs)前端

2.redis的應用場景 

緩存(數據查詢、短鏈接、新聞內容、商品內容等等)。(最多使用
分佈式集羣架構中的session分離。
聊天室的在線好友列表。
任務隊列。(秒殺、搶購、12306等等) 
應用排行榜。 
網站訪問統計。 
數據過時處理(能夠精確到毫秒)linux

3.安裝redis 

下面介紹在CentOS環境下,Redis的安裝與部署,使用redis-3.0穩定版,由於redis從3.0開始增長了集羣功能。  redis

  1. 能夠經過官網下載 地址:http://download.redis.io/releases/redis-3.0.0.tar.gz
  2. 使用linux wget命令

wget http://download.redis.io/releases/redis-3.0.0.tar.gz數據庫

步驟以下:
將redis-3.0.0.tar.gz拷貝到/usr/local下,而後解壓vim

  
  
  
  

cp redis-3.0.0.rar.gz /user/local後端

tar -zxvf redis-3.0.0.tar.gz 緩存

因爲Redis是用C語言編寫,因此編譯時須要gcc,
  
  
  
  

session

yum install gcc -y

進入解壓後的目錄進行編譯,指定目錄安裝 如 /usr/local/redis架構

 

cd /usr/local/redis-3.0.0ssh

make PREFIX=/usr/local/redis install

可能報以下錯誤:

zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error 「Newer version of jemalloc required」
make[1]: * [adlist.o] Error 1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src’
make: * [all] Error 2

緣由分析
在README中有這麼一段話:

Allocator
————

Selecting a non-default memory allocator when building Redis is done by setting the MALLOCenvironment variable. Redis is compiled and linked against libc malloc by default, with the exception of jemalloc being the default on Linux systems. This default was picked because jemalloc has proven to have fewer fragmentation problems than libc malloc.

To force compiling against libc malloc, use:
% make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:
% make MALLOC=jemalloc

意思是說關於分配器allocator, 如有MALLOC 這個 環境變量, 會有用這個環境變量的 去創建Redis。
並且libc 並非默認的分配器, 默認是 jemalloc, 由於 jemalloc 被證實有比libc更少的 fragmentation problems 。
可是若是你又沒有jemalloc 而只有 libc 固然 make 出錯。
因此在編譯的時候須要加一個參數,即:MALLOC=libc

解決辦法
make MALLOC=libc

綜上,執行以下命令完成安裝:

make PREFIX=/usr/local/redis MALLOC=libc install

4.配置Redis

redis.conf是redis的配置文件,redis.conf在redis源碼目錄。
拷貝配置文件到安裝目錄下
進入源碼目錄,裏面有一份配置文件 redis.conf,而後將其拷貝到安裝路徑下

  
  
  
  

cd /usr/local/redis

cp /usr/local/redis-3.0.0/redis.conf /usr/local/redis/bin

cd /usr/local/redis/bin

進入安裝目錄bin下,此時的目錄結構是這樣的
image
  • redis-benchmark redis性能測試工具
  • redis-check-aof AOF文件修復工具
  • redis-check-rdb RDB文件修復工具
  • redis-cli redis命令行客戶端
  • redis.conf redis配置文件
  • redis-sentinal redis集羣管理工具
  • redis-server redis服務進程
5.啓動Redis

1.前端模式啓動
直接運行 ./redis-server將之前端模式啓動,前端模式啓動的缺點是ssh命令窗口關閉則redis-server程序結束,故不推薦使用此方法。
image
2.後端模式啓動
修改redis.conf配置文件, daemonize yes 之後端模式啓動
vim /usr/local/redis/bin/redis.conf
image
執行以下命令啓動redis:

 

cd /usr/local/redis/bin

./redis-server ./redis.conf

鏈接redis:

  
  
  
  

**5.關閉redis**

強行終止redis進程可能會致使redis持久化數據丟失。

正確中止Redis的方式應該是向Redis發送SHUTDOWN命令,

命令爲:

  
  
  
  

cd /usr/local/redis
./bin/redis-cli shutdown

強行終止redis

pkill redis-server
讓redis開機自啓

vim /etc/rc.local
//添加
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-conf

至此redis完成安裝。

相關文章
相關標籤/搜索