Redis 安裝及配置 (轉整理)

Redis 安裝及配置 (轉整理)

 

Redis是一種高級key-value數據庫。它跟memcached相似,不過數據能夠持久化,並且支持的數據類型很豐富。有字符串,鏈表,集 合和有序集合。支持在服務器端計算集合的並,交和補集(difference)等,還支持多種排序功能。因此Redis也能夠被當作是一個數據結構服務 器。

Redis的全部數據都是保存在內存中,而後不按期的經過異步方式保存到磁盤上(這稱爲「半持久化模式」);也能夠把每一次數據變化都寫入到一個append only file(aof)裏面(這稱爲「全持久化模式」)。

I.快速運行Redis

1、下載安裝
進入redis.io官方網站:
Linux代碼javascript

 收藏代碼

  1. $ wget http://redis.googlecode.com/files/redis-2.4.15.tar.gz  
  2. $ tar xzf redis-2.4.5.tar.gz //這裏假設解壓縮到/usr/local/redis  
  3. $ cd redis-2.4.5  
  4. $ make  
  5. $ make install  
  6. $ cd utils  
  7. $./install_server  



就會自動安裝到/usr/local/bin目錄下。在該目錄下生成幾個可執行文件,分別是redis-server、redis-cli、redis-benchmark、redis-stat、redis-check-aof,它們的做用以下:
    redis-server:Redis服務器的daemon啓動程序
    redis-cli:Redis命令行操做工具。固然,你也能夠用telnet根據其純文本協議來操做
    redis-benchmark:Redis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能
    redis-stat:Redis狀態檢測工具,能夠檢測Redis當前狀態參數及延遲情況
    redis-check-aof:

二.啓動服務器
安裝時的最後一步install_server腳本會生成啓動命令文件(試試就知道),下面就是一個執行例子
Linux代碼php

 收藏代碼

  1. Welcome to the redis service installer  
  2. This script will help you easily set up a running redis server  
  3.   
  4.   
  5. Please select the redis port for this instance: [6379]  
  6. Selecting default: 6379  
  7. Please select the redis config file name [/etc/redis/6379.conf]  
  8. Selected default - /etc/redis/6379.conf  
  9. Please select the redis log file name [/var/log/redis_6379.log]  
  10. Selected default - /var/log/redis_6379.log  
  11. Please select the data directory for this instance [/var/lib/redis/6379]  
  12. Selected default - /var/lib/redis/6379  
  13. Please select the redis executable path [/usr/local/bin/redis-server]  
  14. Copied /tmp/6379.conf => /etc/init.d/redis_6379  
  15. Installing service...  
  16. Successfully added to chkconfig!  
  17. Successfully added to runlevels 345!  
  18. Starting Redis server...  
  19. Installation successful!  


/etc/init.d/redis_6379 start
將啓動服務到默認端口6379

三.客戶端訪問
Linux代碼java

 收藏代碼

  1. $ redis-cli  
  2. redis> set foo bar  
  3. OK  
  4. redis> get foo  
  5. "bar"  



四.關閉服務器
Linux代碼git

 收藏代碼

  1. $ /etc/init.d/redis_6379 stop  


以上摘官方文檔http://redis.io/download。若是想使用Windows版的Redis請去http://code.google.com/p/servicestack/wiki/RedisWindowsDownload下載(其版本滯後於官方版本,不建議在生產環境使用Win32.)。

II. 定製服務器啓動參數
在咱們成功安裝Redis後,咱們直接執行redis-server便可運行Redis,此時它是按照默認配置來運行的(默認配置甚至不是後臺運行)。咱們但願Redis按咱們的要求運行,則咱們須要修改配置文件(在redis解壓縮目錄下有一個redis.con能夠做爲範本),下面是redis.conf的主要配置參數的意義:github

引用redis

    daemonize:是否之後臺daemon方式運行
pidfile:pid文件位置
port:監聽的端口號
timeout:請求超時時間
loglevel:log信息級別
logfile:log文件位置
databases:開啓數據庫的數量
save * *:保存快照的頻率,第一個*表示多長時間,第三個*表示執行多少次寫操做。在必定時間內執行必定數量的寫操做時,自動保存快照。可設置多個條件。
rdbcompression:是否使用壓縮
dbfilename:數據快照文件名(只是文件名,不包括目錄)
dir:數據快照的保存目錄(這個是目錄)
appendonly:是否開啓appendonlylog,開啓的話每次寫操做會記一條log,這會提升數據抗風險能力,但影響效率。
appendfsync:appendonlylog如何同步到磁盤(三個選項,分別是每次寫都強制調用fsync、每秒啓用一次fsync、不調用fsync等待系統本身同步)數據庫



下面是一個略作修改後的配置文件內容:
Redis.conf代碼windows

 收藏代碼

  1. daemonize yes  
  2. pidfile /usr/local/redis/var/redis.pid  
  3. port 6000  
  4. timeout 300  
  5. loglevel debug  
  6. logfile /usr/local/redis/var/redis.log  
  7. databases 16  
  8. save 900 1  
  9. save 300 10  
  10. save 60 10000  
  11. rdbcompression yes  
  12. dbfilename dump.rdb  
  13. dir /usr/local/redis/var/  
  14. appendonly no  
  15. appendfsync always  
  16. glueoutputbuf yes  
  17. shareobjects no  
  18. shareobjectspoolsize 1024  


重啓服務器
Linux代碼服務器

 收藏代碼

  1. redis-server /usr/local/redis/redis.conf  


試試看讀寫是否有問題.若是服務器啓動到了指定非默認端口,那麼客戶端鏈接則須要-p參數
如:
Linux代碼數據結構

 收藏代碼

  1. $redis-cli -p 6380  



* 開放服務器端口供其餘主機鏈接
vi /etc/sysconfig/iptables #須要具有其修改權限
可能須要增長一行:
# redis
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
保存後重啓iptables:
service iptables restart (PATH中加入了/sbin/)或者 /etc/init.d/iptables restart

III. PHP客戶端
Redis的php客戶端庫很是之多,code.google.com上就有:
* phpredis
http://code.google.com/p/phpredis/ 是C客戶端做爲php模塊

* php-predis
http://code.google.com/p/php-redis/ 純php客戶端
以上兩個都不在Redis官方推薦推薦之列。

Redis推薦客戶端連接是:http://redis.io/clients,以下圖:


純php庫Predis(便於hack),可是性能不高。https://github.com/nrk/predis
下載該庫文件後運行bin/createSingleFile.php能夠生成一個類庫文件Predis.php,很是方便使用。下面是最簡單的一個Hello World應用:
Php代碼

 收藏代碼

  1. <?php  
  2. require('Predis.php');  
  3. $single_server = array(  
  4.     'host'     => '192.168.1.101',  
  5.     'port'     => 6379,  
  6.     'database' => 15  
  7. );  
  8.   
  9. $client = new Predis\Client($single_server);  
  10.   
  11. $client->set('library', 'predis');  
  12. $retval = $client->get('allen ');  
  13.   
  14. var_dump($retval);  



我推薦使用的客戶端是:phpredis  https://github.com/nicolasff/phpredis
windows版的dll從這裏下載https://github.com/char101/phpredis/downloads

IV . Redis管理工具
1. phpRedisAdmin(推薦)
https://github.com/ErikDubbelboer/phpRedisAdmin/
基於nicolasff/phpredis擴展


2. redis-admin
http://code.google.com/p/redis-admin/

相關文章
相關標籤/搜索