1、数据库分类
2、特点
3、工作机制
当slave启动后,主动向master发送SYNC命令。master接收到SYNC命令后在后台保存快照(RDB持久化)和缓存保存快照这段时间的命令,然后将保存的快照文件和缓存的命令发送给slave。slave接收到快照文件和命令后加载快照文件和缓存的执行命令。
复制初始化后,master每次接收到的写命令都会同步发送给slave,保证主从数据一致性。
4、安全设置:
当master节点设置密码后,客户端访问master需要密码,启动slave需要密码,在配置文件中配置即可,客户端访问slave不需要密码
5、缺点:
master节点在主从模式中唯一,若master挂掉,则redis无法对外提供写服务
6、实例
//1、创建文件夹 [user@localhost ~]$ mkdir -p /app/redis/master-slaver [user@localhost master-slave]$ mkdir bin 7001 7002 7003 //2、拷贝修改配置文件 [user@localhost master-slave]$ cd ../src [user@localhost src]$ cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server ../master-slave/bin [user@localhost master-slave]$ cp /app/redis/redis.conf 7001/7001.conf [user@localhost master-slave]$ vim 7001/7001.conf bind 10.20.30.40 #监听ip,多个ip用空格分隔 daemonize yes #允许后台启动 logfile "/app/redis/master-slave/7001/redis.log" #日志路径 dir /data/redis #数据库备份文件存放目录 masterauth 123456 #slave连接master密码,master可省略 requirepass 123456 #设置master连接密码,slave可省略 # replicaof 10.20.30.40 7001 # 在slave的配置文件中,配置为192.168.30.128 6379的从节点 # cluster-enabled no # 在slave的配置文件中,配置为no appendonly yes #在/data/redis/目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中 //3、启动服务 [user@localhost master-slave]$ cd bin [user@localhost bin]$ ./redis-server ../7001/7001.conf [user@localhost bin]$ ./redis-server ../7002/7002.conf [user@localhost bin]$ ./redis-server ../7003/7003.conf //开三个shell界面 //[user@localhost bin]$ ./redis-cli -h 10.200.202.41 -p 7002 -a 123456 //[user@localhost bin]$ ./redis-cli -h 10.200.202.41 -p 7003 -a 123456 [user@localhost bin]$ ./redis-cli -h 10.20.30.40 -p 7001 -a 123456 10.20.30.40:7001> info replication # Replication role:master connected_slaves:2 slave0:ip=10.200.202.41,port=7002,state=online,offset=686,lag=1 slave1:ip=10.200.202.41,port=7003,state=online,offset=686,lag=0 master_failover_state:no-failover master_replid:7bb378122709dace3458a8fdbbcd201b96e96d20 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:686 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:686 //4、数据演示 10.200.202.41:7001> set name hello (error) CLUSTERDOWN Hash slot not served [user@localhost bin]$ ./redis-cli --cluster check 10.20.30.40:7001 [ERR] Not all 16384 slots are covered by nodes [user@localhost bin]$ ./redis-cli --cluster fix 10.20.30.40:7001 Can I set the above configuration? (type 'yes' to accept): yes //一定要输入yes,不是y 10.200.202.41:7001> set name hello OK //在master节点写入的数据,很快就同步到slave节点上,而且在slave节点上无法写入数据 10.200.202.41:7002> get name "hello" 10.200.202.41:7002> set six man (error) READONLY You can't write against a read only replica.