配置环境:
Master服务器 : 192.168.189.129:44444
Slave服务器 : 192.168.189.131:44444
一:下载安装redis软件包,并解压
# cd /usr/local/src# wget http://redis.googlecode.com/files/redis-2.6.4.tar.gz # tar -zxvf redis-2.6.4.tar.gz# cd redis-2.6.4
二:安装
redis软件不依赖其他的库,也不需要额外的编译参数,可直接make install进行安装
# make install
三:创建所需目录与文件,便于管理
# mkdir -p /usr/local/redis/bin# mkdir -p /usr/local/redis/etc# mkdir -p /usr/local/redis/var# mkdir -p /usr/local/redis/data# cp redis.conf /usr/local/redis/etc/# cp /usr/local/bin/redis-* /usr/local/redis/bin
四:配置redis.conf文件如下
Master服务器端:
# grep -v '^#' redis.conf |grep -v '^$' daemonize yes pidfile /usr/local/redis/var/redis.pid#定义端口port 44444#绑定IPbind 192.168.189.129timeout 300 loglevel notice#指定日志目录logfile /usr/local/redis/var/redis.logdatabases 16#配置持久化(rdb模式)save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yes#定义数据文件和目录dbfilename master.rdbdir /usr/local/redis/dataslave-serve-stale-data yesslave-read-only yesslave-priority 100appendonly noappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb60client-output-buffer-limit pubsub 32mb 8mb 60
Slave服务器端:
# grep -v '^#' redis.conf |grep -v '^$' daemonize yes pidfile /usr/local/redis/var/redis.pidport 44444 bind 192.168.189.131timeout 300 loglevel noticelogfile /usr/local/redis/var/redis.log databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename slave.rdbdir /usr/local/redis/data slaveof 192.168.189.129 44444 ------指定master的地址slave-serve-stale-data yesslave-read-only yesslave-priority 100appendonly noappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb60client-output-buffer-limit pubsub 32mb 8mb 60
五:依次开启主从redis,并测试
Master:# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.confSlave:# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
使用客户端连接主redis,并进行一些简单操作
# /usr/local/redis/bin/redis-cli -h 192.168.189.129 -p 44444
连接从redis,查看数据是否同步
# /usr/local/redis/bin/redis-cli -h 192.168.189.131 -p 44444
从上可看到数据已同步,证明redis主从配置生效
注:由于在配置文件中配置了slave-read-only yes,因此从节点处于只读状态,无法对数据库进行操作和更改。