天天看点

Redis主从配置

准备:192.168.3.76上启动两个端口,模拟两台机器。

1、[root@centos7-2 ~]# cp /etc/redis.conf /etc/redis2.conf

更改端口、日志文件

2、[root@centos7-2 ~]# vim /etc/redis2.conf

port 6380

pidfile /var/run/redis_6380.pid

logfile "/tmp/logs/redis2.log"

dir /data/redis2

#slaveof <masterip> <masterport>

slaveof 127.0.0.1 6379

###指定主服务器IP和端口

#masterauth <master-password>

###如果主服务器设定了密码,需要在从服务器上添加该参数

3、启动

[root@centos7-2 ~]# redis-server /etc/redis.conf

[root@centos7-2 ~]# redis-server /etc/redis2.conf

[root@centos7-2 ~]# netstat -nutlp| grep redis

tcp 0 0 127.0.0.1:6379 0.0.0.0: LISTEN 18001/redis-server 

tcp 0 0 127.0.0.1:6380 0.0.0.0: LISTEN 18684/redis-server

4、测试,有数据

[root@centos7-2 ~]# redis-cli -h 127.0.0.1 -p 6380

127.0.0.1:6380> keys *

1) "key3"

2) "key2"

3) "key1"

5、配置完成,和mysql不同的时,不用同步数据库,redis会自动同步到从服务器上

6、从服务上不能写入数据

Redis主从配置

因为在master上slave-read-only yes ,所以只能读

本文转自 jiekegz  51CTO博客,原文链接:http://blog.51cto.com/jacksoner/2049321