redis学习系列(十一)--redis-复制功能实践

来源:互联网 发布:免费越狱软件 编辑:程序博客网 时间:2024/06/07 06:14

关于复制功能,第十篇只是将原理介绍了一下,有个基本概念,紧接着这一篇就是去实践一下。

基于第九篇的集群,我们选择redis01和redis02去实践复制功能。

首先需要关闭redis01和redis02的集群功能,不然会报错,单纯的复制模式下,集群参数不能开启。

redis01无须配置

redis02需要配置,我的redis01的端口号是7001

slaveof 127.0.0.1 7001
启动redis01,查看日志如下:

3945:M 27 Oct 09:08:21.989 * Increased maximum number of open files to 10032 (it was originally set to 1024).                _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 3.0.0 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 7001 |    `-._   `._    /     _.-'    |     PID: 3945  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               3945:M 27 Oct 09:08:22.008 # Server started, Redis version 3.0.03945:M 27 Oct 09:08:22.008 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.3945:M 27 Oct 09:08:22.008 # 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.3945:M 27 Oct 09:08:22.008 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.3945:M 27 Oct 09:08:22.008 * The server is now ready to accept connections on port 70013945:M 27 Oct 09:08:31.395 * Slave 127.0.0.1:7002 asks for synchronization3945:M 27 Oct 09:08:31.395 * Full resync requested by slave 127.0.0.1:70023945:M 27 Oct 09:08:31.395 * Starting BGSAVE for SYNC with target: disk3945:M 27 Oct 09:08:31.396 * Background saving started by pid 39703970:C 27 Oct 09:08:31.399 * DB saved on disk3970:C 27 Oct 09:08:31.400 * RDB: 0 MB of memory used by copy-on-write3945:M 27 Oct 09:08:31.500 * Background saving terminated with success3945:M 27 Oct 09:08:31.500 * Synchronization with slave 127.0.0.1:7002 succeeded
启动redis02,查看日志:启动之后可以连接到主服务器。

3964:S 27 Oct 09:08:30.392 * Increased maximum number of open files to 10032 (it was originally set to 1024).                _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 3.0.0 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 7002 |    `-._   `._    /     _.-'    |     PID: 3964  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               3964:S 27 Oct 09:08:30.394 # Server started, Redis version 3.0.03964:S 27 Oct 09:08:30.394 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.3964:S 27 Oct 09:08:30.394 # 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.3964:S 27 Oct 09:08:30.394 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.3964:S 27 Oct 09:08:30.394 * The server is now ready to accept connections on port 70023964:S 27 Oct 09:08:31.395 * Connecting to MASTER 127.0.0.1:70013964:S 27 Oct 09:08:31.395 * MASTER <-> SLAVE sync started3964:S 27 Oct 09:08:31.395 * Non blocking connect for SYNC fired the event.3964:S 27 Oct 09:08:31.395 * Master replied to PING, replication can continue...3964:S 27 Oct 09:08:31.395 * Partial resynchronization not possible (no cached master)3964:S 27 Oct 09:08:31.396 * Full resync from master: c2a13a9cbc0d5ff6634c62d6d36fc98d9a05e508:13964:S 27 Oct 09:08:31.500 * MASTER <-> SLAVE sync: receiving 18 bytes from master3964:S 27 Oct 09:08:31.500 * MASTER <-> SLAVE sync: Flushing old data3964:S 27 Oct 09:08:31.500 * MASTER <-> SLAVE sync: Loading DB in memory3964:S 27 Oct 09:08:31.500 * MASTER <-> SLAVE sync: Finished with success3964:S 27 Oct 09:08:31.500 * Background append only file rewriting started by pid 39713964:S 27 Oct 09:08:31.578 * AOF rewrite child asks to stop sending diffs.3971:C 27 Oct 09:08:31.578 * Parent agreed to stop sending diffs. Finalizing AOF...3971:C 27 Oct 09:08:31.578 * Concatenating 0.00 MB of AOF diff received from parent.3971:C 27 Oct 09:08:31.578 * SYNC append only file rewrite performed3971:C 27 Oct 09:08:31.578 * AOF rewrite: 0 MB of memory used by copy-on-write3964:S 27 Oct 09:08:31.597 * Background AOF rewrite terminated with success3964:S 27 Oct 09:08:31.597 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)3964:S 27 Oct 09:08:31.597 * Background AOF rewrite finished successfully

在我试验时redis01和redis02都是开启了aof和rdb模式的。
进入redis01:此时没有数据

[root@localhost redis-cluster]# cd redis01[root@localhost redis01]# ./redis-cli -p 7001127.0.0.1:7001> keys *(empty list or set)127.0.0.1:7001> 
进入redis02,也是没有数据,有数据也会被同步掉了,因为redis02进行了复制,而redis01里面没有数据,所以这边需要注意下,如果是运行一段时间过后需要复制时,千万不能直接重启,要备份它的rdb文件,但是了,一般进行备份时也是重新加入的机器,不存在数据的,

在redis01上执行set操作:

127.0.0.1:7001> set key "this is a key"OK127.0.0.1:7001> set k "this is a k"OK127.0.0.1:7001> 

在redis02上执行查询:

127.0.0.1:7002> get key"this is a key"127.0.0.1:7002> get k"this is a k"127.0.0.1:7002> 
此时redis02是没有写权限的:

127.0.0.1:7002> set kk "yh"(error) READONLY You can't write against a read only slave.127.0.0.1:7002> 

但是我去翻看日志时,没有查看到复制时的日志是怎么保存的,奇怪。

下面接着试一下心跳机制:

从服务器redis02配置如下:

repl-ping-slave-period 10
主服务器redis01配置如下:

min-slaves-to-write 3min-slaves-max-lag 10


在从服务器上执行PING操作,可以得到返回POING。

但是,但是,log里面没有发现对应的日志记录,百度了一圈,没发现啥,以后的测试全记录在此。

原创粉丝点击