设为首页 收藏本站
查看: 1015|回复: 0

[经验分享] Redis单机配置多实例,实现主从同步

[复制链接]

尚未签到

发表于 2017-12-21 11:37:12 | 显示全部楼层 |阅读模式
  版权声明:本文为博主原创文章,未经博主允许不得转载
  本文转自:http://www.cnblogs.com/lgeng/p/6623336.html
  一,单机多实例:
  Redis官网: https://redis.io/
  1,安装:
  

yum -y install gcc gcc-c++    #安装编译工具  

  
[iyunv@localhost data]#
wget http://download.redis.io/releases/redis-3.2.8.tar.gz  
[iyunv@localhost data]# tar xzf redis-3.2.8.tar.gz
  
[iyunv@localhost data]# cd redis-3.2.8
  
[iyunv@localhost data]# make
  
[iyunv@localhost data]# mv redis-3.2.8  /usr/local/redis
  

  
注意:可直接 yum install redis -y 安装
  

  

  启动:(默认启动6379端口)
[iyunv@localhost redis]# /usr/local/redis/src/redis-server

  

DSC0000.png

  

  

  

  

  

  

  2,验证:使用 redis-cli命令验证 (注意路径)
  

  

[iyunv@localhost src]# /usr/local/redis/src/redis-cli -p 6379  
127.0.0.1:6379> keys *
  
(empty list or set)
  
127.0.0.1:6379> set k1 v1
  
OK
  
127.0.0.1:6379> keys *
  
1) "k1"
  
127.0.0.1:6379> get k1
  
"v1"
  
127.0.0.1:6379>
  
127.0.0.1:6379>
  

DSC0001.png

  3,配置环境变量
  输入redis-server和redis-cli命令,每次输入完整的路径
  将路径添加到PATH变量中
  

echo 'PATH=${PATH}:/usr/local/redis/src/' >> /etc/profile  
source  
/etc/profile       #<== 重新加载配置文件  

  二,单机多实例
  创建不同实例的数据存放目录 分别创建6380,6381,6382 三个实例
  每个实例目录中分别创建 conf,db,log目录,并拷贝配置文件到conf中
  

[iyunv@localhost /]# mkdir -p  /data/redis/{6380,6381,6382}/{conf,db,log}  
[iyunv@localhost
/]# cp /usr/local/redis/redis.conf /data/redis/6380/conf/  
[iyunv@localhost
/]# cp /usr/local/redis/redis.conf /data/redis/6381/conf/  
[iyunv@localhost
/]# cp /usr/local/redis/redis.conf /data/redis/6382/conf/  
[iyunv@localhost
/]#  
[iyunv@localhost
/]# cd /data/redis  
[iyunv@localhost redis]#
ls  
6380  6381  6382
  
[iyunv@localhost redis]# tree
  
.
  
├── 6380
  
│   ├── conf
  
│   │   └── redis.conf
  
│   ├── db
  
│   └── log
  
├── 6381
  
│   ├── conf
  
│   │   └── redis.conf
  
│   ├── db
  
│   └── log
  
└── 6382
  
├── conf
  
│   └── redis.conf
  
├── db
  
└── log
  

  
12 directories, 3 files
  
[iyunv@localhost redis]#
  
[iyunv@localhost redis]#
  

  修改配置文件:
  将redis.conf修改为对应的实例参数,修改部分如下
  

[iyunv@localhost redis]# grep "6380\|daemonize" 6380/conf/redis.conf  
daemonize yes                    
<== daemon进程运行  
pidfile
/data/redis/6380/redis.pid           <== 进程id存放文件  
port
6380                                    <== 端口  
logfile
/data/redis/6380/log/redis.log       <== 日志目录  

dir /data/redis/6380/db/                     <== db目录  
[iyunv@localhost redis]#
  
[iyunv@localhost redis]#
grep "6381" 6381/conf/redis.conf  
daemonize yes
  
pidfile
/data/redis/6381/redis.pid  
port
6381  
logfile
/data/redis/6381/log/redis.log  

dir /data/redis/6381/db/  
[iyunv@localhost redis]#
  
[iyunv@localhost redis]#
grep "6382" 6382/conf/redis.conf  
daemonize yes
  
pidfile
/data/redis/6382/redis.pid  
port
6382  
logfile
/data/redis/6382/log/redis.log  

dir /data/redis/6382/db/  
[iyunv@localhost redis]#
  
[iyunv@localhost redis]#
  

  启动实例:
  

[iyunv@localhost redis]# redis-server /data/redis/6380/conf/redis.conf  
[iyunv@localhost redis]# redis
-server /data/redis/6381/conf/redis.conf  
[iyunv@localhost redis]# redis
-server /data/redis/6382/conf/redis.conf  
[iyunv@localhost redis]#
  
