一、环境准备
192.168.5.100 redis-01
192.168.5.101 redis-02
192.168.5.102 redis-03
关闭防火墙、能够通网
二、安装redis
[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.8.tar.gz
[root@localhost ~]# tar xf redis-5.0.8.tar.gz -C /usr/local/
[root@localhost ~]# yum -y install gcc gcc-c++
[root@localhost ~]# cd /usr/local/redis-5.0.8
[root@localhost ~]# make && make install
确认能启动、退出
[root@localhost redis-5.0.8]# redis-server --daemonize yes
12523:C 15 Nov 2023 10:02:04.043 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12523:C 15 Nov 2023 10:02:04.043 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=12523, just started
12523:C 15 Nov 2023 10:02:04.043 # Configuration loaded
[root@localhost redis-5.0.8]# ps -ef |grep redis
root 12508 12476 0 09:59 pts/1 00:00:00 redis-cli
root 12524 1 0 10:02 ? 00:00:00 redis-server *:6379
root 12529 8205 0 10:02 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-5.0.8]# redis-cli ping
PONG
[root@localhost redis-5.0.8]# pkill redis-server
[root@localhost redis-5.0.8]# redis-cli ping
Could not connect to Redis at 127.0.0.1:6379: Connection refused
三、开始搭建集群
1.首先创建6个空文件
[root@redis-01 ~]# mkdir -p /usr/local/redis-cluster/9001
[root@redis-01 ~]# mkdir -p /usr/local/redis-cluster/9002
[root@redis-02 ~]# mkdir -p /usr/local/redis-cluster/9003
[root@redis-02 ~]# mkdir -p /usr/local/redis-cluster/9004
[root@redis-03 redis-5.0.8]# mkdir -p /usr/local/redis-cluster/9005
[root@redis-03 redis-5.0.8]# mkdir -p /usr/local/redis-cluster/9006
2.copy redis.conf redis-server
[root@redis-01 redis-5.0.8]# cp redis.conf /usr/local/redis-cluster/9001
[root@redis-01 redis-5.0.8]# cp redis.conf /usr/local/redis-cluster/9002
[root@redis-02 redis-5.0.8]# cp redis.conf /usr/local/redis-cluster/9003
[root@redis-02 redis-5.0.8]# cp redis.conf /usr/local/redis-cluster/9004
[root@redis-03 redis-5.0.8]# cp redis.conf /usr/local/redis-cluster/9005
[root@redis-03 redis-5.0.8]# cp redis.conf /usr/local/redis-cluster/9006
[root@redis-01 src]# cp redis-server /usr/local/redis-cluster/9001/
[root@redis-01 src]# cp redis-server /usr/local/redis-cluster/9002/
[root@redis-02 src]# cp redis-server /usr/local/redis-cluster/9003/
[root@redis-02 src]# cp redis-server /usr/local/redis-cluster/9004/
[root@redis-03 src]# cp redis-server /usr/local/redis-cluster/9005/
[root@redis-03 src]# cp redis-server /usr/local/redis-cluster/9006/
3.修改redis.conf
在vim里你可以使用以下命令查找并删除注释行:
:g/^#/d
1)port 9001 //设置端口号
2)dir /usr/local/redis_cluster/9001/ //数据存放位置,必须要指定
3)cluster-enabled yes //启动集群模式
4)cluster-config-file nodes-9001.conf //集群节点信息文件
6)cluster-node-timeout 5000
bind 127.0.0.1 //去掉bind绑定访问ip信息
protected-mode no //关闭保护模式
9)appendonly yes //开启AOF模式,默认的持久化上RDB
[root@redis-01 redis-cluster]# cp 9001/redis.conf 9002/redis.conf
cp:是否覆盖"9002/redis.conf"? y
[root@redis-01 redis-cluster]# scp 9001/redis.conf 192.168.5.101:/usr/local/redis-cluster/9003/redis.conf
100% 1654 2.4MB/s 00:00
[root@redis-01 redis-cluster]# scp 9001/redis.conf 192.168.5.101:/usr/local/redis-cluster/9004/redis.conf
4、启动redis服务
[root@redis-01 redis-cluster]# redis-server /usr/local/redis-cluster/9001/redis.conf --daemonize yes
12684:C 15 Nov 2023 10:33:53.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12684:C 15 Nov 2023 10:33:53.480 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=12684, just started
12684:C 15 Nov 2023 10:33:53.480 # Configuration loaded
[root@redis-01 redis-cluster]# redis-cli ping
[root@redis-01 redis-cluster]# redis-cli -h 127.0.0.1 -p 9001 ping
PONG
5.创建redis集群(5.0版本以后直接使用redis-cli)
//-a *** 密码 如果redis.conf中配置了密码创建集群时需要添加
//--cluster-replicas 1 从节点个数
四、不重启redis集群修改最大内存设置
192.168.5.100:9001> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
[root@redis-01 9001]# redis-cli -h 192.168.5.100 -p 9001 config set maxmemory 4GB
OK
[root@redis-01 9001]# redis-cli -h 192.168.5.100 -p 9001 config rewrite
OK
[root@redis-01 9001]# redis-cli -h 192.168.5.100 -p 9001
192.168.5.100:9001> CONFIG GET maxmemory
1) "maxmemory"
2) "4294967296"