一:下载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
Powered by: C++博客 Copyright © 鑫龙