复制配置文件
[root@yum-down ~]# cd /etc/redis/
[root@yum-down redis]# cp 6379.conf 6380.conf
修改端口,和数据文件名,或者保存位置即可‘dir‘
[root@yum-down redis]# vim 6380.conf
:%s/6379/6380/g
appendfilename "appendonly6380.aof"
[root@yum-down redis]# diff 6379.conf 6380.conf
46c46
< pidfile /var/run/redis_6379.pid
---
> pidfile /var/run/redis_6380.pid
48c48
< # Accept connections on the specified port, default is 6379.
---
> # Accept connections on the specified port, default is 6380.
50c50
< port 6379
---
> port 6380
182c182
< dbfilename dump_6379.rdb
---
> dbfilename dump_6380.rdb
645c645
< # cluster-config-file nodes-6379.conf
---
> # cluster-config-file nodes-6380.conf
[root@yum-down redis]#
启动:
[
root@yum-down redis]# cd /etc/redis/
[root@yum-down ~]# /usr/local/redis/bin/redis-server /etc/redis/6380.conf
[root@yum-down ~]# redis-cli -h 10.10.0.250 -p 6380
我们info后,发现默认两台机器都是master。6379改成从
# Replication
role:master
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
[root@yum-down redis]# netstat -ntlp |grep 6380
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 2315/redis-server
tcp 0 0 :::6380 :::* LISTEN 2315/redis-server
[root@yum-down redis]# netstat -ntlp |grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 2293/redis-server
tcp 0 0 :::6379 :::* LISTEN 2293/redis-server
[root@yum-down redis]#
在从服务器上,也就是6379上,修改自己为从,这条命令仍然可以写在配置文件中
通常,127.0.0.1:6379> CONFIG GET * config get *可以在线重置配置
127.0.0.1:6379> slaveof 10.10.0.250 6380
ok
127.0.0.1:6379> info
# Replication
role:slave
master_host:10.10.0.250
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1
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
在主上查看:
10.10.0.250:6380> info
# Replication
role:master
connected_slaves:1
slave0:ip=10.10.0.250,port=6379,state=online,offset=15,lag=1
master_repl_offset:15
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:14
在主上插入测试:
10.10.0.250:6380> set key1 linuxea
OK
10.10.0.250:6380> get key1
"linuxea"
10.10.0.250:6380>
在从上查看:
127.0.0.1:6379> get key1
"linuxea"
127.0.0.1:6379>
从redis2.6后开始,从复制就是只读的,并且支持主从平滑升级
由于本身redis单线程,支持一个cpu,在大量数据情况下,分片可能是不错的方案
redis将rdb传递个从实现主从复制,复制有多从,星状形式
当建立一个从服务器的时候,从服务器会向主服务器发送syn,主接收到syn后再后台执行bgsave,而后将保存的rdb文件发送至从服务器,从服务器收到后载入到内存,一次同步完成。