第一步:下載安裝編譯
#wgethttp://redis.googlecode.com/files/redis-2.4.2.tar.gz
#tar zxvf redis-2.4.2.tar.gz
#cd redis-2.4.2
#make
#make install
#cp redis.conf /etc/
第二步:修改配置
#vi /etc/redis.conf
配置見附錄
第三步:啟動進程
#redis-server/etc/redis.conf
查看進程有沒有成功啟動
#ps -ef | grep redis
測試輸入一個索引值
#redis-cli set test"123456"
擷取索引值
#redis-cli get test
關閉redis
# redis-cli shutdown //關閉所有
關閉某個連接埠上的redis
# redis-cli -p 6397 shutdown //關閉6397連接埠的redis
說明:關閉以後快取資料會自動dump到硬碟上,硬碟地址見redis.conf中的dbfilename dump.rdb
附錄:無錯配置
只要做如下配置即可:
daemonize yes
pidfile /var/run/redis.pid
port 6379
#bind 127.0.0.1
timeout 600
loglevel notice
logfile /elain/logs/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /elain/data/redis/
# maxclients 128
appendonly yes
appendfilename appendonly.aof
# appendfsync always
appendfsync everysec
# appendfsync no
requirepass elain
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 1024
really-use-vm yes
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
# include /path/to/local.conf
# include /path/to/other.conf
1,是否以後台進程運行,預設為no
daemonize no
2,如以後台進程運行,則需指定一個pid,預設為/var/run/redis.pid
pidfile /var/run/redis.pid
3,監聽連接埠,預設為6379
port 6379
4,綁定主機IP,預設值為127.0.0.1(注釋)
bind 127.0.0.1
5,逾時時間,預設為300(秒)
timeout 300
6,日誌記錄等級,有4個可選值,debug,verbose(預設值),notice,warning
loglevel verbose
7,日誌記錄方式,預設值為stdout
logfile stdout
8,可用資料庫數,預設值為16,預設資料庫為0
databases 16
9,指出在多長時間內,有多少次更新操作,就將資料同步到資料檔案。這個可以多個條件配合,比如預設設定檔中的設定,就設定了三個條件。
900秒(15分鐘)內至少有1個key被改變
save 900 1
300秒(5分鐘)內至少有10個key被改變
save 300 10
10,儲存至本機資料庫時是否壓縮資料,預設為yes
rdbcompression yes
11,本機資料庫檔案名稱,預設值為dump.rdb
dbfilename /root/redis_db/dump.rdb
12,本機資料庫存放路徑,預設值為 ./
dir /root/redis_db/
13,當本機為從服務時,設定主服務的IP及連接埠(注釋)
slaveof <masterip> <masterport>
14,當本機為從服務時,設定主服務的串連密碼(注釋)
masterauth <master-password>
15,串連密碼(注釋)
requirepass foobared
16,最大用戶端串連數,預設不限制(注釋)
maxclients 128
17,設定最大記憶體,達到最大記憶體設定後,Redis會先嘗試清除已到期或即將到期的Key,當此方法處理後,任到達最大記憶體設定,將無法再進行寫入操作。(注釋)
maxmemory <bytes>
18,是否在每次更新操作後進行日誌記錄,如果不開啟,可能會在斷電時導致一段時間內的資料丟失。因為redis本身同步資料檔案是按上面save條件來同步的,所以有的資料會在一段時間內只存在於記憶體中。預設值為no
appendonly yes
19,更新記錄檔名,預設值為appendonly.aof(注釋)
appendfilename /root/redis_db/appendonly.aof
20,更新日誌條件,共有3個可選值。no表示等作業系統進行資料緩衝同步到磁碟,always表示每次更新操作後手動調用fsync()將資料寫到磁碟,everysec表示每秒同步一次(預設值)。
appendfsync everysec
21,是否使用虛擬記憶體,預設值為no
vm-enabled yes
22,虛擬記憶體檔案路徑,預設值為/tmp/redis.swap,不可多個Redis執行個體共用
vm-swap-file /tmp/redis.swap
23,將所有大於vm-max-memory的資料存入虛擬記憶體,無論vm-max-memory設定多小,所有索引資料都是記憶體儲存的 (Redis的索引資料就是keys),也就是說,當vm-max-memory設定為0的時候,其實是所有value都存在於磁碟。預設值為0。
vm-max-memory 0
24,虛擬記憶體檔案以Block Storage,每塊32bytes
vm-page-size 32
25,虛擬內在檔案的最大數
vm-pages 134217728
26,可以設定訪問swap檔案的線程數,設定最好不要超過機器的核心數,如果設定為0,那麼所有對swap檔案的操作都是串列的.可能會造成比較長時間的延遲,但是對資料完整性有很好的保證.
vm-max-threads 4
27,把小的輸出緩衝放在一起,以便能夠在一個TCP packet中為用戶端發送多個響應,具體原理和真實效果我不是很清楚。所以根據注釋,你不是很確定的時候就設定成yes
glueoutputbuf yes
28,在redis 2.0中引入了hash資料結構。當hash中包含超過指定元素個數並且最大的元素沒有超過臨界時,hash將以一種特殊的編碼方式(大大減少記憶體使用量)來儲存,這裡可以設定這兩個臨界值
hash-max-zipmap-entries 64
29,hash中一個元素的最大值
hash-max-zipmap-value 512
30,開啟之後,redis將在每100毫秒時使用1毫秒的CPU時間來對redis的hash表進行重新hash,可以降低記憶體的使用。當你的使 用情境中,有非常嚴格的即時性需要,不能夠接受Redis時不時的對請求有2毫秒的延遲的話,把這項配置為no。如果沒有這麼嚴格的即時性要求,可以設定 為yes,以便能夠儘可能快的釋放記憶體
activerehashing yes
JAVA測試類別如下:
package com.vopzoon.app.base.queue;import org.springframework.stereotype.Component;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/** * 利用key-value記憶體資料庫redis實現的任務隊列集合 * @author liuwang * */@Componentpublic class QueueHelper {/** * redis pool池 */private JedisPool pool;/** * redis客服端 */private Jedis client;/** * 建構函式 * @param host IP地址 * @param port 連接埠 * @param isAuth 是否要求輸入密碼 false表示不要求輸入密碼 * @param password */public QueueHelper(String host,int port,boolean isAuth,String password){pool = new JedisPool(new JedisPoolConfig(), host, port);client = pool.getResource();if(isAuth){client.auth(password);}}public QueueHelper(String host, int port) {this(host, 6379, false, null);}public QueueHelper(String host) {this(host, 6379, false, null);}public QueueHelper(String host, String password) {this(host, 6379, true, password);}public QueueHelper(String host, int port, String password) {this(host, port, true, password);}/** * 從 List 頭部添加一個元素 * @param key * @param value * @return 1表示成功 0表示key不存在 key對應的不是list返回錯誤 */public long pushAtHead(String key,String value){return client.lpush(key,value);}/** * 從 List 尾部添加一個元素 * @param key * @param value * @return 1表示成功 0表示key不存在 key對應的不是list返回錯誤 */public long pushAtTail(String key,String value){return client.rpush(key,value);}/** * 彈出key對應的list第一個元素 * @param key * @return 第一個元素,如果key對應的list不存在或者是空返回null * 如果key對應值不是list返回錯誤 */public String popAtHead(String key){return client.lpop(key);}/** * 彈出key對應的list最後一個元素 * @param key * @return 最後一個元素,如果key對應的list不存在或者是空返回null * 如果key對應值不是list返回錯誤 */public String popAtTail(String key){return client.rpop(key);}/** * 覆蓋原來的資料 * @param key 對應的key值 * @param value 對應的value值 * @return */public String replace(String key, String value) {return client.set(key, value);}/** * 刪除隊列中的記錄 * @param key 記錄的key值 * @return */public long delete(String key) {return client.del(key);}/** * 擷取key對應的list的長度 * @param key * @return */public long getQueueSize(String key){return client.llen(key);}/** * 清空所有資料庫 */public void clearAllData(){client.flushAll();}/** * 關閉資源 */ public void destory() {//使用完之後將串連返回到串連池中 pool.returnResource(client); //銷毀串連池中的所有串連 pool.destroy(); }//test執行個體public static void main(String []args){QueueHelper client = new QueueHelper("10.1.101.222","xxxx");//long result = client.pushAtHead("aaa", "bbb");//System.out.println(result);System.out.println(client.popAtHead("aaa"));//清空redis所有資料庫(慎用)//client.clearAllData();}}
QueueHelper client = new QueueHelper("10.1.101.222","xxxx");xxxx是配置的密碼
在redis.conf中的requirepass xxxx配置
補充一下, 在linux中, 如果設定了密碼, 串連用戶端的時候
redis-cli -a xxxx
[root@queue001 ~]# redis-cli --helpredis-cli 2.4.2Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]] -h <hostname> Server hostname (default: 127.0.0.1) -p <port> Server port (default: 6379) -s <socket> Server socket (overrides hostname and port) -a <password> Password to use when connecting to the server -r <repeat> Execute specified command N times -i <interval> When -r is used, waits <interval> seconds per command. It is possible to specify sub-second times like -i 0.1. -n <db> Database number -x Read last argument from STDIN -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \n) --raw Use raw formatting for replies (default when STDOUT is not a tty) --latency Enter a special mode continuously sampling latency. --help Output this help and exit --version Output version and exit
自己可以查看協助嘛..呵呵