ubuntu下redis安裝配置

ubuntu下redis安裝配置  原文地址:http://www.ttlsa.com/archives/184
一.redis介紹
redis是一個key-value存儲系統,與memcached類似,但是解決了斷電後數據完全丟失的現象。支持數據類型有string,lists,sets,zsets。這些數據類型都支持push/pop,add/remove以及取交集並集差集等操作,對這些操作都是原子性的,redis還支持各種不同的排序能力。

二.redis安裝
$ sudo wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz
$ sudo tar zxvf redis-2.2.12.tar.gz -C ../software/
$ sudo cd /usr/local/src/software/redis-2.2.12/src
$ sudo make PREFIX=/usr/local/redis-2.2.12
$ sudo make test
Testing Redis version 2.2.12 (00000000)
831 tests, 831 passed, 0 failed
$ sudo make PREFIX=/usr/local/redis-2.2.12  install

常見錯誤:
(cd ..; tclsh8.5 tests/test_helper.tcl --tags "" --file "")
/bin/sh: tclsh8.5: not found
make: *** [test] Error 127
解決方法:
$ sudo apt-get install tcl8.5

三.redis配置
$ sudo mkdir -p /usr/local/redis-2.2.12/{etc,var}
redis-server:redis服務的啓動程序
redis-cli:redis命令行操作工具
redis-benchmark:redis性能測試工具
redis-check-aof:更新日誌檢查
redis-check-dump:本地數據檢查
$ sudo cp redis.conf /usr/local/redis-2.2.12/etc/

redis.conf配置參數說明:
daemonize  //是否以後臺進程運行,默認爲no
pidfile /var/run/redis.pid  //pid文件路徑
port 6379  //監聽端口
bind 127.0.0.1  //綁定主機ip
unixsocket /tmp/redis.sock   //sock文件路徑
timeout 300  //超時時間,默認是300s
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel verbose  //日誌等級,可選項有debug,verbose,notice,warning 默認是erbose
logfile stdout  //日誌記錄方式,默認是stdout
syslog-enabled no //日誌記錄到系統日誌中,默認是no
syslog-ident redis  //指定系統日誌標識
# Specify the syslog facility.  Must be USER or between LOCAL0-LOCAL7.
syslog-facility local0  //指定系統日誌設備,默認是local0
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16  //可用數據庫數,默認值是16,默認數據庫是0

save <seconds> <changes>  //在多長時間內,有多少次更新操作,就將數據同步到數據文件。
save 900 1  //15min內至少1個key被改變
save 300 10 //5min內至少有300個key被改變
save 60 10000  //60s內至少有10000個key被改變

rdbcompression yes  //存儲至本地數據庫時是否壓縮數據,默認是yes

dbfilename dump.rdb  //本地數據庫文件名,默認是dump.rdb
dir ./  //本地數據庫存放路徑,默認是./

slaveof <masterip> <masterport>  //當本機爲從服務時,設置主服務的ip以及端口
masterauth <master-password>  //主服務的連接密碼

# When a slave lost the connection with the master, or when the replication
# is still in progress, the slave can act in two different ways:
#
# 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
#    still reply to client requests, possibly with out of data data, or the
#    data set may just be empty if this is the first synchronization.
#
# 2) if slave-serve-stale data is set to 'no' the slave will reply with
#    an error "SYNC with master in progress" to all the kind of commands
#    but to INFO and SLAVEOF.
#
slave-serve-stale-data yes

requirepass foobared  //連接密碼foobared

maxclients 128  //最大連接數,默認不限制

maxmemory <bytes>  //設置最大內存,達到最大內存設置後,redis會先嚐試清除已到期或即將到期的key,當此方法處理後,任然到達最大內存設置,將無法再進行寫入操作

maxmemory設置策略
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys->random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
maxmemory-policy volatile-lru  //maxmemory設置策略,默認是volatile-lru.
maxmemory-samples 3

appendonly no  //是否 在每次更新操作後進行日誌記錄,如果不開啓,可能會在斷電時導致一段時間內的數據丟失。因爲redis本身同步數據文件是按照上面save條件來進行同步的,所以有的數據會在一段時間內只存在於內存中。默認是no
appendfilename appendonly.aof  //更新日誌文件名,默認是appendonly.aof

redis支持的三種不同的同步方式:
# no: don't fsync, just let the OS flush the data when it wants. Faster.  //等待OS進行數據緩存同步到硬盤
# always: fsync after every write to the append only log . Slow, Safest.  //每次更新操作後調用fsync()將數據寫到磁盤
# everysec: fsync only if one second passed since the last fsync. Compromise. //每秒同步一次
appendfsync everysec //更新日誌條件,默認是everysec
no-appendfsync-on-rewrite no

slowlog-log-slower-than 10000  //設置redis slow log時間,只包括命令執行時間,不包括IO操作時間,比如客戶端連接,應答相應時間等等。單位是microseconds(一百萬分之一秒),默認是10000.負值表示禁用slow log,0表示記錄所有命令。

slowlog-max-len 1024  //slowlog最大長度1024.這會消耗內存,使用SLOWLOG RESET來回收slowlog內存。


#在redis2.4版本,強烈不建議使用virtual memory。
vm-enabled no  //是否使用虛擬內存,默認是no
vm-swap-file /tmp/redis.swap  //虛擬內存文件路徑,默認是/tmp/redis.swap,不可多個redis實例共享虛擬內存文件。
vm-max-memory 0 //設置最大vm,默認爲0,所有的value存在於磁盤中。
vm-page-size 32  //設置vm的page大小,默認是32
vm-pages 134217728  //設置swap文件中最大memory pages,默認是134217728。swap大小=vm-page-size * vm-pages
vm-max-threads 4  //vm同時運行的最大io線程

#指定在超過一定的數量或者最大的元素超過某一臨界值時,採用一種特殊的哈希算法
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
activerehashing yes  //是否重置hash表

include /path/to/other.conf   //引用其他配置文件

$ sudo vim /etc/sysctl.conf
vm.overcommit_memory = 1   //指定內核針對內存分配的策略,其值可以是0,1,2
0表示內核將檢查是否有足夠的可用內存供應用進程使用;如果有足夠的可用內存,內存申請允許;否則,內存申請失敗,並把錯誤返回給應用進程。
1表示內核允許分配所有的物理內存,而不管當前的內存狀態如何。
2表示內核允許分配超過所有物理內存和交換空間總和的內存
$ sudo sysctl -p

$ sudo vim  redis.conf
daemonize yes
pidfile /var/run/redis.pid
port 6379
timeout 300
loglevel verbose
logfile /usr/local/redis-2.2.12/var/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis-2.2.12/var/data
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
slowlog-log-slower-than 10000
slowlog-max-len 1024
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
activerehashing yes
啓動:
$ sudo /usr/local/redis-2.2.12/bin/redis-server /usr/local/redis-2.2.12/etc/redis.conf

四.測試
$ cat /etc/passwd | ./redis-cli -x set mykey
OK
$ ./redis-cli get mykey
"root:x:0:0:root:/root:/bin/bash\daemon:x:1:1:daemon:/usr/sbin:/bin/sh\mysql:x:111:120:MySQL Server,,,:/var/lib/mysql:/bin/false\"

ubuntu下redis安裝配置  原文地址:http://www.ttlsa.com/archives/184

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章