- 主从配置(一主而从)
(1)如果服务是启动的,先停掉redis服务
(2)进入root目录创建文件夹master-slave
mkdir master-slave
(3) 进入到master-slave目录下
cd master-slave
(4) 创建三个配置文件
touch redis638{0,2,4}.conf
[root@leitingyan master-slave]# ls
redis6380.conf redis6382.conf redis6384.conf
(5)编辑Master的配置文件,redis6380.conf:在文件中加入以下内容
vim redis6380.conf
--------------------------------------------
include /usr/local/redis-3.2.9/redis.conf
daemonize yes
port 6380
pidfile /var/run/redis_6380.pid
logfile 6380.log
dbfilename dump6380.rdb
(6)编辑Master的配置文件,redis6382.conf:在文件中加入以下内容
vim redis6382.conf
--------------------------------------------
include /usr/local/redis-3.2.9/redis.conf
daemonize yes
port 6382
pidfile /var/run/redis_6382.pid
logfile 6382.log
dbfilename dump6382.rdb
slaveof 127.0.0.1 6380
(7)编辑Master的配置文件,redis6384.conf:在文件中加入以下内容
vim redis6384.conf
--------------------------------------------
include /usr/local/redis-3.2.9/redis.conf
daemonize yes
port 6384
pidfile /var/run/redis_6384.pid
logfile 6384.log
dbfilename dump6384.rdb
slaveof 127.0.0.1 6380
(8)启动:先启动
[root@leitingyan master-slave]# redis-server redis6380.conf
[root@leitingyan master-slave]# redis-server redis6382.conf
[root@leitingyan master-slave]# redis-server redis6384.conf
(9)查看
- 启动主的客户端并查看信息
[root@leitingyan master-slave]# redis-cli -p 6380
-----------------------------------------------------
127.0.0.1:6380> info replication
# Replication
##说明角色是主的
role:master
##和他连接的从的个数是两个
connected_slaves:2
##两个从的基本信息,包括IP地址,端口号连接状态
slave0:ip=127.0.0.1,port=6382,state=online,offset=183,lag=0
slave1:ip=127.0.0.1,port=6384,state=online,offset=183,lag=1
master_repl_offset:183
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:182
- 启动从的客户端并查看信息
[root@leitingyan ~]# redis-cli -p 6384
--------------------------------------------
127.0.0.1:6384> info replication
# Replication
##说明他是从的,
role:slave
##他对应的主的的IP地址
master_host:127.0.0.1
#端口号:(从的)
master_port:6380
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:505
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
127.0.0.1:6384>
(10)测试
- 主的可以写,也是读
127.0.0.1:6380> set k1 v1
OK
---------------------------------------------------------------
127.0.0.1:6380> get k1
"v1"
- 从的只能读,不能写,写的时候会报错
127.0.0.1:6384> get k1
"v1"
---------------------------------------------------------------
127.0.0.1:6384> set k2 v2
(error) READONLY You can't write against a read only slave.