1.什么是主从复制
主服务器写数据会实时同步(复制)到从服务器
2.主从复制搭建步骤
搭建主从复制架构
1.主不需要配置
2.从机:
1.cd /usr/local/redis.6.0.6 cp -r bin bin2
2.修改从机的redis.conf,保存:
1.replicaof 127.0.0.1 6379
2.port 6380
3.清除从机中的持久化文件: rm -rf dump.rdb appendonly.aof
3.启动从机:
1.cd bin2
2 ./redis-server ./redis.conf
4.这时从机重新生成了appendonly.aof dump.rdb文件
5.在主机设置值
1./redis-cli;:set a 1
2.get a
6.在从机取值
1./redis-cli -p 6380: get a 取值成功
7.在从机不能设置,只能读
1./redis-cli -p 6380:
127.0.0.1:6380> set a 3
(error) READONLY You can't write against a read only replica.
8.搭建第二个从机
1.cd /usr/local/redis.6.0.6/
2.cp -r bin2 bin3
3.修改从机bin3的redis.conf,保存:
1.port 6381
2.清除从机中的持久化文件: rm -rf dump.rdb appendonly.aof
4.启动从机: 1.cd bin3 2 ./redis-server ./redis.conf
5.这时从机重新生成了appendonly.aof dump.rdb文件
6.在主机设置值 1./redis-cli;:set a 2 2.get a
7.在从机取值 1./redis-cli -p 6381: get a 取值成功
8.在从机不能设置,只能读
1./redis-cli -p 6381:
127.0.0.1:6380> set a 3
(error) READONLY You can't write against a read only