转载请注明出处:http://blog.csdn.net/l1028386804/article/details/61952074
如今,越来越多的企业开始使用Redis了。很多朋友给我发来私信,问如何才能搭建一个Redis集群。那么,今天,我就给大家带来一篇关于Redis主从配置的文章,好了,不多说了,我们直接进入主题。
一、创建相关目录
# mkdir -p /usr/local/redis/{etc,bin,var}
# mkdir -p /data/logs/redis /data/dbcache
二、编译安装Redis
# tar -xvzf redis-2.4.13.tar.gz
# cd redis-2.4.13
# make
# cd src && cp redis-server redis-cli redis-benchmark /usr/local/redis/bin
# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
# /sbin/sysctl -p
三、主服务器设置(192.168.1.100)
# vim /usr/local/redis/etc/redis.conf
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
bind 192.168.1.100
unixsocket /usr/local/redis/var/redis.sock
unixsocketperm 755
timeout 300
loglevel verbose
logfile /data/logs/redis/redis.log
# syslog-enabled no
# syslog-ident redis
# syslog-facility local0
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data/dbcache
# slaveof <masterip> <masterport>
# masterauth <master-password>
# repl-ping-slave-period 10
# repl-timeout 60
requirepass redis123
# rename-command CONFIG ""
maxclients 0
# maxmemory <bytes>
# maxmemory-policy volatile-lru
# maxmemory-samples 3
appendonly no
appendfilename appendonly.aof
appendfsync always
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 128
vm-enabled no
vm-swap-file /data/dbcache/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 512
hash-max-zipmap-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
四、从服务器设置(192.168.1.200)
daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
bind 192.168.1.200
unixsocket /usr/local/redis/var/redis.sock
unixsocketperm 755
timeout 300
loglevel verbose
logfile /data/logs/redis/redis.log
# syslog-enabled no
# syslog-ident redis
# syslog-facility local0
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data/dbcache
# slaveof <masterip> <masterport>
# masterauth <master-password>
# repl-ping-slave-period 10
# repl-timeout 60
# rename-command CONFIG ""
maxclients 0
# maxmemory <bytes>
# maxmemory-policy volatile-lru
# maxmemory-samples 3
appendonly no
appendfilename appendonly.aof
appendfsync always
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 128
vm-enabled no
vm-swap-file /data/dbcache/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 512
hash-max-zipmap-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
slave-serve-stale-data yes
slaveof 192.168.1.100 6379
masterauth redis123
五、启动服务
# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
主服务器上执行:
# /usr/local/redis/bin/redis-cli -h 192.168.1.100 -a redis123 set test 123456
从服务器上执行:
# /usr/local/redis/bin/redis-cli -h 192.168.1.200 get test
六、启动脚本
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /usr/local/redis/etc/redis.conf
# pidfile: /usr/local/redis/var/redis.pid
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/redis/bin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/usr/local/redis/etc/redis.conf"
lockfile="/var/lock/subsys/redis"
start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
$redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
/sbin/killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
sleep 1
start
}
reload() {
echo -n $"Reloading $prog: "
/sbin/killproc $redis -HUP
RETVAL=$?
echo
return $retval
}
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
reload)
$1
;;
*)
echo $"Usage: $0 {start|stop|restart|reload}"
exit 2
esac
七、启动服务
# chmod 700 /etc/init.d/redis
# chkconfig --add redis
# service redis start
八、测试是否已启动
# /usr/local/redis/bin/redis-cli ping
九、性能测试
# /usr/local/redis/bin/redis-benchmark
十、关闭服务
# /usr/local/redis/bin/redis-cli -p 6379 shutdown
十一、强制刷新数据到磁盘【Redis默认是异步写入磁盘的】
# /usr/local/redis/bin/redis-cli -p 6379 save