1分钟教你redis集群搭建(2服務器)

遇到有疑惑的可参考这个链接

注意: 关闭redis集群不能直接kill掉进程,或者关机,我们要通过命令redis-cli -p 7001 shutdown进行关闭,这样在关闭之前,数据才能够进行保存

1. 安装 redis

详:略

2. 创建 n 个 redis.conf 文件(redis 集群需要至少6个节点[3主3从])

详:

1
2
3
4
5
6
7
daemonize    yes                          //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7002,7003
port 7000 //端口7000,7002,7003
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 5000 //请求超时 设置5秒够了
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志

3. 用redis.server 启动 redis

1
2
3
4
5
6
7
8
101服務器
redis-server ./7000/redis.conf
redis-server ./7001/redis.conf
redis-server ./7002/redis.conf
102服務器
redis-server ./7003/redis.conf
redis-server ./7004/redis.conf
redis-server ./7005/redis.conf

4. 安装 redis (不安装也行 主要是为了安装redis-trib.rb脚本而已)

1
gem install redis(需要ruby rubygems)

5. 关联集群

1
redis-trib.rb  create  --replicas  1  172.16.86.101:7000 172.16.86.101:7001  172.16.86.101:7002 172.16.86.102:7003 172.16.86.102:7004 172.16.86.102:7005

6. 检查集群是否搭建完成

1
2
3
4
5
6
7
8
9
10
11
12
13
[chris-cai@localhost cluster]$ redis-cli -c -p 7000
127.0.0.1:7000> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:228
cluster_stats_messages_received:228

其他说明

1. redis常用命令放到Linux系统目录中,方便直接使用

这3个文件可以复制到/usr/local/bin 中,日后可以直接在linux命令行调用,无需切换目录输入路径等

1
2
3
redis-cli
redis-server
redis-trib.rb

2. 常用命令

1
2
3
4
redis-cli -h 172.16.86.102 -p 7003
redis-cli -c -p 7003
cluster nodes
CLUSTER INFO

让0b00721a509444db793d28448d8f02168b94bd38成为7000的从节点
1
redis-cli -c -p 7000 cluster replicate 0b00721a509444db793d28448d8f02168b94bd38

1分钟教你redis集群搭建(2服務器)

https://heyfl.gitee.io/redis/Redis-Install-Settring.html

作者

神奇宝贝大师

发布于

2017-03-08

更新于

2022-07-04

许可协议

评论