Redis 3.2.1:Deploy Master / Slave On RHEL5U10
如题所示,本文将呈现Redis中,配置主辅同步架构的所有细节。
Master:192.168.232.139
Slave:192.168.232.140
Redis Port:6379
Master:
配置文件(redis.conf)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
[root@redis1 ~]# cat /etc/redis.conf | grep -v ^# | strings bind 192.168.232.139 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "/var/log/redis.log" 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 repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly no appendfilename "appendonly.aof" appendfsync everysec 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 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 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 [root@redis1 ~]# |
启动Redis(Master):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@redis1 ~]# ps -ef | grep redis root 28707 24923 0 18:27 pts/0 00:00:00 grep redis [root@redis1 ~]# [root@redis1 ~]# netstat -tupln | grep redis [root@redis1 ~]# [root@redis1 ~]# redis-server /etc/redis.conf [root@redis1 ~]# [root@redis1 ~]# ps -ef | grep redis root 28711 1 0 18:28 ? 00:00:00 redis-server 127.0.0.1:6379 root 28715 24923 0 18:28 pts/0 00:00:00 grep redis [root@redis1 ~]# [root@redis1 ~]# netstat -tupln | grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 28711/redis-server [root@redis1 ~]# [root@redis1 ~]# |
开启对日志文件的监视:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[root@redis1 ~]# tail -f /var/log/redis.log 28743:M 09 Jul 18:34:47.942 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 28743 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 28743:M 09 Jul 18:34:47.944 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 28743:M 09 Jul 18:34:47.944 # Server started, Redis version 3.2.1 28743:M 09 Jul 18:34:47.944 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 28743:M 09 Jul 18:34:47.945 * DB loaded from disk: 0.000 seconds 28743:M 09 Jul 18:34:47.945 * The server is now ready to accept connections on port 6379 00000000000000000000000000000000000 (... ... 等待。) |
Slave:
配置文件:
1 2 3 |
[root@redis2 ~]# cat /etc/redis.conf | grep --color ^slaveof slaveof 192.168.232.139 6379 [root@redis2 ~]# |
一样的,开启对日志的监视:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@redis2 ~]# tail -f /var/log/redis.log 28646:M 09 Jul 18:06:10.660 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 28646:M 09 Jul 18:06:10.660 # Server started, Redis version 3.2.1 28646:M 09 Jul 18:06:10.661 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 28646:M 09 Jul 18:06:10.661 * The server is now ready to accept connections on port 6379 28646:M 09 Jul 18:14:56.705 # User requested shutdown... 28646:M 09 Jul 18:14:56.705 * Saving the final RDB snapshot before exiting. 28646:M 09 Jul 18:14:56.757 * DB saved on disk 28646:M 09 Jul 18:14:56.757 * Removing the pid file. 28646:M 09 Jul 18:14:56.757 # Redis is now ready to exit, bye bye... ---------------------------------------------------------------- |
启动数据库(Slave):
1 2 3 4 5 6 7 8 9 10 11 |
[root@redis2 ~]# ps -ef | grep redis root 28682 28667 0 18:29 pts/1 00:00:00 tail -f /var/log/redis.log root 28688 24919 0 18:31 pts/0 00:00:00 grep redis [root@redis2 ~]# [root@redis2 ~]# redis-server /etc/redis.conf [root@redis2 ~]# [root@redis2 ~]# ps -ef | grep redis root 28682 28667 0 18:29 pts/1 00:00:00 tail -f /var/log/redis.log root 28690 1 0 18:31 ? 00:00:00 redis-server 127.0.0.1:6379 root 28694 24919 0 18:31 pts/0 00:00:00 grep redis [root@redis2 ~]# |
日志,Slave:
1 2 3 4 5 6 7 8 9 10 |
28690:S 09 Jul 18:34:48.346 * Connecting to MASTER 192.168.232.139:6379 28690:S 09 Jul 18:34:48.346 * MASTER <-> SLAVE sync started 28690:S 09 Jul 18:34:48.347 * Non blocking connect for SYNC fired the event. 28690:S 09 Jul 18:34:48.347 * Master replied to PING, replication can continue... 28690:S 09 Jul 18:34:48.347 * Partial resynchronization not possible (no cached master) 28690:S 09 Jul 18:34:48.351 * Full resync from master: cfea537491cb613900534632466dcc7d25ac043b:1 28690:S 09 Jul 18:34:48.391 * MASTER <-> SLAVE sync: receiving 76 bytes from master 28690:S 09 Jul 18:34:48.391 * MASTER <-> SLAVE sync: Flushing old data 28690:S 09 Jul 18:34:48.391 * MASTER <-> SLAVE sync: Loading DB in memory 28690:S 09 Jul 18:34:48.391 * MASTER <-> SLAVE sync: Finished with success |
日志,Master:
1 2 3 4 5 6 7 8 |
28743:M 09 Jul 18:34:48.306 * Slave 192.168.232.140:6379 asks for synchronization 28743:M 09 Jul 18:34:48.306 * Full resync requested by slave 192.168.232.140:6379 28743:M 09 Jul 18:34:48.306 * Starting BGSAVE for SYNC with target: disk 28743:M 09 Jul 18:34:48.307 * Background saving started by pid 28746 28746:C 09 Jul 18:34:48.325 * DB saved on disk 28746:C 09 Jul 18:34:48.326 * RDB: 6 MB of memory used by copy-on-write 28743:M 09 Jul 18:34:48.349 * Background saving terminated with success 28743:M 09 Jul 18:34:48.349 * Synchronization with slave 192.168.232.140:6379 succeeded |
主库增加几条记录:
1 2 3 4 5 6 7 8 9 |
[root@redis1 ~]# redis-cli -h 192.168.232.139 -p 6379 192.168.232.139:6379> set a_name adamhuan OK 192.168.232.139:6379> set b_name allah OK 192.168.232.139:6379> keys * 1) "b_name" 2) "a_name" 192.168.232.139:6379> |
去备库查看一下:
1 2 3 4 5 6 7 8 |
[root@redis2 ~]# netstat -tupln | grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 28690/redis-server [root@redis2 ~]# [root@redis2 ~]# redis-cli -p 6379 127.0.0.1:6379> keys * 1) "a_name" 2) "b_name" 127.0.0.1:6379> |
这样,Redis的Master-slave架构的主辅同步就搭建完成了。
————————————————
Done。