阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

126次阅读
没有评论

共计 3684 个字符,预计需要花费 10 分钟才能阅读完成。

该文使用 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

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计3684字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中