redis和memcache比较像的,memcache可以实现服务器的集群,redis肯定也是可以的。下面在一台机,实现redis主从复制。
1,copy一下redis.conf,生成一个从机的配置
cp /usr/local/redis/redis.conf /usr/local/redis/redis_slave.conf
2,修改主服务器的配置redis.conf
bind 127.0.0.1
3,修改从服务器的配置redis_slave.conf
pidfile /usr/local/redis/var/redis_slave.pid
port 6380
bind 127.0.0.1
logfile /usr/local/redis/var/redis_slave.log
dbfilename dump_slave.rdb
slaveof 127.0.0.1 6379
4,启动主服务器,从服务器
redis-server /usr/local/redis/redis.conf
redis-server /usr/local/redis/redis_slave.conf
查看一下,redis.log文件会有以下内容
[6956] 11 Apr 17:07:14 * Server started, Redis version 2.4.10
[6956] 11 Apr 17:07:14 # 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.
[6956] 11 Apr 17:07:14 * The server is now ready to accept connections on port 6379
[6956] 11 Apr 17:22:15 * 1 changes in 900 seconds. Saving...
[6956] 11 Apr 17:22:15 * Background saving started by pid 7154
[7154] 11 Apr 17:22:15 * DB saved on disk
[6956] 11 Apr 17:22:15 * Background saving terminated with success
[6956] 11 Apr 18:30:14 * 1 changes in 900 seconds. Saving...
[6956] 11 Apr 18:30:14 * Background saving started by pid 17740
[17740] 11 Apr 18:30:14 * DB saved on disk
[6956] 11 Apr 18:30:14 * Background saving terminated with success
redis_slave.log
[24786] 13 Apr 16:07:55 * Server started, Redis version 2.4.10
[24786] 13 Apr 16:07:55 # 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.
[24786] 13 Apr 16:07:55 * The server is now ready to accept connections on port 6380
[24786] 13 Apr 16:07:55 * Connecting to MASTER...
[24786] 13 Apr 16:07:55 * MASTER <-> SLAVE sync started
[24786] 13 Apr 16:07:55 * Non blocking connect for SYNC fired the event.
[24786] 13 Apr 16:07:55 * MASTER <-> SLAVE sync: receiving 142 bytes from master
[24786] 13 Apr 16:07:55 * MASTER <-> SLAVE sync: Loading DB in memory
[24786] 13 Apr 16:07:55 * MASTER <-> SLAVE sync: Finished with success
查看一下
ps -ef|grep redis
root 24786 1 0 16:07 ? 00:00:00 /usr/local/bin/redis-server /usr/local/redis-2.4.10/redis_slave.conf
root 24812 1 0 16:18 ? 00:00:00 /usr/local/bin/redis-server /etc/redis.conf
root 24821 24586 0 16:22 pts/0 00:00:00 grep redis
5,测试结果
连接主服务器
/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set fbbin binbin
OK
redis 127.0.0.1:6379> set webname www.inhai.com
"www.inhai.com"
redis 127.0.0.1:6379> exit
========================================
连接从服务器
/usr/local/bin/redis-cli -h 127.0.0.1 -p 6380
redis 127.0.0.1:6380> get webname
"www.inhai.com"
redis 127.0.0.1:6380> exit
尝试了一下,redis的主主同步,但没有成功,查看了一下redis_slave.log
[24786] 13 Apr 16:14:57 * Connecting to MASTER...
[24786] 13 Apr 16:14:57 * MASTER <-> SLAVE sync started
[24786] 13 Apr 16:14:57 * Non blocking connect for SYNC fired the event.
[24786] 13 Apr 16:14:57 # I/O error writing to MASTER: Connection refused
redis.log
[24801] 13 Apr 16:15:22 * Connecting to MASTER...
[24801] 13 Apr 16:15:22 * MASTER <-> SLAVE sync started
[24801] 13 Apr 16:15:22 * Non blocking connect for SYNC fired the event.
[24801] 13 Apr 16:15:22 # MASTER aborted replication with an error: ERR Can't SYNC while not connected with my master
本文介绍了如何在单台服务器上配置Redis的主从复制,包括复制配置的修改、启动主从服务器,并通过测试验证主从同步的效果。在Redis主从同步过程中,详细记录了配置步骤和日志信息。

被折叠的 条评论
为什么被折叠?



