带存储功能键值数据库redis安装和使用
redis是一个带存储功能的键值数据库,类似日本的Tokyo Tyrant / Tokyo Cabinet 项目。下面是官方的介绍:
Redis is an advanced key-value store. It is similar to memcached but the dataset is not volatile, and values can be strings, exactly like in memcached, but also lists, sets, and ordered sets. All this data types can be manipulated with atomic operations to push/pop elements, add/remove elements, perform server side union, intersection, difference between sets, and so forth. Redis supports different kind of sorting abilities.
这里有篇国外的人对redis,memcached,tokyo tyrant等做的对测试:
http://www.ruturaj.net/redis-memcached-tokyo-tyrant-mysql-comparison
可以看出redis的性测非常出色。
另外redis对数据结构的支持也非常丰富,什么队列,栈,哈希等等都支持,而memcached只支持K-V。
官方站点为:http://redis.googlecode.com/
安装:
wget http://redis.googlecode.com/files/redis-1.2.1.tar.gz
tar -xvzf redis-1.2.1.tar.gz
cd redis-1.2.1
make
不要想make install,他们还没做这个功能,呵呵。
cp redis-cli redis-benchmark redis-server /usr/bin
cp redis.conf /etc/
数据库文件保存在 /var/lib/redis:
mkdir /var/lib/redis
vi /etc/redis.conf
daemonize yes
pidfile /var/run/redis.pid
port 6379
timeout 300
loglevel debug
logfile /var/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /var/lib/redis
appendonly yes
appendfsync always
glueoutputbuf yes
shareobjects no
shareobjectspoolsize 1024
save 900 1
save 300 10
save 60 10000
这里的意思是900秒内有1个改变,300秒内有10个改变,60秒内有10000个改变,redis就会内存中的key保存到数据库文件中去。
开机启动:
vi /etc/rc.local
/usr/bin/redis-server /etc/redis.conf
测试:
新建一个key
[root@mysqltest ~]# redis-cli set testkey keyv
OK
取一个key
[root@mysqltest ~]# redis-cli get testkey
keyv
查看redis内的所有key
[root@mysqltest ~]# redis-cli keys *
testkey
查看redis状态
[root@mysqltest ~]# redis-cli info
redis_version:1.2.1
arch_bits:64
multiplexing_api:epoll
uptime_in_seconds:144
uptime_in_days:0
connected_clients:1
connected_slaves:0
used_memory:619250
changes_since_last_save:1
bgsave_in_progress:0
last_save_time:1265165103
bgrewriteaof_in_progress:0
total_connections_received:5
total_commands_processed:4
role:master
db0:keys=1,expires=0