今天初试redis,记录下安装配置过程
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
1.redis安装
# tar xvf redis-2.4.5.tar.gz
# cd redis-2.4.5
# make
#复制两个配置文件和命令文件到以下对应目录
# mkdir /etc/redis
# mkdir /usr/local/redis/var -p
# cp redis.conf /etc/redis/
# cd src/
# cp redis-benchmark redis-cli redis-server /usr/bin/
# vim /etc/redis/redis.conf
2.参数的意义:
daemonize:是否以后台daemon方式运行
pidfile:pid文件位置
port:监听的端口号
timeout:请求超时时间
loglevel:log信息级别
logfile:log文件位置
databases:开启数据库的数量
save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
rdbcompression:是否使用压缩
dbfilename:数据快照文件名(只是文件名,不包括目录)
dir:数据快照的保存目录(这个是目录)
appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)
下面是一个略做修改后的配置文件内容:
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
timeout 300
loglevel debug
logfile /usr/local/redis/var/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/var/
appendonly no
appendfsync always
syslog-enabled yes
syslog-facility local0
maxmemory 256000000
maxclients 500
#glueoutputbuf yes
#shareobjects no
#shareobjectspoolsize 1024
3.建立启动脚本:redisd
#!/bin/bash
#
# Init file for redis
#
# chkconfig: - 80 12
# description: redis daemon
#
# processname: redis
# config: /etc/redis/redis.conf
# pidfile: /usr/local/redis/var/redis.pid
. /etc/init.d/functions
BIN="/usr/bin"
CONFIG="/etc/redis/redis.conf"
PIDFILE="/usr/local/redis/var/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/redisd
# chkconfig --add redisd
# chkconfig redisd on
同时配置内核参数sysctl
vm.overcommit_memory = 1
启动:service redisd start
4.测试redis
# redis-benchmark
====== PING (inline) ======
10000 requests completed in 0.42 seconds
50 parallel clients
3 bytes payload
keep alive: 1
71.62% <= 1 milliseconds
95.91% <= 2 milliseconds
95.93% <= 4 milliseconds
96.05% <= 5 milliseconds
96.29% <= 6 milliseconds
97.07% <= 7 milliseconds
98.31% <= 8 milliseconds
98.32% <= 9 milliseconds
98.33% <= 12 milliseconds
98.45% <= 13 milliseconds
98.46% <= 18 milliseconds
98.47% <= 19 milliseconds
98.52% <= 20 milliseconds
98.81% <= 22 milliseconds
99.52% <= 23 milliseconds
99.63% <= 24 milliseconds
99.64% <= 27 milliseconds
99.65% <= 29 milliseconds
99.66% <= 31 milliseconds
99.67% <= 33 milliseconds
99.68% <= 37 milliseconds
99.69% <= 39 milliseconds
99.70% <= 41 milliseconds
99.71% <= 43 milliseconds
99.72% <= 46 milliseconds
99.73% <= 48 milliseconds
99.74% <= 50 milliseconds
99.75% <= 52 milliseconds
99.76% <= 53 milliseconds
99.80% <= 56 milliseconds
99.81% <= 58 milliseconds
99.82% <= 60 milliseconds
99.83% <= 62 milliseconds
99.84% <= 78 milliseconds
99.85% <= 80 milliseconds
99.86% <= 82 milliseconds
99.87% <= 84 milliseconds
99.88% <= 103 milliseconds
99.89% <= 105 milliseconds
99.90% <= 107 milliseconds
99.91% <= 110 milliseconds
99.92% <= 113 milliseconds
99.93% <= 115 milliseconds
99.94% <= 117 milliseconds
99.95% <= 136 milliseconds
99.96% <= 138 milliseconds
99.97% <= 140 milliseconds
99.98% <= 143 milliseconds
100.00% <= 143 milliseconds
23866.35 requests per second
5.基于命令行测试:
[root@server37 trunk]# redis-cli set spawn king
OK
[root@server37 trunk]# redis-cli get spawn
"king"
后续应用,以待整理
阅读(2076) | 评论(0) | 转发(1) |