手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

CentOS 6.5 64位下Redis主从复制配置

[日期:2017-02-20] 来源:Linux社区  作者:jalja [字体: ]

该文使用CentOS 6.5 64位  Redis3.2.8   

主从复制

Redis的复制功能是支持多个数据库之间的数据同步。一类是主数据库(master)一类是从数据库(slave),主数据库可以进行读写操作,当发生写操作的时候自动将数据同步到从数据库,而从数据库一般是只读的,并接收主数据库同步过来的数据,一个主数据库可以有多个从数据库,而一个从数据库只能有一个主数据库。通过redis的复制功能可以很好的实现数据库的读写分离,提高服务器的负载能力。主数据库主要进行写操作,而从数据库负责读操作。

Redis主从复制:主从复制可以允许多个slave server 拥有和master server相同的数据库副本

1、Redis主从复制的特点:

a、  master 可以有多个slave

b、  多个slave 可以链接同一个master外,还可以链接其他slave

c、  主从复制不会阻塞master,在数据同步的时候,master可以继续处理client请求

d、  提高系统的伸缩性

2、Redis主从复制的过程:

a、  slave与master建立链接,发送sync同步请求。

b、  master会启动一个后台进程,将数据库快照保存到文件中,同时master主进程会开始收集新的写命令并缓存。

c、  后台完成保存后,就将此文件发送给slave

d、  Slave将此文件保存到硬盘上。

3、Redis 主从复制操作步骤

环境:

Redis主从结构支持一主多从(所有从节点的配置都一样)

master:192.168.6.190

slave:192.168.6.191

配置:

配置slave服务器,在slave服务器的配置文件中加入一下代码

slaveof 192.168.222.1 6379 #指定master的ip和端口

Masterauth linuxidc #主机密码

################################# REPLICATION #################################

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 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 slaves.
# 2) Redis slaves 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 slaves automatically try to reconnect to masters
#    and resynchronize with them.
#
# slaveof <masterip> <masterport>
slaveof 192.168.6.190 6379
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>

启动master服务器:

[root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf

查看master配置信息:127.0.0.1:6379> info

# Replication
role:master
connected_slaves:1
slave0:ip=192.168.6.191,port=6379,state=online,offset=141,lag=0
master_repl_offset:141
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:140

启动slave服务器:

[root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf

查看slave配置信息:127.0.0.1:6379> info

# Replication
role:slave
master_host:192.168.6.190
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:99
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

测试:

master:

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name linuxidc
OK
127.0.0.1:6379>

slave:

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name linuxidc
OK
127.0.0.1:6379>

配置时遇到错误:master_link_status:down

1、确定master与slave的redis端口是开放的,未被防火墙拦截

2、修改 master redis.cnf 文件中bind 为bind 0.0.0.0

下面关于Redis的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 14.04安装Redis与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10下Redis集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis实战 中文PDF http://www.linuxidc.com/Linux/2016-04/129932.htm

CentOS 7下Redis的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm

Redis 的详细介绍请点这里
Redis 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-02/140822.htm

linux
相关资讯       Redis主从复制 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款