1. 程式人生 > >Redis學習01_redis安裝部署(centos) Redis學習(一):CentOS下redis安裝和部署

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

原文: http://www.cnblogs.com/herblog/p/9305668.html

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

 

1.基礎知識 

redis是用C語言開發的一個開源的高效能鍵值對(key-value)資料庫。它通過提供多種鍵值資料型別來適應不同場景下的儲存需求,目前為止redis支援的鍵值資料型別如下字串、列表(lists)、集合(sets)、有序集合(sorts sets)、雜湊表(hashs)

2.redis的應用場景 

快取(資料查詢、短連線、新聞內容、商品內容等等)。(最多使用
分散式叢集架構中的session分離。
聊天室的線上好友列表。
任務佇列。(秒殺、搶購、12306等等) 
應用排行榜。 
網站訪問統計。 
資料過期處理(可以精確到毫秒)

3.安裝redis 

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

  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下,然後解壓

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

tar -zxvf redis-3.0.0.tar.gz

由於Redis是用C語言編寫,所以編譯時需要gcc,

yum install gcc -y

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

cd /usr/local/redis-3.0.0

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完成安裝。

1.基礎知識 

redis是用C語言開發的一個開源的高效能鍵值對(key-value)資料庫。它通過提供多種鍵值資料型別來適應不同場景下的儲存需求,目前為止redis支援的鍵值資料型別如下字串、列表(lists)、集合(sets)、有序集合(sorts sets)、雜湊表(hashs)

2.redis的應用場景 

快取(資料查詢、短連線、新聞內容、商品內容等等)。(最多使用
分散式叢集架構中的session分離。
聊天室的線上好友列表。
任務佇列。(秒殺、搶購、12306等等) 
應用排行榜。 
網站訪問統計。 
資料過期處理(可以精確到毫秒)

3.安裝redis 

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

  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下,然後解壓

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

tar -zxvf redis-3.0.0.tar.gz

由於Redis是用C語言編寫,所以編譯時需要gcc,

yum install gcc -y

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

cd /usr/local/redis-3.0.0

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完成安裝。