[iyunv@localhost redis]# netstat
-ntlp | grep -E ":6380|:6381|:6382"  
tcp        
0      0 127.0.0.1:6380              0.0.0.0:*                   LISTEN      14301/redis-server  
tcp        
0      0 127.0.0.1:6381              0.0.0.0:*                   LISTEN      14305/redis-server  
tcp        
0      0 127.0.0.1:6382              0.0.0.0:*                   LISTEN      14309/redis-server  
[iyunv@localhost redis]#
  
[iyunv@localhost redis]#
  

  验证(略) redis-cli -p 6380 ; redi-cli -p 6381 ; redis-cli -p 6382
  三,配置主从同步
  修改从库配置,6380实例为主库,  从库为 6381,6382
  

[iyunv@localhost ~]# vim /data/redis/6381/conf/redis.conf  
[iyunv@localhost
~]# vim /data/redis/6382/conf/redis.conf  

  
################################# 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>
#    time. You may want to configure the replication backlog>
#    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
127.0.0.1 6380  

  
# 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>  

  
# When a slave loses its connection with the master, or when the replication
  
# is still
in progress, the slave can act in two different ways:  
#
  
#
1) if slave-serve-stale-data is set to 'yes' (the default) the slave 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 slave-serve-stale-data is set to 'no' the slave will reply with  
#    an error
"SYNC with master in progress" to all the kind of commands  

  验证:
  先在主库上info一下
  

[iyunv@localhost 6380]# redis-cli -p 6380 "info"  
.
  
.此处略去n行
  
.
  
# Replication
  
role:master                                    
<== 角色:master  
connected_slaves:
2                      <== slave链接数 2  
slave0:ip
=127.0.0.1,port=6381,state=online,offset=141,lag=1     <== slave 的信息  
slave1:ip
=127.0.0.1,port=6382,state=online,offset=141,lag=1  
master_repl_offset:
141  
repl_backlog_active:
1  
repl_backlog_size:
1048576  
repl_backlog_first_byte_offset:
2  
repl_backlog_histlen:
140  

  

  看看从库
  

[iyunv@localhost 6381]# redis-cli -p 6381 "info"  
...
  
.此处略去n行
  
....
  
# Replication
  
role:slave          
<==角色 slave  
master_host:
127.0.0.1     <==master主机  
master_port:
6380       <== master端口  
master_link_status:up      
<== 链接状态 up  
master_last_io_seconds_ago:
5  
master_sync_in_progress:
0  
slave_repl_offset:
673  
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  

  

[iyunv@localhost 6382]# redis-cli -p 6382 "info"  
...
  
.此处略去n行
  
....
  
# Replication
  
role:slave
  
master_host:
127.0.0.1  
master_port:
6380  
master_link_status:up
  
master_last_io_seconds_ago:
4  
master_sync_in_progress:
0  
slave_repl_offset:
911  
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  

  

  在主库上写入数据:
  

  

[iyunv@localhost 6380]# redis-cli -p 6380  
127.0.0.1:6380> set k1 v1
  
OK
  
127.0.0.1:6380> keys *
  
1) "k1"
  
127.0.0.1:6380> get k1
  
"v1"
  
127.0.0.1:6380>
  

  

  从库上查看是否已同步
  

  

[iyunv@localhost 6381]# redis-cli -p 6381  
127.0.0.1:6381> keys *
  
1) "k1"
  
127.0.0.1:6381> get k1
  
"v1"
  
127.0.0.1:6381>
  
127.0.0.1:6381> exit
  
[iyunv@localhost 6381]# redis-cli -p 6382
  
127.0.0.1:6382> keys *
  
1) "k1"
  
127.0.0.1:6382> get k1
  
"v1"
  
127.0.0.1:6382>
  

  

  主库删除数据:
  

[iyunv@localhost 6380]# redis-cli -p 6380  
127.0.0.1:6380> keys *
  
1) "k1"
  
127.0.0.1:6380> del k1
  
(integer) 1
  
127.0.0.1:6380> keys *
  
(empty list or set)
  
127.0.0.1:6380>
  

  从库查看
  

[iyunv@localhost 6381]# redis-cli -p 6381  
127.0.0.1:6381> keys *
  
(empty list or set)
  
127.0.0.1:6381> exit
  
[iyunv@localhost 6381]# redis-cli -p 6382
  
127.0.0.1:6382> keys *
  
(empty list or set)
  
127.0.0.1:6382>
  
127.0.0.1:6382> exit
  
[iyunv@localhost 6381]#
  

  END
  本文转自:http://www.cnblogs.com/lgeng/p/6623336.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426397-1-1.html 上篇帖子: [转] Redis 存储List对象 下篇帖子: Java 连接操作 Redis 出现错误
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表