redis分布式主从同步_负载集群教程

编辑Tag赚U币

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 10002

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文件会有以下内容

[3774] 27 Jun 12:53:32 * The server is now ready to accept connections on port 10002

[3774] 27 Jun 12:53:32 - 0 clients connected (0 slaves), 539568 bytes in use

[3774] 27 Jun 12:53:32 * Connecting to MASTER...

[3772] 27 Jun 12:53:32 - Accepted 127.0.0.1:33237

[3772] 27 Jun 12:53:32 * Slave ask for synchronization

[3772] 27 Jun 12:53:32 * Starting BGSAVE for SYNC

[3772] 27 Jun 12:53:32 * Background saving started by pid 3775

[3774] 27 Jun 12:53:32 * MASTER <-> SLAVE sync started: SYNC sent

[3775] 27 Jun 12:53:32 * DB saved on disk

[3772] 27 Jun 12:53:33 * Background saving terminated with success

[3772] 27 Jun 12:53:33 * Synchronization with slave succeeded

[3774] 27 Jun 12:53:33 * MASTER <-> SLAVE sync: receiving 333 bytes from master

[3774] 27 Jun 12:53:33 * MASTER <-> SLAVE sync: Loading DB in memory

[3774] 27 Jun 12:53:33 * MASTER <-> SLAVE sync: Finished with success

查看一下

root@ubuntu:/usr/local/redis# ps -e|grep redis

3774 ? 00:00:00 redis-server

4148 ? 00:00:00 redis-server

5,测试结果

查看复制打印?
1.<?php  
2.$redis = new redis();  
3.$redis->connect('127.0.0.1', 6379);  
4.$redis->set('test',json_encode(array(1,2,3,4)));  
5.print_r(json_decode($redis->get('test')));  
6.?> 
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',json_encode(array(1,2,3,4)));
print_r(json_decode($redis->get('test')));
?>root@ubuntu:/usr/local/redis# telnet 127.0.0.1 10002

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get test

$9

[1,2,3,4]

缓存数据是放在6379这个端口的服务器上,代码执行后,登录到10002端口的slave服务器上也可以看到有数据,说明已经同步了。

我尝试了一下,redis的主主同步,但是没有成功,我看了一下log,以从服务器127.0.0.1 10002为master的时候,同时启动二个端口,怎么也连接不上127.0.0.1 6379是不支持呢?还是配置有问题,我在网上查了一下,没看到有人配置主主同步的。

来源:网络搜集//所属分类:负载集群教程/更新时间:2012-06-21
相关负载集群教程