当前位置: 首页 > redis, 分布式系统, 缓存系统 > 正文

redis主从同步配置实例

1 星2 星3 星4 星5 星 (2 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

在一台机,实现redis主从复制:

1,copy一下redis.conf,生成一个从机的配置

1
cp /usr/local/redis/redis.conf /usr/local/redis/redis_slave.conf

2,修改主服务器的配置redis.conf

1
bind 127.0.0.1

3,修改从服务器的配置redis_slave.conf

1
2
3
4
5
6
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,启动主服务器,从服务器

1
2
redis-server /usr/local/redis/redis.conf
redis-server /usr/local/redis/redis_slave.conf

查看一下,redis.log文件会有以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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
2
3
4
5
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')));  
?>
1
2
3
4
5
6
7
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服务器上也可以看到有数据,说明已经同步了。

本文固定链接: http://www.chepoo.com/redis-master-slave-config.html | IT技术精华网

redis主从同步配置实例:目前有1 条留言

  1. 沙发
    :

    学习了~

    [回复]

发表评论