redis 是一個基於內存的高性能key-value數據庫,數據都保存在內存中定期刷新到磁盤,以極高的讀寫效率而備受關注。他的特點是支持各種數據結構,stirng,hashes, list,set,和sorted sets
1、下載安裝
wget http://download.redis.io/redis-stable.tar.gz
tar -zxvf redis-stable.tar.gz
cd redis-stable
make
make test 檢查一下是否正常,遇到2個錯誤
[root@localhost redis-stable]# make test
cd src && make test
make[1]: Entering directory `/usr/local/src/redis-stable/src'
which: no tclsh8.5 in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/geffzhang/bin)
You need 'tclsh8.5' in order to run the Redis test
make[1]: *** [test] 錯誤 1
make[1]: Leaving directory `/usr/local/src/redis-stable/src'
make: *** [test] 錯誤 2
[root@localhost redis-stable]#
沒安裝tcl
按照官網http://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.html 上的安裝
make install
mkdir -p /usr/local/bin
cp -pf redis-server /usr/local/bin
cp -pf redis-benchmark /usr/local/bin
cp -pf redis-cli /usr/local/bin
cp -pf redis-check-dump /usr/local/bin
cp -pf redis-check-aof /usr/local/bin
make[1]: Leaving directory `/usr/local/src/redis-stable/src'
[root@localhost redis-stable]#
好了,現在redis就安裝成功了
Redis 由四個可執行文件:redis-benchmark、redis-cli、redis-server、redis-stat 這四個文件,加上一個redis.conf就構成了整個redis的最終可用包。它們的作用如下:
- redis-server:Redis服務器的daemon啟動程序
- redis-cli:Redis命令行操作工具。當然,你也可以用telnet根據其純文本協議來操作
- redis-benchmark:Redis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能
- redis-stat:Redis狀態檢測工具,可以檢測Redis當前狀態參數及延遲狀況
現在就可以啟動redis了,redis只有一個啟動參數,就是他的配置文件路徑。
redis-server /etc/redis.conf
注意,默認復制過去的redis.conf文件的daemonize參數為no,所以redis不會在后台運行,這時要測試,我們需要重新開一個終端。修改為yes則為后台運行redis。另外配置文件中規定了pid文件,log文件和數據文件的地址,如果有需要先修改,默認log信息定向到stdout.
下面是redis.conf的主要配置參數的意義:
- daemonize:是否以后台daemon方式運行
- pidfile:pid文件位置
- port:監聽的端口號
- timeout:請求超時時間
- loglevel:log信息級別
- logfile:log文件位置
- databases:開啟數據庫的數量
- save * *:保存快照的頻率,第一個*表示多長時間,第三個*表示執行多少次寫操作。在一定時間內執行一定數量的寫操作時,自動保存快照。可設置多個條件。
- rdbcompression:是否使用壓縮
- dbfilename:數據快照文件名(只是文件名,不包括目錄)
- dir:數據快照的保存目錄(這個是目錄)
- appendonly:是否開啟appendonlylog,開啟的話每次寫操作會記一條log,這會提高數據抗風險能力,但影響效率。
- appendfsync:appendonlylog如何同步到磁盤(三個選項,分別是每次寫都強制調用fsync、每秒啟用一次fsync、不調用fsync等待系統自己同步)
這時你可以打開一個終端進行測試了,配置文件中默認的監聽端口是6379
2、建立用戶和日志目錄
第一次啟動時建議為Redis建立用戶和日志目錄
[root@localhost redis-stable]# useradd redis
[root@localhost redis-stable]# mkdir -p /var/lib/redis
#db文件放在這里,需要修改redis.conf
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# Also the Append Only File will be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis
[root@localhost redis-stable]# mkdir -p /var/log/redis
# Specify the log file name. Also 'stdout' can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile /var/log/redis/redislog
[root@localhost redis-stable]# chown redis.redis /var/lib/redis
[root@localhost redis-stable]# chown redis.redis /var/log/redis
3、配置Init腳本
Redis管理腳本基於Ubuntu 的發行版上的,Ubuntu的可以看這篇文章ubuntu安裝啟動redis,在Centos linux 上並不能用,下面有個腳本可以用於CentOS 。
用這個腳本管理之前,需要先配置下面的內核參數,否則Redis腳本在重啟或停止redis時,將會報錯,並且不能自動在停止服務前同步數據到磁盤上:
# vi /etc/sysctl.conf
vm.overcommit_memory = 1
然后應用生效:
# sysctl –p
建立redis啟動腳本:
# vim /etc/init.d/redis
#!/bin/bash
#
# Init file for redis
#
# chkconfig: - 80 12
# description: redis daemon
#
# processname: redis
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
#BIN="/usr/local/bin"
BIN="/usr/local/bin"
CONFIG="/etc/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
然后增加服務並開機自啟動:
# chmod 755 /etc/init.d/redis
# chkconfig --add redis
# chkconfig --level 345 redis on
# chkconfig --list redis
[root@localhost redis-stable]# service redis start
Starting Redis Server: [確定]
[root@localhost redis-stable]# cat /var/log/redis/redislog
[14250] 14 Jul 22:23:15 * Server started, Redis version 2.4.15
[14250] 14 Jul 22:23:15 * The server is now ready to accept connections on port 6379
[14250] 14 Jul 22:23:15 - 0 clients connected (0 slaves), 717512 bytes in use
[14250] 14 Jul 22:23:20 - 0 clients connected (0 slaves), 717512 bytes in use
[14250] 14 Jul 22:23:25 - 0 clients connected (0 slaves), 717512 bytes in use
[14250] 14 Jul 22:23:30 - 0 clients connected (0 slaves), 717512 bytes in use
[14250] 14 Jul 22:23:35 - 0 clients connected (0 slaves), 717512 bytes in use
[root@localhost redis-stable]#
centos 6.3 server 安裝redis-2.4.15
