使用昨天的脚本,在另外一台虚拟机上安装好了第二个REDIS服务器
脚本详见REDIS自动化安装脚本
主虚拟机上的REDIS服务
[redis_dba@MYSQL-MASTER ~]$ ps -ef | grep redis
root 3833 3411 0 19:53 pts/0 00:00:00 su - redis_dba
1101 3887 1 0 19:55 ? 00:00:01 /u01/redis-5.0.2/bin/redis-server 192.168.2.11:6379
1101 3894 3834 0 19:55 pts/0 00:00:00 redis-cli -h 192.168.2.11 -p 6379
从虚拟机上的REDIS服务:
[redis_dba@MYSQL-SLAVE1 src]$ ps -ef | grep redis
1101 3447 1 0 18:25 ? 00:00:11 u01/redis-5.0.2/bin/redis-server 192.168.2.12:6379 root 4117 3843 0 19:35 pts/0 00:00:00 su - redis_dba
1101 4393 4118 0 20:02 pts/0 00:00:00 grep redis
主服务客户端连接查看信息:
192.168.2.11:6379> info [section]
# Replication
role:master
connected_slaves:0
master_replid:c089aea8d70fb86d40afa4d7d1b49850aa80c242
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:0.024000
used_cpu_user:0.095000
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=4,expires=0,avg_ttl=0
从库服务也如此:
# Replication
role:master
connected_slaves:0
master_replid:67fa15b015c40aca8a0fd83cb08150aeb15f9e5a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:9.404000
used_cpu_user:3.291000
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
# Cluster
cluster_enabled:0
# Keyspace
192.168.2.12:6379>
两个就master_replid 不一样啦!
据说REDIS从库设置很容易,就是从库设置两个参数就OK!
哪两个参数呢? 分别是
哦 也是,我们就干
192.168.2.12:6379> shutdown nosave
not connected> exit
[redis_dba@MYSQL-SLAVE1 src]$ cd ..
[redis_dba@MYSQL-SLAVE1 redis-5.0.2]$ vim redis.conf
。。。。。。。没找到该参数 什么情况?
很多时候网络的文章都过时了,自然也包含本文,将来也过时。
不急,布吉聪明你和我自然找到了!
REPLICATION,主从模式的配置。注意,之前版本的redis,配置为slave of,现在改为REPLICATION。主从模式,可以是树状的,从服务属于多台主服务,且从服务也可以有从服务。主从模式,可实现读写分离;高可用模式下,主服务出现问题,也可以通过哨兵切换从服务为主服务;可实现主服务不用数据持久化,从服务进行持久化工作,减轻主服务负担等等。
replicaof <masterip> <masterport>:配置主服务的ip和端口。配置之后,就是这台机器的小弟了。主服务也能知道谁是他的小弟。
masterauth <master-password>:如果主服务需要密码认证,这里需要配置从服务连接主服务的密码。
replica-read-only:默认为yes,配置从服务默认为只读模式。
################################# REPLICATION #################################
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# +------------------+ +---------------+
# | Master | ---> | Replica |
# | (receive writes) | | (exact copy) |
# +------------------+ +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition replicas automatically try to reconnect to masters
# and resynchronize with them.
#
# replicaof <masterip> <masterport>
replicaof 192.168.2.11 6379
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
# When a replica loses its connection with the master, or when the replication
# is still in progress, the replica can act in two different ways:
#
# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will
# still reply to client requests, possibly with out of date data, or the
# data set may just be empty if this is the first synchronization.
#
# 2) if replica-serve-stale-data is set to 'no' the replica will reply with
# an error "SYNC with master in progress" to all the kind of commands
# but to INFO, replicaOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG,
# SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB,
# COMMAND, POST, HOST: and LATENCY.
#
保存好了 重启服务器
service redis start #从库哦
[redis_dba@MYSQL-SLAVE1 src]$ redis-cli -h 192.168.2.12 -p 6379
192.168.2.12:6379> info
# Replication
role:slave
master_host:192.168.2.11
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1346
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:5fb3e73d951afe6e3adb51821dc8764bcf4f3bfa
从库表示已经连上了主库 呵呵上面的信息简单易懂,接下来我们同步玩玩
主库:
192.168.2.11:6379> set key "Happy New Year for you! to 2019 Pig Year "
OK
192.168.2.11:6379>
从库
192.168.2.12:6379> get key
"Happy New Year for you! to 2019 Pig Year "

主从复制如何交互?
下面来研究下slave服务器和master服务器间是如何建立起主从同步机制的。
1 Slave服务启动,主动连接Master,并发送SYNC命令,请求初始化同步
2 Master收到SYNC后,执行BGSAVE命令生成RDB文件,并缓存该时间段内的写命令
3 Master完成RDB文件后,将其发送给所有Slave服务器,
4 Slave服务器接收到RDB文件后,删除内存中旧的缓存数据,并装载RDB文件
5 Master在发送完RDB后,即刻向所有Slave服务器发送缓存中的写命令
6 至此初始化完成,后续进行增量同步
搞定了主从,那么就搞高之间的切换侦探。 这方面技术有KEEP ALIVE和REDIS 哨兵。 下回再分享!




