开始使用Redis2.8.17
Redis第一篇在Redis2.4.5上搭建主从复制环境,对它的主从复制的工作机制,真正的惊呆了。不知道Redis2.8.17的主从复制机制是怎样的,Redis到了2.4.5这个版本,主从复制还做成那样,Impossible is nothing! 本篇把主从复制环境再搭一遍看看效果,这次在Unbuntu上用官方支持的版本。
Ubuntu上安装Redis
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make
$ make install
编译出二进制文件后存在于redis-2.8.17/src目录,不过Redis自动的在/usr/local/bin/redis-server建立了软连指向src目录下的redis-server,包括其他的可执行文件。
需要说明的是,使用redis-server -v查看Redis的版本号是2.8.9,继上篇吐槽Redis2.4.5的主从复制机制外,对于Redis犯的这种错误,小伙伴们都惊呆了。
启动Redis,有给了一个惊喜,在Ubuntu上,不带redis.conf配置文件启动没有问题,可以正常启动,带了配置文件,报各种directive问题,
Bad directive or wrong number of arguments
先放弃Unbuntu上的环境,还是继续在Windos上搭建吧。
找到解决的办法,make完了,再执行make install命令即可解决上面的问题
Redis主从复制搭建步骤
Redis搭建主从复制的步骤十分简单,以下在Windows上操作。
1. 解压Redis压缩包至E:\devsoftware\redis-2.8.17
2.在Redis目录中复制两份redis.windows.conf,分别命名为redis.windows.conf.slave1,redis.windows.conf.slave2
3.修改redis.conf.slave1文件,
- port 6379 改为 port 6380
- 打开slaveof指令,改为slaveof 127.0.0.1 6379
- 将快照文件名改为dbfilename dump.slave1.rdb
- 如果使用AOF方式持久化,则需要将appendfilename appendonly.aof改为appendfilename appendonly.slave1.aof
4. 修改redis.conf.slave2文件
- port 6379 改为 port 6381
- 打开slaveof指令,改为slaveof 127.0.0.1 6379
- 将快照文件名改为dbfilename dump.slave2.rdb
- 如果使用AOF方式持久化,则需要将appendfilename appendonly.aof改为appendfilename appendonly.slave2.aof
5. 启动三个Redis服务器,形成一主两从的主从复制副本集
通过上面的启动过程,可见只需要redis-server一个启动脚本即可,它读取不同的配置文件,来完成初始化
- Master启动时的日志输出
[21684] 25 Nov 20:54:16 * Slave ask for synchronization
[21684] 25 Nov 20:54:16 * Starting BGSAVE for SYNC
[21684] 25 Nov 20:54:16 * Foregroud saving started by pid 21684
[21684] 25 Nov 20:54:17 * DB saved on disk
[21684] 25 Nov 20:54:17 * Background saving terminated with suc
[21684] 25 Nov 20:54:17 * Synchronization with slave succeeded
- Slave1启动时的日志输出
[21068] 25 Nov 20:54:15 * Server started, Redis version 2.4.5
[21068] 25 Nov 20:54:15 * DB loaded from disk: 0 seconds
[21068] 25 Nov 20:54:15 * The server is now ready to accept connections on port
[21068] 25 Nov 20:54:16 - DB 0: 1 keys (0 volatile) in 4 slots HT.
[21068] 25 Nov 20:54:16 - 0 clients connected (0 slaves), 1180024 bytes in use
[21068] 25 Nov 20:54:16 * Connecting to MASTER...
[21068] 25 Nov 20:54:16 * MASTER <-> SLAVE sync started
[21068] 25 Nov 20:54:16 * Non blocking connect for SYNC fired the event.
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: receiving 19 bytes from master
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Loading DB in memory
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Finished with success
主服务器写入从服务器读取数据测试
- 往主服务器上写入一个数据,检查从服务器是否同步
E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set abc 1
OK
redis 127.0.0.1:6379> get abc
"1"
redis 127.0.0.1:6379>Ctrl + C
- 两个从服务器的读取操作:
E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6380
redis 127.0.0.1:6380> get abc
"1"
redis 127.0.0.1:6380> ^C
E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6381
redis 127.0.0.1:6381> get abc
"1"
redis 127.0.0.1:6381>
从服务器写入,其它服务器读取数据测试
系统提示,
(error) READONLY You can't write against a read only slave.
slave-read-only yes
Since Redis 2.6 by default slaves are read-only.
Note: read only slaves are not designed to be exposed to untrusted clients on the internet. It's just a protection layer against misuse of the instance. Still a read only slave exports by default all the administrative commands
such as CONFIG, DEBUG, and so forth. To a limited extend you can improve security of read only slaves using 'rename-command' to shadow all the administrative / dangerous commands.
主服务器挂了,是否会自动的Lead选举
内存快照文件
REDIS0002? def? abc?
save 900 1 //900秒内,有1个key变化,则做快照
save 300 10 //300秒内,有10个key变化,则做快照
save 60 10000 //60秒内,有10000个key变化,则做快照
计时时间点是从内存快照做完开始算起