redis单机安装
安装并配置redis服务
[root@localhost ~]# ll
-rw-r--r-- 1 root root 1966337 10月 10 10:09 redis-5.0.4.tar.gz
[root@localhost ~]# tar xf redis-5.0.4.tar.gz
[root@localhost ~]# cd redis-5.0.4/
[root@localhost redis-5.0.4]# make
[root@localhost redis-5.0.4]# cd
[root@localhost ~]# mkdir /data/redis
[root@localhost ~]# cp -r redis-5.0.4/* /data/redis
#redis-server #服务端程序
#redis-cli #客户端程序
#redis.conf #主配置文件
#修改本机IP
[root@localhost ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/192.168.100.104/g' /data/redis/redis.conf
#开启redis的保护模式
[root@localhost ~]# sed -i '/protected-mode/s/yes/no/g' /data/redis/redis.conf
#开启redis后台守护
[root@localhost ~]# sed -i '/daemonize/s/no/yes/g' /data/redis/redis.conf
#设置redis密码
[root@localhost ~]# sed -i '/requirepass/s/foobared/123123/g' /data/redis/redis.conf
#开启redis密码
[root@localhost ~]# sed -i '/requirepass 123123/s/^#//g' /data/redis/redis.conf
开启redis
[root@localhost ~]# /data/redis/src/redis-server /data/redis/redis.conf
30713:C 10 Oct 2022 10:31:15.925 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30713:C 10 Oct 2022 10:31:15.925 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=30713, just started
30713:C 10 Oct 2022 10:31:15.925 # Configuration loaded
[root@localhost ~]# netstat -utpln|grep 6379
tcp 0 0 192.168.100.104:6379 0.0.0.0:* LISTEN 30714/redis-server
[root@localhost ~]# /data/redis/src/redis-cli -h 192.168.100.104 -p 6379 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.104:6379> keys *
(empty list or set)
192.168.100.104:6379> set name haha
OK
192.168.100.104:6379> keys *
1) "name"
192.168.100.104:6379> get name
"haha"
redis主从复制
准备环境
系统 | IP | 主机名 | 所需软件 | redis角色及端口 |
---|---|---|---|---|
centos7 | 192.168.100.104 | master | redis-5.0.4.tar.gz | master、6379 |
centos7 | 192.168.100.105 | slave | redis-5.0.4.tar.gz | slave、6379、6380 |
master安装配置
[root@master ~]# mkdir /data/redis
[root@master ~]# tar xf redis-5.0.4.tar.gz
[root@master ~]# cd redis-5.0.4/
[root@master redis-5.0.4]# make
[root@master ~]# cp redis/src/redis-server /data/redis/
[root@master ~]# cp redis/src/redis-cli /data/redis/
[root@master ~]# cp redis/redis.conf /data/redis/
[root@master ~]# ll /data/redis/
总用量 12672
-rwxr-xr-x 1 root root 4806256 10月 10 16:16 redis-cli
-rw-r--r-- 1 root root 62155 10月 10 16:16 redis.conf
-rwxr-xr-x 1 root root 8101856 10月 10 16:16 redis-server
#修改本机IP
[root@master ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/192.168.100.104/g' /data/redis/redis.conf
#开启redis的保护模式
[root@master ~]# sed -i '/protected-mode/s/yes/no/g' /data/redis/redis.conf
#开启redis后台守护
[root@master ~]# sed -i '/daemonize/s/no/yes/g' /data/redis/redis.conf
#设置redis密码
[root@master ~]# sed -i '/requirepass/s/foobared/123123/g' /data/redis/redis.conf
#开启redis密码
[root@master ~]# sed -i '/requirepass 123123/s/^#//g' /data/redis/redis.conf
[root@master ~]# /data/redis/redis-server /data/redis/redis.conf
slave安装配置(双实例)
[root@slave ~]# mkdir /data/redis
[root@slave ~]# tar xf redis-5.0.4.tar.gz
[root@slave ~]# cd redis-5.0.4/
[root@slave redis-5.0.4]# make
[root@slave ~]# cp redis/src/redis-server /data/redis/
[root@slave ~]# cp redis/src/redis-cli /data/redis/
[root@slave ~]# cp redis/redis.conf /data/redis/
[root@slave ~]# ll /data/redis/
总用量 12672
-rwxr-xr-x 1 root root 4806256 10月 10 16:16 redis-cli
-rw-r--r-- 1 root root 62155 10月 10 16:16 redis.conf
-rwxr-xr-x 1 root root 8101856 10月 10 16:16 redis-server
[root@slave ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/192.168.100.105/g' /data/redis/redis.conf
[root@slave ~]# sed -i '/protected-mode/s/yes/no/g' /data/redis/redis.conf
[root@slave ~]# sed -i '/daemonize/s/no/yes/g' /data/redis/redis.conf
[root@slave ~]# sed -i '/requirepass/s/foobared/123123/g' /data/redis/redis.conf
[root@slave ~]# sed -i '/requirepass 123123/s/^#//g' /data/redis/redis.conf
[root@slave ~]# cp /data/redis/redis.conf /data/redis/redis01.conf
[root@slave ~]# sed -i '92s/6379/6380/g' /data/redis/redis01.conf #更改监听端口
[root@slave ~]# sed -i '158s/6379/6380/g' /data/redis/redis01.conf #更改PID文件
[root@slave ~]# sed -i '171s/^\(.\).\{9\}/logfile "\/data\/redis\/redis01.log"/g' /data/redis/redis01.conf #更改日志文件位置,9代表旧内容的字节数
[root@slave ~]# /data/redis/redis-server /data/redis/redis.conf
[root@slave ~]# /data/redis/redis-server /data/redis/redis01.conf
配置master角色的文件
[root@master ~]# vi /data/redis/redis.conf
457 min-replicas-to-write 2 #设置slave节点的数量,如果slave节点数量少于此值,那么master节点将停止客户端的一切写请求
458 min-replicas-max-lag 10 #master与slave之间同步数据的超时时间,若超过此时间,master节点将停止客户端的一切写操作
[root@master ~]# ps -ef|grep redis
[root@master ~]# kill -9 PID
[root@master ~]# /data/redis/redis-server /data/redis/redis.conf
配置slave角色的文件
[root@slave ~]# vi /data/redis/redis.conf
286 replicaof 192.168.100.104 6379 #指定master的ip地址以及端口
293 masterauth 123123 #指定master的连接密码
[root@slave ~]# vi /data/redis/redis01.conf
286 replicaof 192.168.100.104 6379
293 masterauth 123123
[root@master ~]# ps -ef|grep redis
[root@master ~]# kill -9 PID
[root@master ~]# /data/redis/redis-server /data/redis/redis.conf
验证主从复制
master创建内容
[root@master ~]# /data/redis/redis-cli -h 192.168.100.104 -p 6379 -a 123123
192.168.100.104:6379> set name xiaohong
OK
192.168.100.104:6379> get name
"xiaohong"
192.168.100.104:6379> info replication
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=192.168.100.105,port=6380,state=online,offset=312,lag=1
slave1:ip=192.168.100.105,port=6379,state=online,offset=312,lag=1
master_replid:a038ab9322bd51c9cb45d20b1a07058ac9bd9201
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:312
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:312
slave验证复制情况
[root@slave ~]# /data/redis/redis-cli -h 192.168.100.105 -p 6379 -a 123123
192.168.100.105:6379> keys *
1) "name"
192.168.100.105:6379> get name
"xiaohong"
[root@slave ~]# /data/redis/redis-cli -h 192.168.100.105 -p 6380 -a 123123
192.168.100.105:6380> keys *
1) "name"
192.168.100.105:6380> get name
"xiaohong"
重启master,验证RDB持久化
[root@master ~]# /data/redis/redis-server /data/redis/redis.conf
192.168.100.104:6379> keys *
1) "name"
192.168.100.104:6379> info replication
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=192.168.100.105,port=6380,state=online,offset=70,lag=1
slave1:ip=192.168.100.105,port=6379,state=online,offset=70,lag=2
master_replid:62644fe9ef3f876b8270f9eb9cce38d185995b4f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
redis集群sentinel
系统 | IP | 主机名 | 所需软件 | 端口 |
---|---|---|---|---|
centos7 | 192.168.100.104 | master | redis-5.0.4.tar.gz | redis:6379 ;redis sentinel:26379 |
centos7 | 192.168.100.105 | slave1 | redis-5.0.4.tar.gz | redis:6379 |
centos7 | 192.168.100.106 | slave2 | redis-5.0.4.tar.gz | redis:6379 |
安装所有节点的redis服务(所有节点配置相同)
[root@master ~]# tar xf redis-5.0.4.tar.gz
[root@master ~]# cd redis-5.0.4/
[root@master redis-5.0.4]# make
root@master redis-5.0.4]# cd
[root@master ~]# mkdir -p /data/redis
[root@master ~]# cp redis-5.0.4/redis.conf /data/redis/
[root@master ~]# cp redis-5.0.4/src/redis-server /data/redis/
[root@master ~]# cp redis-5.0.4/src/redis-cli /data/redis/
#master与slave分别改为本机IP
[root@master ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/192.168.100.104/g' /data/redis/redis.conf
#开启redis的保护模式
[root@master ~]# sed -i '/protected-mode/s/yes/no/g' /data/redis/redis.conf
#开启redis后台守护
[root@master ~]# sed -i '/daemonize/s/no/yes/g' /data/redis/redis.conf
#设置redis密码
[root@master ~]# sed -i '/requirepass/s/foobared/123123/g' /data/redis/redis.conf
#开启redis密码
[root@master ~]# sed -i '/requirepass 123123/s/^#//g' /data/redis/redis.conf
[root@master ~]# /data/redis/redis-server /data/redis/redis.conf
配置master角色的文件
[root@master ~]# vi /data/redis/redis.conf
457 min-replicas-to-write 2 #设置slave节点的数量,如果slave节点数量少于此值,那么master节点将停止客户端的一切写请求
458 min-replicas-max-lag 10 #master与slave之间同步数据的超时时间,若超过此时间,master节点将停止客户端的一切写操作
[root@master ~]# ps -ef|grep redis
[root@master ~]# kill -9 PID
[root@master ~]# /data/redis/src/redis-server /data/redis/redis.conf
配置slave角色的文件,两节点一致
[root@slave1 ~]# vi /data/redis/redis.conf
286 replicaof 192.168.100.104 6379 #指定master的ip地址以及端口
293 masterauth 123123 #指定master的连接密码
[root@slave2 ~]# vi /data/redis/redis01.conf
286 replicaof 192.168.100.104 6379
293 masterauth 123123
#两节点分别重启
[root@slave1 ~]# ps -ef|grep redis
[root@slave1 ~]# kill -9 PID
[root@slave1 ~]# /data/redis/redis-server /data/redis/redis.conf
验证主从同步
#master添加数据
[root@master ~]# /data/redis/redis-cli -h 192.168.100.104 -p 6379 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.104:6379> set name hello
OK
192.168.100.104:6379> get name
"hello"
#slave1验证同步情况
[root@slave1 ~]# /data/redis/redis-cli -h 192.168.100.105 -p 6379 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.105:6379> keys *
1) "name"
192.168.100.105:6379> get name
"hello"
#slave2验证同步情况
[root@slave2 ~]# /data/redis/redis-cli -h 192.168.100.106 -p 6379 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.106:6379> keys *
1) "name"
192.168.100.106:6379> get name
"hello"
在master节点配置redis sentinel
[root@master ~]# cp redis-5.0.4/src/redis-sentinel /data/redis/ # sentinel启动脚本
[root@master ~]# cp redis-5.0.4/sentinel.conf /data/redis/ # sentinel配置文件
[root@master ~]# mkdir -p /data/redis/sentinel_data #创建临时文件存放位置
[root@master ~]# touch /data/redis/sentinel_log/redis-sentinel.log #创建日志文件存放位置
[root@master ~]# touch /data/redis/sentinel_log/redis-sentinel.pid #创建pid文件存放位置
[root@master ~]# vi /data/redis/sentinel.conf
21 port 26379 #默认端口
31 pidfile "/data/redis/sentinel_log/redis-sentinel.pid"
36 logfile "/data/redis/sentinel_log/redis-sentinel.log"
65 dir "/data/redis/sentinel_data"
113 sentinel monitor master 192.168.100.104 6379 1 #1表示当一台master出现故障,则进行切换
121 sentinel down-after-milliseconds master 3000 #指定master的失效时间,单位毫秒,3秒
123 sentinel failover-timeout master 180000 #切换操作完成的时间周期为180秒,逾期认为切换失败
146 sentinel auth-pass master 123123 #连接master与slave节点的密码
219 sentinel config-epoch master 1 #切换后,最多有多少节点可以与新的master进行同步
[root@master ~]# /data/redis/redis-sentinel /data/redis/sentinel.conf & #后台启动
测试sentinel,关闭master
[root@master ~]# ps -ef|grep redis
root 5296 1 0 09:50 ? 00:00:03 /data/redis/redis-server 192.168.100.104:6379
root 19457 1131 0 10:33 pts/0 00:00:00 /data/redis/redis-sentinel *:26379 [sentinel]
root 19471 1131 0 10:35 pts/0 00:00:00 grep --color=auto redis
[root@master ~]# kill -9 5296
[root@master ~]# tail -100f /data/redis/sentinel_log/redis-sentinel.log
19457:X 11 Oct 2022 10:33:23.822 # Sentinel ID is 7be8f52b3f964a4533b30398a31d0e9b35370f8e
19457:X 11 Oct 2022 10:33:23.822 # +monitor master master 192.168.100.104 6379 quorum 1
19457:X 11 Oct 2022 10:35:35.228 # +sdown master master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.228 # +odown master master 192.168.100.104 6379 #quorum 1/1
19457:X 11 Oct 2022 10:35:35.228 # +new-epoch 2
19457:X 11 Oct 2022 10:35:35.228 # +try-failover master master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.233 # +vote-for-leader 7be8f52b3f964a4533b30398a31d0e9b35370f8e 2
19457:X 11 Oct 2022 10:35:35.233 # +elected-leader master master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.233 # +failover-state-select-slave master master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.297 # +selected-slave slave 192.168.100.105:6379 192.168.100.105 6379 @ master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.297 * +failover-state-send-slaveof-noone slave 192.168.100.105:6379 192.168.100.105 6379 @ master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.351 * +failover-state-wait-promotion slave 192.168.100.105:6379 192.168.100.105 6379 @ master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.414 # +promoted-slave slave 192.168.100.105:6379 192.168.100.105 6379 @ master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.414 # +failover-state-reconf-slaves master master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:35.472 * +slave-reconf-sent slave 192.168.100.106:6379 192.168.100.106 6379 @ master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:36.471 * +slave-reconf-inprog slave 192.168.100.106:6379 192.168.100.106 6379 @ master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:36.471 * +slave-reconf-done slave 192.168.100.106:6379 192.168.100.106 6379 @ master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:36.523 # +failover-end master master 192.168.100.104 6379
19457:X 11 Oct 2022 10:35:36.523 # +switch-master master 192.168.100.104 6379 192.168.100.105 6379
19457:X 11 Oct 2022 10:35:36.523 * +slave slave 192.168.100.106:6379 192.168.100.106 6379 @ master 192.168.100.105 6379
19457:X 11 Oct 2022 10:35:36.523 * +slave slave 192.168.100.104:6379 192.168.100.104 6379 @ master 192.168.100.105 6379
19457:X 11 Oct 2022 10:35:39.576 # +sdown slave 192.168.100.104:6379 192.168.100.104 6379 @ master 192.168.100.105 6379
#发现master已被切换到slave1节点中
验证主从复制
#slave1创建数据
[root@slave1 ~]# /data/redis/redis-cli -h 192.168.100.105 -p 6379 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.105:6379> keys *
1) "name"
192.168.100.105:6379> get name
"hello"
192.168.100.105:6379> set age 18
OK
192.168.100.105:6379> keys *
1) "name"
2) "age"
192.168.100.105:6379> get age
"18"
192.168.100.105:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.100.106,port=6379,state=online,offset=88860,lag=1
master_replid:9ca051d3ed04aadc319b318a1332d171e5141f0e
master_replid2:c333d4b3ab3c9631dbd3064fc28044b8c4a0159f
master_repl_offset:88860
second_repl_offset:56146
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:88860
#slave2验证
[root@slave2 ~]# /data/redis/redis-cli -h 192.168.100.106 -p 6379 -a 123123
192.168.100.106:6379> keys *
1) "age"
2) "name"
192.168.100.106:6379> get age
"18"
192.168.100.106:6379> info replication
# Replication
role:slave
master_host:192.168.100.105
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:87974
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9ca051d3ed04aadc319b318a1332d171e5141f0e
master_replid2:c333d4b3ab3c9631dbd3064fc28044b8c4a0159f
master_repl_offset:87974
second_repl_offset:56146
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:87974
重启原master节点
注意
:当原master角色作为新slave角色后,6379实例只会指定当前master的IP,不会自动生成密码,所以需要在6379实例配置文件中,添加当前master角色的连接密码
[root@master ~]# /data/redis/redis-server /data/redis/redis.conf
[root@master ~]# /data/redis/redis-cli -h 192.168.100.104 -p 6379 -a 123123
192.168.100.104:6379> info replication
# Replication
role:slave
master_host:192.168.100.105
master_port:6379
master_link_status:down #连接状态down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1665456311
slave_priority:100
slave_read_only:1
connected_slaves:0
min_slaves_good_slaves:0
master_replid:7c560da3493f99887f065310deace0e154bd7323
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.100.104:6379> exit
[root@master ~]# ps -ef|grep redis
[root@master ~]# kill -9 PID
[root@master ~]# echo "masterauth 123123" >>/data/redis/redis.conf
[root@master ~]# /data/redis/redis-server /data/redis/redis.conf
[root@master ~]# /data/redis/redis-cli -h 192.168.100.104 -p 6379 -a 123123
192.168.100.104:6379> info replication
# Replication
role:slave
master_host:192.168.100.105
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:108543
slave_priority:100
slave_read_only:1
connected_slaves:0
min_slaves_good_slaves:0
master_replid:9ca051d3ed04aadc319b318a1332d171e5141f0e
master_replid2:c333d4b3ab3c9631dbd3064fc28044b8c4a0159f
master_repl_offset:108543
second_repl_offset:1304
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1304
repl_backlog_histlen:107240
192.168.100.104:6379> exit
redis集群cluster
系统 | IP | 主机名 | 端口 | 软件 |
---|---|---|---|---|
centos7 | 192.168.100.103 | redis1 | 6379、6380 | redis-5.0.4.tar.gz |
centos7 | 192.168.100.107 | redis2 | 6379、6380 | redis-5.0.4.tar.gz |
centos7 | 192.168.100.108 | redis3 | 6379、6380 | redis-5.0.4.tar.gz |
安装redis,三处节点一致
[root@redis1 soft]# tar xf redis-5.0.4.tar.gz
[root@redis1 soft]# cd redis-5.0.4
[root@redis1 redis-5.0.4]# make
准备实例,每个节点两实例
[root@redis1 redis-5.0.4]# mkdir /data/redis{6379..6380}
[root@redis1 redis-5.0.4]# cp ./src/redis-server /data/redis6379/
[root@redis1 redis-5.0.4]# cp ./src/redis-server /data/redis6380/
[root@redis1 redis-5.0.4]# cp ./src/redis-cli /data/redis6379/
[root@redis1 redis-5.0.4]# cp ./src/redis-cli /data/redis6380/
[root@redis1 redis-5.0.4]# cp ./redis.conf /data/redis6379/
[root@redis1 redis-5.0.4]# cp ./redis.conf /data/redis6380/
[root@redis1 redis-5.0.4]# mkdir /data/redis6379/data
[root@redis1 redis-5.0.4]# mkdir /data/redis6380/data
[root@redis1 redis-5.0.4]# mkdir /data/redis6379/logs
[root@redis1 redis-5.0.4]# mkdir /data/redis6380/logs
修改主配置文件,端口不一致
3台都需要
[root@redis1 data]# vi redis6379/redis.conf
69 bind 192.168.100.103 #本机ip
92 port 6379 #端口
136 daemonize yes #设置redis后台运行
158 pidfile /data/redis6379/logs/redis_6379.pid #pid文件
171 logfile "/data/redis6379/logs/redis.log" #日志位置
263 dir /data/redis6379/data #数据文件存放位置,手动创建
293 masterauth <123123> #连接密码
507 requirepass 123123 #两处密码一致
699 appendonly yes #aof日志开启
838 cluster-enabled yes #开启cluster集群模式
846 cluster-config-file nodes-6379.conf #cluster集群配置文件,无需创建,自动生成
852 cluster-node-timeout 15000 #cluster超时时间,默认15s
[root@redis1 data]# cp redis6379/redis.conf redis6380/
[root@redis1 data]# vi redis6380/redis.conf
:%s/6379/6380/g #替换端口
启动redis实例,三节点一致
[root@redis1 data]# /data/redis6379/redis-server /data/redis6379/redis.conf
[root@redis1 data]# /data/redis6380/redis-server /data/redis6380/redis.conf
[root@redis1 data]# netstat -utpln |grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 13573/redis-server
tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 13578/redis-server
tcp 0 0 127.0.0.1:16379 0.0.0.0:* LISTEN 13573/redis-server
tcp 0 0 127.0.0.1:16380 0.0.0.0:* LISTEN 13578/redis-server
创建集群
[root@redis1 ~]# /data/redis6379/redis-cli -a 123123 --cluster create 192.168.100.103:6379 192.168.100.103:6380 192.168.100.107:6379 192.168.100.107:6380 192.168.100.108:6379 192.168.100.108:6380 --cluster-replicas 1
#-a 用户设置密码
#--cluster create:创建集群
#--cluster-replicas 1:表示一个master 对应一个slave
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.100.107:6380 to 192.168.100.103:6379
Adding replica 192.168.100.108:6380 to 192.168.100.107:6379
Adding replica 192.168.100.103:6380 to 192.168.100.108:6379
M: b3b16639d7afca9d17c05469cd948a33cf4b3a53 192.168.100.103:6379
slots:[0-5460] (5461 slots) master
S: 2c261a24460a10b0122011723249f4b68e7f52c7 192.168.100.103:6380
replicates 1e777760aa7dcad087128be0a71bc755a5bb26a7
M: 003753e3c19fd0a1f838d36dc55c47ccfafa83ce 192.168.100.107:6379
slots:[5461-10922] (5462 slots) master
S: 131132bebe4ebb68b5d2ffa66a2211b3a743de72 192.168.100.107:6380
replicates b3b16639d7afca9d17c05469cd948a33cf4b3a53
M: 1e777760aa7dcad087128be0a71bc755a5bb26a7 192.168.100.108:6379
slots:[10923-16383] (5461 slots) master
S: 0b0f303c5bff01dc731e58a117365fe0d01eb873 192.168.100.108:6380
replicates 003753e3c19fd0a1f838d36dc55c47ccfafa83ce
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.100.103:6379)
M: b3b16639d7afca9d17c05469cd948a33cf4b3a53 192.168.100.103:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 003753e3c19fd0a1f838d36dc55c47ccfafa83ce 192.168.100.107:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 2c261a24460a10b0122011723249f4b68e7f52c7 192.168.100.103:6380
slots: (0 slots) slave
replicates 1e777760aa7dcad087128be0a71bc755a5bb26a7
S: 0b0f303c5bff01dc731e58a117365fe0d01eb873 192.168.100.108:6380
slots: (0 slots) slave
replicates 003753e3c19fd0a1f838d36dc55c47ccfafa83ce
M: 1e777760aa7dcad087128be0a71bc755a5bb26a7 192.168.100.108:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 131132bebe4ebb68b5d2ffa66a2211b3a743de72 192.168.100.107:6380
slots: (0 slots) slave
replicates b3b16639d7afca9d17c05469cd948a33cf4b3a53
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
验证集群状态
[root@redis1 ~]# /data/redis6379/redis-cli -a 123123 -h 192.168.100.103 -p 6379 -c
#-c:表示集群,不可省略
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.103:6379> cluster nodes
003753e3c19fd0a1f838d36dc55c47ccfafa83ce 192.168.100.107:6379@16379 master - 0 1667103284000 3 connected 5461-10922
b3b16639d7afca9d17c05469cd948a33cf4b3a53 192.168.100.103:6379@16379 myself,master - 0 1667103281000 1 connected 0-5460
2c261a24460a10b0122011723249f4b68e7f52c7 192.168.100.103:6380@16380 slave 1e777760aa7dcad087128be0a71bc755a5bb26a7 0 1667103285549 5 connected
0b0f303c5bff01dc731e58a117365fe0d01eb873 192.168.100.108:6380@16380 slave 003753e3c19fd0a1f838d36dc55c47ccfafa83ce 0 1667103284529 6 connected
1e777760aa7dcad087128be0a71bc755a5bb26a7 192.168.100.108:6379@16379 master - 0 1667103283516 5 connected 10923-16383
131132bebe4ebb68b5d2ffa66a2211b3a743de72 192.168.100.107:6380@16380 slave b3b16639d7afca9d17c05469cd948a33cf4b3a53 0 1667103283000 4 connected
标签:root,redis,192.168,6379,master,data
From: https://www.cnblogs.com/lzexin/p/17844995.html