redis主从搭建


1、前提 两台服务器 已经安装好redis

2.修改作为master的配置文件 其他的不用动

protected-mode no

3.修改作为slave配置文件

# 主从 设置为no
protected-mode no

#端口要和master不一样
port 6380

# 指定master ip
slaveof 192.168.51.65 6379

# 如果master有密码 要设置这个
masterauth Xunmeizongmu_2019

# 指定日志打印路径
logfile "/home/zmoon/redis-5.0.12/log.log"

最后重启两个redis执行以下命令即可查看配置主从是否成功

登录cli

SUSE67:/home/zmoon/redis-5.0.12/bin # ./redis-cli -p 6380 -a Xunmeizongmu_2019
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> 
127.0.0.1:6380>  info replication 
# Replication
role:slave
master_host:192.168.51.65
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:1064

测试 :master设置数据 slave获取数据

SUSE65:/home/zmoon/redis-5.0.12/bin # ./redis-cli -p 6379 -a Xunmeizongmu_2019
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> SET key "value"
OK
127.0.0.1:6379> 
SUSE67:/home/zmoon/redis-5.0.12/bin # ./redis-cli -p 6380 -a Xunmeizongmu_2019
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> 
127.0.0.1:6380> 
127.0.0.1:6380> 
127.0.0.1:6380> get
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6380> get key
"value"

到这刘 redis的主从便搭好了