Redis单机配置多实例,实现主从同步

版权声明:本文为博主原创文章,欢迎转载,转载请保留或注明出处
本文转自:http://www.cnblogs.com/lgeng/p/6623336.html

一,单机多实例:
Redis官网: https://redis.io/
1,安装:

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

[root@localhost data]# wget http://download.redis.io/releases/redis-3.2.8.tar.gz
[root@localhost data]# tar xzf redis-3.2..tar.gz
[root@localhost data]# cd redis-3.2.
[root@localhost data]# make
[root@localhost data]# mv redis-3.2. /usr/local/redis 注意:可直接 yum install redis -y 安装

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

Redis单机配置多实例,实现主从同步
 
 

2,验证:使用 redis-cli命令验证 (注意路径)
[root@localhost src]# /usr/local/redis/src/redis-cli -p
127.0.0.1:> keys *
(empty list or set)
127.0.0.1:> set k1 v1
OK
127.0.0.1:> keys *
) "k1"
127.0.0.1:> get k1
"v1"
127.0.0.1:>
127.0.0.1:>

Redis单机配置多实例,实现主从同步

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中

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

修改配置文件:

将redis.conf修改为对应的实例参数,修改部分如下



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

启动实例:

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

验证(略) redis-cli -p 6380 ; redi-cli -p 6381 ; redis-cli -p 6382

三,配置主从同步
修改从库配置,6380实例为主库,  从库为 6381,6382


[root@localhost ~]# vim /data/redis//conf/redis.conf
[root@localhost ~]# vim /data/redis//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.
#
# ) 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.
# ) 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.
# ) 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 # 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:
#
# ) 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.
#
# ) 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一下

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

看看从库

[root@localhost ]# redis-cli -p  "info"
...
.此处略去n行
....
# Replication
role:slave         <==角色 slave
master_host:127.0.0.1   <==master主机
master_port:      <== master端口
master_link_status:up <== 链接状态 up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
[root@localhost ]# redis-cli -p  "info"
...
.此处略去n行
....
# Replication
role:slave
master_host:127.0.0.1
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:


在主库上写入数据:

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

从库上查看是否已同步
[root@localhost ]# redis-cli -p
127.0.0.1:> keys *
) "k1"
127.0.0.1:> get k1
"v1"
127.0.0.1:>
127.0.0.1:> exit
[root@localhost ]# redis-cli -p
127.0.0.1:> keys *
) "k1"
127.0.0.1:> get k1
"v1"
127.0.0.1:>

主库删除数据:

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

从库查看

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

END

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


上一篇:JMeter中返回Json数据的处理方法(转)


下一篇:SpringMVC返回JSON数据时日期格式化问题