127.0.0.1:6379> info replication #查看当前库的基本信息 # Replication role:master #当前角色master connected_slaves:0 #没有从机 master_failover_state:no-failover master_replid:da7835383800aab344482c4efdd805f485fc28eb master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
[root@pakho bin]# pwd /usr/local/bin
#位于redis配置文件 [root@pakho redis_config]# pwd /usr/local/bin/redis_config #根据端口copy配置文件,根据端口修改配置文件 [root@pakho redis_config]# cp redis.conf redis79.conf [root@pakho redis_config]# cp redis.conf redis80.conf [root@pakho redis_config]# cp redis.conf redis81.conf #配置配置文件 [root@pakho redis_config]# vim redis79.conf logfile "6379.log" dbfilename dump6379.rdb [root@pakho redis_config]# vim redis80.conf port 6380 pidfile /var/run/redis_6380.pid logfile "6380.log" dbfilename dump6380.rdb [root@pakho redis_config]# vim redis81.conf port 6381 pidfile /var/run/redis_6381.pid logfile "6381.log" dbfilename dump6381.rdb
[root@pakho bin]# redis-server redis_config/redis79.conf #启动79 redis服务 [root@pakho bin]# ls 6379.log #此时会生成一个6379.log表明以及启动
[root@pakho bin]# redis-server redis_config/redis80.conf #启动80 redis服务
[root@pakho bin]# redis-server redis_config/redis81.conf #启动81 redis服务
启动成功
[root@pakho bin]# ps aux | grep -v grep | grep redis root 6674 0.1 0.9 162396 9916 ? Ssl 20:30 0:00 redis-server 127.0.0.1:6379 root 6683 0.1 0.9 162396 9924 ? Ssl 20:33 0:00 redis-server 127.0.0.1:6380 root 6689 0.0 0.9 162396 9912 ? Ssl 20:34 0:00 redis-server 127.0.0.1:6381
6379 [root@pakho bin]# redis-cli -p 6379 127.0.0.1:6379> ping PONG 127.0.0.1:6379> info replication # Replication role:master 6380 [root@pakho bin]# redis-cli -p 6380 127.0.0.1:6380> info replication # Replication role:master 6381 [root@pakho bin]# redis-cli -p 6381 127.0.0.1:6381> ping PONG 127.0.0.1:6381> info replication # Replication role:master
slaveof host 6379
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6380> info replication # Replication role:slave #当前角色从机 master_host:127.0.0.1 #可以看到主机角色 master_port:6379 master_link_status:up master_last_io_seconds_ago:6 master_sync_in_progress:0 slave_repl_offset:0 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:38aa9e9204f21b598c2c162f176a599497082d95 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:0
#79 master检查 127.0.0.1:6379> info replication # Replication role:master #当前角色主机 connected_slaves:1 #连接一个从机 slave0:ip=127.0.0.1,port=6380,state=online,offset=224,lag=0 #连接一个从机,目前是在线的 master_failover_state:no-failover master_replid:38aa9e9204f21b598c2c162f176a599497082d95 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:224 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:224
127.0.0.1:6381> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:10 master_sync_in_progress:0 slave_repl_offset:700 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:38aa9e9204f21b598c2c162f176a599497082d95 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:700 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:659 repl_backlog_histlen:42
127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=742,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=742,lag=0 master_failover_state:no-failover master_replid:38aa9e9204f21b598c2c162f176a599497082d95 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:742 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:742
关于配置文件配置主从永久生效
################################# REPLICATION ################################# # replicaof <masterip> <masterport> #设置对应主机地址,主机端口 # masterauth <master-password> #如果主机有密码,设置密码
127.0.0.1:6379> set k1 v1 #主机设置值 OK 127.0.0.1:6380> keys * #从机查看 1) "k1" 127.0.0.1:6380> get k1 "v1" 127.0.0.1:6381> keys * 1) "k1" 127.0.0.1:6381> get k1 "v1"
试验从机能否写入?
只读主从复制
127.0.0.1:6381> set k2 v2 (error) READONLY You can't write against a read only replica.
127.0.0.1:6379> shutdown not connected> exit
127.0.0.1:6380> info replication # Replication role:slave #从机不会自动切换 master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:1886 master_link_down_since_seconds:108 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:38aa9e9204f21b598c2c162f176a599497082d95 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1886 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1886
[root@pakho bin]# redis-server redis_config/redis79.conf [root@pakho bin]# redis-cli -p 6379 127.0.0.1:6379> ping PONG 127.0.0.1:6380> get k2 "v2" 127.0.0.1:6381> get k2 "v2"
127.0.0.1:6381> shutdown not connected> exit 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=458,lag=1 master_failover_state:no-failover master_replid:5e4a8baf094e06a17e005d33b09d52c190cea5f7 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:472 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:472
127.0.0.1:6379> set k3 v3 OK #重新连接从机 [root@pakho bin]# redis-server redis_config/redis81.conf [root@pakho bin]# redis-cli -p 6381 127.0.0.1:6381> ping PONG 127.0.0.1:6381> get k3 (nil) #主机重新配置一个值 127.0.0.1:6379> set k4 v4 OK #从机依旧无法查看 127.0.0.1:6381> get k4 (nil)
127.0.0.1:6381> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6381> info replication # Replication role:slave 127.0.0.1:6381> get k4 #此时又会重新拿到数据 "v4"
#增量复制 127.0.0.1:6379> set k5 v5 OK 127.0.0.1:6381> get k5 "v5"
127.0.0.1:6381> SLAVEOF 127.0.0.1 6380 OK 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:1758 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:5e4a8baf094e06a17e005d33b09d52c190cea5f7 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1758 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:909 repl_backlog_histlen:850
127.0.0.1:6379> set k6 v6 OK 127.0.0.1:6380> get k6 "v6" 127.0.0.1:6381> get k6 "v6"
127.0.0.1:6379> shutdown not connected> exit
slaveof no one
让自己变成主机!其他主机可以手动连接到最新的主节点(手动);127.0.0.1:6381> SLAVEOF no one OK 127.0.0.1:6381> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:e171928680f81750502ae220981e2aa417dc118e master_replid2:5e4a8baf094e06a17e005d33b09d52c190cea5f7 master_repl_offset:2557 second_repl_offset:2558 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:909 repl_backlog_histlen:1649
#恢复原状 127.0.0.1:6381> SLAVEOF 127.0.0.1 6379 OK