Redis學習手冊一:安裝配置

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

   Redis的所有數據都是保存在內存中,然後不定期的通過異步方式保存到磁盤上(半持久化模式);也可以把每一次數據變化都寫入到一個append only file(aof)裏面(全持久化模式)。

環境:Redhat 5.3 64bit

一、Redis安裝

①、下載Redis

   下載Redis可以直接從其官網http://redis.io/download下載最新版,如下:

# wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz

②、安裝tclsh

   Redis在make test有使用到tclsh對Redis進行測試,所有需要想將tclsh安裝好,如果沒有安裝的話,在make test過程中會出現如下錯誤:

# make test
cd src && make test
make[1]: Entering directory `/usr/local/webserver/redis/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/usr/local/webserver/redis/src'
make: *** [test] Error 2

tclsh下載可以直接從官網http://www.tcl.tk/software/tcltk/download.html下載其最新版


# wget http://hivelocity.dl.sourceforge.net/project/tcl/Tcl/8.5.14/tcl8.5.14-src.tar.gz
# tar xzvf tcl8.5.14-src.tar.gz
# cd tcl8.5.14-src/unix     #windows進入tcl8.5.14-src/win
# ./configure --prefix=/app/soft/tcl8.5.11 --enable-64bit  #enable-64bit對64系統生效
# make && make installan

安裝完成之後需要將tclsh添加到PATH中,並使其生效


# vim /etc/profile
···
PATH=/app/soft/tcl8.5.11/bin:$PATH
export PATH
···
# source /etc/profile

這樣tclsh就已經安裝完成了。

③、安裝Redis

# tar xzvf redis-2.6.14.tar.gz
# cd redis-2.6.14
# make
# make test   #檢查Redis是否已經make成功,這個步驟可以省略,不過建議還是使用
# make PREFIX=/app/soft/redis install  #默認安裝路徑:/usr/local
# mkdir -p /app/soft/redis/{etc,share,data}
# cp redis.conf /app/soft/redis/etc/   #複製配置文件到redis配置目錄

以上整個Redis已經安裝完成了。

二、配置Redis

①、如下爲Redis配置信息:

#  cat redis.conf|grep -v ^#
daemonize yes            #開啓守護進程
pidfile /var/run/redis.pid    #設置PID文件
port 6579                #設置Redis端口
timeout 300
tcp-keepalive 0
loglevel verbose        #設置日誌級別
syslog-enabled yes      #開啓syslog
syslog-ident redis      #設置Redis在syslog裏面的標識符 
syslog-facility local6  #設置Redis在syslog使用的設備
databases 5
save 900 1              #Redis硬盤數據保存設置
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb    #Redis數據保存文件
dir /app/soft/redis2.6.14/data    #Redis數據保存目錄
slaveof 183.232.10.64  6579       #開啓主從同步,設置Master的IP及端口
masterauth Dmx#xYkJ0Z8            #設置主從同步密碼
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
requirepass Dmx#xYkJ0Z8            #設置Redis認證密碼
maxclients 10000                   #設置客戶端連接數
maxmemory 512M                     #設置內容大小
appendonly no                      #設置是否開啓AOF模式
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-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
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

②、日誌設置

通過Redis日誌的配置可以知道,這次設置Redis日誌是通過syslog來統一管理的,然後再通過logrotate來進行日誌輪循,具體配置如下:

##syslog配置
#  cat /etc/syslog.conf
···
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none;redis.none    /var/log/messages
#redis.none去除Redis的日誌輸出到message
···
# Save redis messages also to redis.log
local6.*                                             /var/log/redis.log
#設置Redis日誌輸入到/var/log/redis.log文件
···
##logrotate配置
# cat /etc/logrotate.d/redis
/var/log/redis.log {
    missingok
    weekly
    notifempty
    rotate 15
    size 200M
    sharedscripts
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

logrotate配置文檔的意思爲:每週檢查一次Redis日誌文件,大小超過200M就輪循一次,保存15個輪循日誌文件。

③、內核設置

因爲Redis需要使用到內存,所有最好配置一個內核參數,否則有可能會報警,具體如下:

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

三、Redis啓動與關閉

#Redis啓動
/app/soft/redis/bin/redis-server /app/soft/redis/etc/redis.conf
#Redis關閉
/app/soft/redis/bin/redis-cli shutdown  或者kill pid

五、Redis服務驗證

/app/soft/redis/bin/redis-cli 登錄,然後再通過ping、info、命令查看redis情況,具體示例如下:

# ./redis-cli -h localhost -p 6579 -a password
redis localhost:6579> info
# Server
redis_version:2.6.14
redis_git_sha1:00000000
redis_git_dirty:0
redis_mode:standalone
os:Linux 2.6.18-128.el5 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.1.2
process_id:18661
run_id:45efd447d2a29af7ffdd6f430139232de2d2d596
tcp_port:6579
uptime_in_seconds:532309
uptime_in_days:6
hz:10
lru_clock:1323989
# Clients
connected_clients:7
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:131584952
used_memory_human:125.49M
used_memory_rss:135761920
used_memory_peak:131748656
used_memory_peak_human:125.65M
used_memory_lua:31744
mem_fragmentation_ratio:1.03
mem_allocator:jemalloc-3.2.0
# Persistence
loading:0
rdb_changes_since_last_save:478
rdb_bgsave_in_progress:0
rdb_last_save_time:1376388583
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
# Stats
total_connections_received:373
total_commands_processed:15615130
instantaneous_ops_per_sec:6
rejected_connections:0
expired_keys:0
evicted_keys:0
keyspace_hits:612642
keyspace_misses:3976261
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:6940
# Replication
role:slave
master_host:10.232.10.64
master_port:6579
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_priority:100
slave_read_only:1
connected_slaves:0
# CPU
used_cpu_sys:480.20
used_cpu_user:280.40
used_cpu_sys_children:99.57
used_cpu_user_children:946.66
# Keyspace
db0:keys=551,expires=0
redis localhost:6579>


通過上面4個步驟基本上就將Redis搭建起來了。

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