Redis 主从配置

主从配置,也就是在主库上写数据,从库也会跟着实时同步,这里我在一台机器上起两个 redis 服务来做主从

[root@localhost ~]# cp /etc/redis.conf /etc/redis2.conf    // 拷贝主库的配置文件,/etc/redis2.conf 作为从库的配置文件
[root@localhost ~]# cat /etc/redis2.conf    // 修改从库配置文件
port 6380                                   // 端口不能与主库一致
pidfile /var/run/redis_6380.pid             // pid文件不能与主库一致
dir /data/redis2                            // 数据存放目录不能与主库一致
logfile "/var/log/redis2.log"               // 日志文件不要与主库一致
slaveof 127.0.0.1 6379                      // 指定主库的IP和端口
masterauth 123456                           // 可选,如果主库设置了密码,这里要添加认证
slave-read-only yes // 可选,设置从库为只读,默认是可以写入到从库的
[root@localhost ~]# mkdir -p /data/redis2           // 创建从库的数据存放目录
[root@localhost ~]# redis-server /etc/redis2.conf  // 启动从库,就算搭建完主从了
[root@localhost ~]# ps aux | grep redis
root      30703  0.2  0.6 147472  9932 ?        Ssl  08:56   0:16 redis-server 127.0.0.1:6379
root      31585  0.6  0.6 147352  9692 ?        Ssl  10:49   0:00 redis-server 127.0.0.1:6380
root      31593  0.0  0.0 112728   976 pts/1    S+   10:49   0:00 grep --color=auto redis
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6380    // 登录到从库,查看是否有主库的数据
127.0.0.1:6380> keys *

    

原文地址:https://www.cnblogs.com/pzk7788/p/10464298.html