一,先附上配置文件
1,master(6379.conf)上面的配置文件
daemonize yes pidfile /usr/local/redis/logs/redis_6379.pid port 6379 tcp-backlog 1024 bind 0.0.0.0 timeout 0 tcp-keepalive 60 loglevel notice logfile "/usr/local/redis/logs/redis_6379.log" databases 16 save "" #save 900 1 #save 300 10 #save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /usr/local/redis/data/ #如果做故障切换,不论主从节点都要填写密码且要保持一致 masterauth linlin slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 requirepass linlin appendonly yes appendfilename "appendonly.aof" # appendfsync always appendfsync everysec # appendfsync no no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 1000 slowlog-max-len 1000 latency-monitor-threshold 0 notify-keyspace-events "" 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 hll-sparse-max-bytes 3000 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 hz 10 aof-rewrite-incremental-fsync yes
2,slave1 (6380.conf)上面的配置文件
daemonize yes pidfile /usr/local/redis/logs/redis_6380.pid port 6380 tcp-backlog 1024 bind 0.0.0.0 timeout 0 tcp-keepalive 60 loglevel notice logfile "/usr/local/redis/logs/redis_6380.log" databases 16 save "" #save 900 1 #save 300 10 #save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /usr/local/redis/data/ slaveof 10.211.55.7 6379 #如果做故障切换,不论主从节点都要填写密码且要保持一致 masterauth linlin slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 requirepass linlin appendonly yes appendfilename "appendonly.aof" # appendfsync always appendfsync everysec # appendfsync no no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 1000 slowlog-max-len 1000 latency-monitor-threshold 0 notify-keyspace-events "" 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 hll-sparse-max-bytes 3000 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 hz 10 aof-rewrite-incremental-fsync yes
2,slave2(6381.conf)上面的配置文件
daemonize yes pidfile /usr/local/redis/logs/redis_6381.pid port 6381 tcp-backlog 1024 bind 0.0.0.0 timeout 0 tcp-keepalive 60 loglevel notice logfile "/usr/local/redis/logs/redis_6381.log" databases 16 save "" #save 900 1 #save 300 10 #save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /usr/local/redis/data/ slaveof 10.211.55.7 6379 #如果做故障切换,不论主从节点都要填写密码且要保持一致 masterauth linlin slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 requirepass linlin appendonly yes appendfilename "appendonly.aof" # appendfsync always appendfsync everysec # appendfsync no no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 1000 slowlog-max-len 1000 latency-monitor-threshold 0 notify-keyspace-events "" 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 hll-sparse-max-bytes 3000 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 hz 10 aof-rewrite-incremental-fsync ye
二,具体搭建过程(上述配置可以直接用于redis-sentinel集群使用)
1,搭建redis master
cd /usr/local/src
wget http://download.redis.io/releases/redis-3.0.7.tar.gz
tar -zxvf redis-3.0.7.tar.gz
cd redis-3.0.7
make
make PREFIX=/usr/local/redis-3.0.7 install
ln -s /usr/local/redis-3.0.7 /usr/local/redis
mkdir -p /usr/local/redis/conf
cp redis.conf /usr/local/redis/conf/6379.conf (这里可以直接用上面的master配置文件)
mkdir -p /usr/local/redis/data
mkdir -p /usr/local/redis/logs
注:
#为防止启动redis的时候,有警告信息,所以提前修改参数。
echo 'net.core.somaxconn= 2048' >> /etc/sysctl.conf
#这个配置和tcp-backlog有联系,但是tcp-backlog不能大于它。
echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled
sysctl -p
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
上述master端安装完毕,启动master端。
cd /usr/local/redis/bin/
./redis-server ../conf/6379.conf
2, master安装完成后,安装slave1端,我这里有2个slave端,基本上和master配置一样。slave端有明确的slave of master_ip master_port配置。
cd /usr/local/redis/conf
touch 6380.conf ,可以直接使用上slave1上的配置内容.
启动redis salve1节点。
cd /usr/local/redis/bin
./redis-server ../conf/6380.conf
3, 安装slave2,操作一样,省略..
cd cd /usr/local/redis/bin
./redis-server ../conf/6381.conf
4,基本上,redis主从搭建完毕,确认一下主从同步是否成功。
设置key,查看是否获取成功。
以上,表示redis主从搭建完毕。