1.下载redis服务包
tar xzf redis-5.0.7.tar.gz
cd redis-5.0.7
make
mkdir -p /usr/local/redis/bin
cp src/redis-server /usr/local/redis/bin
cp src/redis-cli /usr/local/redis/bin
cp src/redis-sentinel /usr/local/redis/bin
cp src/redis-trib.rb /usr/local/redis/bin
cp src/redis-check-aof /usr/local/redis/bin
cp src/redis-check-rdb /usr/local/redis/bin
cp src/redis-benchmark /usr/local/redis/bin
2.创建 6 个配置文件并配置(可以拿一个conf文件先修改配置文件,再依次修改端口后存放在/etc/redis-cluster/ 目录下,不需要执行cp redis.conf /etc/redis-cluster/redis-7001.conf这一操作。)
mkdir /etc/redis-cluster
cp redis.conf /etc/redis-cluster/redis-7001.conf
cp redis.conf /etc/redis-cluster/redis-7002.conf
cp redis.conf /etc/redis-cluster/redis-7003.conf
cp redis.conf /etc/redis-cluster/redis-7004.conf
cp redis.conf /etc/redis-cluster/redis-7005.conf
cp redis.conf /etc/redis-cluster/redis-7006.conf
mkdir /opt/redis-7001
mkdir /opt/redis-7002
mkdir /opt/redis-7003
mkdir /opt/redis-7004
mkdir /opt/redis-7005
mkdir /opt/redis-7006
3.修改配置文件
port 7001 # 监听端口
bind 0.0.0.0 # 监听 ip
dir /opt/redis-7001 # 指定文件存放路径 ( .rdb .aof nodes-xxxx.conf 这样的文件都会在此路径下) cluster-enabled yes # 启动集群模式
cluster-config-file nodes-7001.conf # 集群节点配置文件
daemonize yes # 后台启动
cluster-node-timeout 5000 # 集群节点超时时间
appendonly yes # 指定持久化方式,开启 AOF 模式
protected-mode no # 非保护模式
4.进入(usr/local/redis/bin目录下)依次启动六个节点
./redis-server /etc/redis-cluster/redis-7001.conf(示例)
5.创建集群
./redis-cli --cluster create 192.168.223.111:7001 192.168.223.111:7002 192.168.223.111:7003 192.168.223.111:7004 192.168.223.111:7005 192.168.223.111:7006 --cluster-replicas 1
结果如下:
6.查看集群信息
./redis-cli -c -h 192.168.223.111 -p 7001
7.对集群设置密码
依次对6个节点设置密码
./redis-cli -c -p 7001
config set masterauth test123
config set requirepass test123
auth test123
config rewrite
不需要重启集群
8.再次登录时未授权存取数据会报错。使用./redis-cli -c -h 192.168.223.111 -p 7001 -a ‘test123’
附:若通过客户端取值出现乱码,可使用命令:./redis-cli --raw -c -h 192.168.223.111 -p 7001 -a ‘test123’重新连接即可。