redis主从配置

来源:互联网 发布:mac下安装ant 编辑:程序博客网 时间:2024/04/20 00:40
root@debian:/usr/local/redis/etc# cp redis.conf redis.master.conf
root@debian:/usr/local/redis/etc# cp redis.conf redis.slave.conf

master listen on port 6380
slave listen on port 6381

在配置文件redis.slave.conf中加入
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
# slaveof <masterip> <masterport>
slaveof localhost 6380

启动主从服务,先启动主redis,再启动从 redis,因为从redis要依赖主redis。
root@debian:/usr/local/redis/etc# redis-server redis.master.conf
New pid: 2553
root@debian:/usr/local/redis/etc# redis-server redis.slave.conf
New pid: 2570

root@debian:/usr/local/redis# more redis.master.log
21 Jun 17:56:03 . Accepted 127.0.0.1:47892
21 Jun 17:56:03 - Slave ask for synchronization
21 Jun 17:56:03 - Starting BGSAVE for SYNC
21 Jun 17:56:03 - Background saving started by pid 2571
21 Jun 17:56:03 - DB saved on disk
21 Jun 17:56:03 . 0 clients connected (1 slaves), 412423 bytes in use, 0 shared objects
21 Jun 17:56:03 - Background saving terminated with success
21 Jun 17:56:03 - Synchronization with slave succeeded

root@debian:/usr/local/redis# more redis.slave.log
21 Jun 17:53:49 * Opening TCP port: bind: Address already in use

21 Jun 17:56:01 - Server started, Redis version 1.2.6
21 Jun 17:56:02 * WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. 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.
21 Jun 17:56:02 - The server is now ready to accept connections on port 6381
21 Jun 17:56:03 . 0 clients connected (0 slaves), 412229 bytes in use, 0 shared objects
21 Jun 17:56:03 - Connecting to MASTER...
21 Jun 17:56:03 - Receiving 10 bytes data dump from MASTER
21 Jun 17:56:03 - MASTER <-> SLAVE sync succeeded
21 Jun 17:56:08 . 1 clients connected (0 slaves), 412382 bytes in use, 0 shared objects
21 Jun 17:56:13 . 1 clients connected (0 slaves), 412382 bytes in use, 0 shared objects
21 Jun 17:56:18 . 1 clients connected (0 slaves), 412382 bytes in use, 0 shared objects

测试主从数据同步
先在主redis从写入一条数据
root@debian:/usr/local/redis# telnet localhost 6380
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set name 10
caihuafeng
+OK
get name
$10
caihuafeng

telnet到从redis后就可以直接看到在主redis中写入的数据了。
root@debian:/usr/local/redis# telnet localhost 6381
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get name
$10
caihuafeng

主从redis对应的数据文件也是一样的。
root@debian:/usr/local/redis# more dump.master.rdb
REDIS0001oot@debian:/usr/local/redis# more dump.slave.rdb
REDIS0001
原创粉丝点击