redis主从

redis主从配置:

原理(增量同步):

从服务器-------发送sync到 ------>

                        主服务器----BGSAVE--->

                                    主服务器将保存的RDB文件发送给从服务器

                                                            从服务器收到RDB文件后,重载到从服务器的内存中,

                                                                                                           一次同步完成                                         

在同一台机器上启两个进程

修改配置文件

[root@node120 redis326]# ls

6379.conf 6380.conf

[root@node120 redis326]# pwd

/etc/redis326

 

在配置文件6380.conf中将6379修改成6380

[root@node120 redis326]# diff 6380.conf 6379.conf

83c83

< # Accept connections on the specified port, defaultis 6380 (IANA #815344).

---

> # Accept connections on the specified port, defaultis 6379 (IANA #815344).

85c85

< port 6380

---

> port 6379

152c152

< pidfile /var/run/redis_6380.pid

---

> pidfile /var/run/redis_6379.pid

239c239

< dbfilename dump_6380.rdb

---

> dbfilename dump_6379.rdb

600c600

< appendfilename "appendonly_6380.aof"

---

> appendfilename "appendonly_6379.aof"

732c732

< # cluster-config-file nodes-6380.conf

---

> # cluster-config-file nodes-6379.conf

 

手动启动redis:

[root@node120 redis326]#/usr/local/redis326/bin/redis-server 6379.conf

[root@node120 redis326]#/usr/local/redis326/bin/redis-server 6380.conf

[root@node120 redis326]# pwd

/etc/redis326

 

[root@node120 redis326]# ps -ef | grep redis

root     7244     1  0 15:15 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:6379

root     7248     1  0 15:15 ?        00:00:00 /usr/local/redis326/bin/redis-server192.168.2.120:6380

root     7252  6687  0 15:16 pts/1    00:00:00 grep --color=auto redis

 

将6380作为6379的从数据库:

192.168.2.120:6380> slaveof 192.168.2.120 6379     #也可以将这个配置写入配置文件中

OK

 

192.168.2.120:6380> info

# Replication

role:slave

master_host:192.168.2.120

master_port:6379

master_link_status:up

master_last_io_seconds_ago:7

master_sync_in_progress:0

slave_repl_offset:57

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

 

192.168.2.120:6379> info

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.2.120,port=6380,state=online,offset=127,lag=0

master_repl_offset:127

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:126

 

 

测试

master:

192.168.2.120:6379> keys *

(empty list or set)

192.168.2.120:6379> set key1 rscpass

OK

192.168.2.120:6379> keys *

1) "key1"

 

slave:

192.168.2.120:6380> keys *

1) "key1"

 

 

从服务器是只读的,不能写入数据:

192.168.2.120:6380> set key1 12

(error) READONLY You can't write against a read onlyslave.

 

 

 

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS