Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。Redis 是一个高性能的key-value数据库。
redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP客户端,使用很方便。(以上内容摘自百度百科)
一:下载redis并安装
- [root@server11 ~]# wget http://redis.googlecode.com/files/redis-2.6.4.tar.gz
- [root@server11 ~]# tar -zxvpf redis-2.6.4.tar.gz
- [root@server11 ~]# cd redis-2.6.4
- [root@server11 redis-2.6.4]# make install
32位操作系统上可能会出现如下报错:
zmalloc.o: In function `zmalloc_used_memory':
/root/redis-2.6.4/src/zmalloc.c:223: undefined reference to `__sync_add_and_fetch_4'
collect2: ld returned 1 exit status
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/root/redis-2.6.4/src'
make: *** [all] Error 2
解决方案参考:
https://groups.google.com/forum/?fromgroups#!topic/redis-db/NI_d00he39o
二:编辑redis配置文件,启动redis
- [root@server11 ~]# mkdir -p /usr/local/redis2/{bin,etc,var}
- [root@server11 redis-2.6.4]# cp redis.conf /usr/local/redis2/etc/
- [root@server11 redis-2.6.4]# cp /usr/local/bin/redis-* /usr/local/redis2/bin/
- [root@server11 ~]# grep -v '^#' /usr/local/redis2/etc/redis.conf |grep -v '^$' //各参数含义在配置文件中有具体的解释和介绍
- daemonize yes
- pidfile /usr/local/redis2/var/redis.pid
- port 6379
- bind 192.168.1.112
- timeout 300
- loglevel notice
- logfile stdout
- databases 16
- save 900 1
- save 300 10
- save 60 10000
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
- dbfilename dump.rdb
- dir ./
- slave-serve-stale-data yes
- slave-read-only yes
- slave-priority 100
- appendonly no
- 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
- [root@server11 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
- [root@server11 ~]# netstat -ntpl |grep 6379
- tcp 0 0 192.168.1.112:6379 0.0.0.0:* LISTEN 6044/redis-server
- [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112
- redis 192.168.1.112:6379> set number 100
- OK
- redis 192.168.1.112:6379> get number
- "100"
- redis 192.168.1.112:6379> del number
- (integer) 1
- redis 192.168.1.112:6379> get number
- (nil)
- redis 192.168.1.112:6379> quit
三: redis主从配置,关闭主节点的redis实例,复制/usr/local/redis2/目录至从节点
- [root@server11 ~]# ps -ef |grep redis
- root 6044 1 0 09:37 ? 00:00:00 /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
- root 6072 27475 0 09:44 pts/1 00:00:00 grep redis
- [root@server11 ~]# kill 6044
- [root@server11 ~]# scp -rvp /usr/local/redis2/ root@192.168.1.113:/usr/local/
- [root@server12 ~]# grep -v '^#' /usr/local/redis2/etc/redis.conf |grep -v '^$' //注意红色字体部分
- daemonize yes
- pidfile /usr/local/redis2/var/redis.pid
- port 6379
- bind 192.168.1.113
- timeout 300
- loglevel notice
- logfile stdout
- databases 16
- save 900 1
- save 300 10
- save 60 10000
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
- dbfilename salve.rdb
- dir ./
- slaveof 192.168.1.112 6379
- slave-serve-stale-data yes
- slave-read-only yes
- slave-priority 100
- appendonly no
- 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
四:分别启动主从节点redis实例并测试
- [root@server11 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
- [root@server12 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
- [root@server12 ~]# netstat -ntpl |grep 6379
- tcp 0 0 192.168.1.113:6379 0.0.0.0:* LISTEN 16653/redis-server
- [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112
- redis 192.168.1.112:6379> set number 1
- OK
- redis 192.168.1.112:6379> get number
- "1"
- redis 192.168.1.112:6379> quit
- [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113
- redis 192.168.1.113:6379> get number
- "1"
- redis 192.168.1.113:6379> del number
- (error) READONLY You can't write against a read only slave.
- redis 192.168.1.113:6379> quit
本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/1065549如需转载请自行联系原作者
ylw6006