1.配置文件
requirepass 123456
2.使用密码登录
两种方式:
第一种:
redis-cli -h db01
AUTH 123456
第二种:
redis-cli -h db01 -a 123456 get k_1
9.主从复制
方法1:临时生效
SLAVEOF 10.0.0.51 6379
方法2:写入配置文件
SLAVEOF 10.0.0.51 6379
取消同步命令:
SLAVEOF no one
主从复制流程:
从库发起同步请求
11:55:12.686 * Connecting to MASTER 10.0.0.51:6379
11:55:12.687 * MASTER <-> SLAVE sync started
11:55:12.687 * Non blocking connect for SYNC fired the event.
11:55:12.687 * Master replied to PING, replication can continue...
11:55:12.688 * Partial resynchronization not possible (no cached master)
11:55:12.690 * Full resync from master: c63b80909e49adfa3880dcc87ccffb89d148a564:436
主库接收请求
11:55:12.694 * Slave 10.0.0.52:6379 asks for synchronization
11:55:12.694 * Full resync requested by slave 10.0.0.52:6379
主库开始BGSAVE
11:55:12.694 * Starting BGSAVE for SYNC with target: disk
11:55:12.695 * Background saving started by pid 13009
11:55:12.700 * DB saved on disk
11:55:12.700 * RDB: 6 MB of memory used by copy-on-write
从库接收主库的数据,清空自己的数据,然后将主库发送过来的RDB文件加载到内存中
11:55:12.780 * MASTER <-> SLAVE sync: receiving 12887 bytes from master
11:55:12.781 * MASTER <-> SLAVE sync: Flushing old data
11:55:12.781 * MASTER <-> SLAVE sync: Loading DB in memory
11:55:12.782 * MASTER <-> SLAVE sync: Finished with success
主库接收从库消息,主从复制成功
11:55:12.786 * Background saving terminated with success
11:55:12.786 * Synchronization with slave 10.0.0.52:6379 succeeded
10.Redis主从复制部署
快速创建第二台redis节点命令:
rsync -avz /opt/* db02:/opt/
rsync -avz /data db02:/
cd /opt/redis
make install
sed -i 's#51#52#g' /opt/redis_6379/conf/redis_6379.conf
rm -rf /data/redis_6379/*
redis-server /opt/redis_6379/conf/redis_6379.conf
配置方法:
方法1: 临时生效
[root@db-02 ~]# redis-cli -h 10.0.1.52
10.0.0.52:6379> SLAVEOF 10.0.1.51 6379
OK
方法2: 写入配置文件
SLAVEOF 10.0.1.51 6379
主从复制流程:
1.从节点发送同步请求到主节点
2.主节点接收到从节点的请求之后,做了如下操作
立即执行bgsave将当前内存里的数据持久化到磁盘上
持久化完成之后,将rdb文件发送给从节点
3.从节点从主节点接收到rdb文件之后,做了如下操作
清空自己的数据
载入从主节点接收的rdb文件到自己的内存里
4.后面的操作就是和主节点实时的了
取消主从复制
SLAVEOF no one
注意!!!
1.从节点只读不可写
2.从节点不会自动故障转移,它会一直同步主
10.0.1.52:6379> set k1 v1
(error) READONLY You can't write against a read only slave.
3.主从复制故障转移需要人工介入
修改代码指向REDIS的IP地址
从节点需要执行SLAVEOF no one
注意!!!
1.从节点会清空自己原有的数据,如果同步的对象写错了,就会导致数据丢失