redis 下载地址:http://download.redis.io/releases/redis-3.0.0.tar.gz
redis安装
[size=1em]$ wget http://download.redis.io/releases/redis-3.0.0.tar.gz
[size=1em]$ tar xzf redis-3.0.0.tar.gz
[size=1em]$ cd redis-3.0.0
[size=1em]$ make
$ src/redis-server[size=1em]
redis 主从复制
以下是关于 Redis 复制功能的几个重要方面:
1. 一个Master可以有多个Slave。
2. Redis使用异步复制。从2.8开始,Slave periodically acknowledge the amount of data processed from the replication stream.
3. 不仅主服务器可以有从服务器, 从服务器也可以有自己的从服务器, 多个从服务器之间可以构成一个图状结构;
4. 复制在Master端是非阻塞模式的,这意味着即便是多个Slave执行首次同步时,Master依然可以提供查询服务;
5. 复制在Slave端也是非阻塞模式的:如果你在redis.conf做了设置,Slave在执行首次同步的时候仍可以使用旧数据集提供查询;你也可以配置为当Master与Slave失去联系时,让Slave返回客户端一个错误提示;
6. 当Slave要删掉旧的数据集,并重新加载新版数据时,Slave会阻塞连接请求(一般发生在与Master断开重连后的恢复阶段);
7. 复制功能可以单纯地用于数据冗余(data redundancy),也可以通过让多个从服务器处理只读命令请求来提升扩展性(scalability): 比如说, 繁重的 SORT 命令可以交给附属节点去运行。
8.It is possible to use replication to avoid the cost of having the master write the full dataset to disk: just configure your master redis.conf to avoid saving (just comment all the "save" directives), then connect a slave configured to save from time to time. However in this setup make sure masters don't restart automatically (please read the next section for more information).
9.In setups where Redis replication is used,it is strongly advised to have persistence turned on in the master, or when this is not possible, for example because of latency concerns, instances should be configured to avoid restarting .
10,在master上一定要开启持久化,否则很危险。 Redis复制工作原理
1. 如果设置了一个Slave,无论是第一次连接还是重连到Master,它都会发出一个SYNC命令;
2. 当Master收到SYNC命令之后,会做两件事:
a) Master starts background saving,即在后台保存数据到磁盘(rdb快照文件);
b) buffer all new commands received that will modify the dataset.(非查询类);
3. 当Master在后台把数据保存到快照文件完成之后,Master会把这个快照文件传送给Slave,而Slave则把内存清空后,加载该文件到内存中;
4. 而Master也会把此前收集到缓冲区中的命令,通过Reids命令协议形式转发给Slave,Slave执行这些命令,实现和Master的同步;
5.Slaves are able to automatically reconnect when the master slave link goes down for some reason. If the master receives multiple concurrent(并发) slave synchronization requests, it performs a single background save in order to serve all of them.
5. Master/Slave此后会不断通过异步方式进行命令的同步,达到最终数据的同步一致;
6. 需要注意的是Master和Slave之间一旦发生重连都会引发全量同步操作。但在2.8及之后版本,也可能是部分同步操作。
部分复制
2.8开始,当Master和Slave之间的连接断开之后,他们之间可以采用持续复制处理方式代替采用全量同步。
This works by creating an in-memory backlog of the replication stream on the master side.
The master and all the slaves agree on areplication offset and a master run>,
when the link goes down, the slave will reconnect and ask the master to continue the replication. Assuming the master run>, and that the offset specified is available in the replication backlog, replication will resume from the point where it left off.
If either of these conditions are unmet, a full resynchronization is performed
As the run> a full resynchronization is needed when the slave restarts.
Diskless replication
Normally a full resynchronization requires to create an RDB file on disk, then>
With slow disks this can be a very stressing operation for the master. Redis version 2.8.18 will be the first version to have experimental support for diskless replication. In this setup the child process directly sends the RDB over the wire to slaves, without using the disk as intermediate storage.
The feature is currently considered experimental. Read-only slave
Since Redis 2.6, slaves support a read-only mode that is enabled by default. This behavior is controlled by theslave-read-onlyoption in the redis.conf file, and can be enabled and disabled at runtime using CONFIG SET.
Read-only slaves will reject all write commands, there are a few legitimate use case for storing ephemeral data in writable slaves. However in the future it is possible that this feature will be dropped. Redis主从实施
ip 192.168.28.18 192.168.28.28
port 6379 6380 6379
192.168.28.18
cd redis-3.0.0/
mv redis.conf redis-master6379.conf
mv redis.conf redis-master6380.conf
master 配置192.168.28.18 6379
vi redis-master6379.conf
1、打开master持久化操作。AOF和RDB持久化比较??
appendonly yes
appendfsync everysec
2、设置redis 连接密码
requirepass April
192.168.28.18 6379 的slave 配置:192.168.28.18 6380 192.168.28.28 6379
port 6380 --一太机器上redis的端口不要冲突
REPLICATION下配置:
slaveof 192.168.28.18 6379 master的ip和port
masterauth April
run
192.168.28.18:
src/redis-server redis-master6379.conf
src/redis-server redis-slave6380.conf &
192.168.28.28
src/redis-server redis.conf
test
src/redis-cli -h 192.168.28.18 -p 6379 -a April
192.168.28.18:6379> set name biggerl
192.168.28.18:6380> get name
"biggerl"
[redis@datanode-1 redis-3.0.0]$ src/redis-cli -h 192.168.28.28 -p 6379 -a April
192.168.28.28:6379> get name
"biggerl"
默认slave都是readonly,写会返回error
192.168.28.28:6379> set file speed7
(error) READONLY You can't write against a read only slave.