Redis主从配置

什么是主从复制

持久化保证了即便 redis 服务重启也会丢失数据,由于 redis 服务重启后会将硬盘上持久化的数据恢复到内存中,可是当 redis 服务器的硬盘损坏了可能会致使数据丢失,若是经过 redis 的主从复制机制就能够避免这种单点故障。redis

一台机上作主从配置

#复制配置文件并更名为redis2.conf
cp /etc/redis.conf /etc/redis2.conf
#修改配置文件的内容以下:服务器

port 6380
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis2.log"
dir /data/redis2
slaveof 127.0.0.1 6379  #增长此行
#若是主上设置了密码,还须要增长
masterauth password>com //password是主的密码

建立/data/redis2目录,并启动redis服务tcp

[root@jin-10 ~]# mkdir /data/redis2
[root@jin-10 ~]# redis-server /etc/redis2.conf
[root@jin-10 ~]# ps aux |grep redis
root       1627  0.0  0.2 145416  2588 ?        Ssl  09:28   0:04 redis-server 127.0.0.1:6379
root       1812  0.5  0.2 145300  2288 ?        Ssl  10:45   0:00 redis-server 127.0.0.1:6380
root       1824  0.0  0.0 112724   992 pts/0    S+   10:45   0:00 grep --color=auto redis
[root@jin-10 ~]# netstat -lntp |grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      1627/redis-server 1 
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      1812/redis-server 1

能够看到,两个redis服务都已经起来了,监听的端口分别是6379和6380。测试

测试主从

127.0.0.1:6379> keys *
 1) "mykey"
 2) "seta"
 3) "user"
 4) "key"
 5) "key3"
 6) "set2"
 7) "list"
 8) "key2"
 9) "set1"
10) "info"
127.0.0.1:6379> exit
[root@jin-10 ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> keys *
 1) "key3"
 2) "key"
 3) "mykey"
 4) "set1"
 5) "seta"
 6) "user"
 7) "set2"
 8) "key2"
 9) "info"
10) "list"

能够看到,从上会实时自动同步主上的数据。code

相关文章
相关标签/搜索