Redis教程

Redis-6.0.7 For Linux7.* 集群

本文主要是介绍Redis-6.0.7 For Linux7.* 集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

部署 Redis 6.0.7 集群

操作系统   :Oracle Linux 7.9
redis版本  :redis-6.0.7

1、安装 gcc,make,wget
Redis是C语言开发的,安装前是需要先进行编译,编译依赖于gcc环境,这里需要安装gcc。
yum install -y gcc-c++ make wget 

2、下载 Redis 安装包
wget http://download.redis.io/releases/redis-6.0.7.tar.gz
wget http://download.redis.io/releases/redis-6.2.5.tar.gz

3、编译安装 Redis
#############################################################################
## 如果make 报错了,可能是gcc版本问题,解决措施如下:
## 查看gcc的版本是否在 5.3以上,Centos7 , Oracle Linux 7.9 默认是4.8.5.我这里的就是4.8.5
gcc -v

## 解决方案:升级到 5.3及以上版本:
1)、安装gcc全家桶 : yum install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make
2)、升级gcc : yum -y install centos-release-scl devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
wget http://mirror.centos.org/centos/7/sclo/x86_64/rh/Packages/d/devtoolset-9-gcc-9.3.1-2.2.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/sclo/x86_64/rh/Packages/d/devtoolset-9-gcc-c++-9.3.1-2.2.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/sclo/x86_64/rh/Packages/d/devtoolset-9-runtime-9.1-0.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/sclo/x86_64/rh/Packages/d/devtoolset-9-libstdc++-devel-9.3.1-2.2.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/sclo/x86_64/rh/Packages/d/devtoolset-9-binutils-2.32-16.el7.x86_64.rpm
yum localinstall -y devtoolset-9-*

3)、设置永久生效 : echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile 
    设置临时生效 : make install 完成后,可以不用。
    # scl enable devtoolset-9 bash (推荐使用这个方法) 
    # gcc -v 
    gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

#############################################################################

3.1、## 编译安装: -- 默认安装路径 :/usr/local/bin/redis-server
## 解压:
tar xzf redis-6.0.7.tar.gz
cd redis-6.0.7 && make && make install

tar xzf redis-6.2.5.tar.gz
cd redis-6.2.5 && make && make install

3.2、## 编译安装: -- 指定安装路径 : PREFIX=  路径会自动创建   -- 推荐:
## 解压:
tar xzf redis-6.0.7.tar.gz
cd redis-6.2.5 && make && make install PREFIX=/opt/redis

## 解压:
tar xzf redis-6.2.5.tar.gz
cd redis-6.2.5 && make && make install PREFIX=/opt/redis


设置环境变量: vi /etc/profile
cat /etc/profile
echo "###################################" >> /etc/profile
echo "export REDIS_HOME=/opt/redis"   >> /etc/profile
echo "export PATH=\$REDIS_HOME/bin:\$PATH"  >> /etc/profile
echo "###################################" >> /etc/profile
grep -i redis /etc/profile
source /etc/profile
env | grep -i redis


3.3、## 卸载: -- 进入源码目录 :
## cd redis-6.2.5 && make uninstall


3.4、## 启动测试:
redis-server redis.conf


4、创建Redis集群

### Redis port number too high. Cluster communication port is 10,000 port numbers higher than your Redis port. 
### Your Redis port number must be lower than 55535.

## 通过脚本创建6个redis配置:
## 指定路径:-- 与安装路径放在一起,便于管理: 端口号不能大于55535 :

###########################################################################
###########################################################################
## 同一个系统,使用不同的端口:
for NODE_ID in $(seq -w 6371 6376); \
do \
mkdir -p /opt/redis/node-${NODE_ID}/conf
mkdir -p /opt/redis/logs
mkdir -p /opt/redis/node-${NODE_ID}/data
touch /opt/redis/node-${NODE_ID}/conf/redis.conf
cat << EOF >/opt/redis/node-${NODE_ID}/conf/redis.conf
#####################################
port ${NODE_ID}
bind 0.0.0.0
pidfile /var/run/redis_${NODE_ID}.pid
logfile /opt/redis/logs/redis_${NODE_ID}.log
dir /opt/redis/node-${NODE_ID}/data
cluster-enabled yes
cluster-config-file /opt/redis/node-${NODE_ID}/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
requirepass myRedis
#####################################
EOF
done

###########################################################################
###########################################################################

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## 给参数文件 增加 几个参数:
for NODE_ID in $(seq -w 6371 6376); \
do \
cat << EOF >> /opt/redis/node-${NODE_ID}/conf/redis.conf
requirepass myRedis
EOF
cat /opt/redis/node-${NODE_ID}/conf/redis.conf
done
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

###########################################################################
###########################################################################
## 不同的系统,使用相同的端口:
for NODE_ID in $(seq -w 1 6); \
do \
mkdir -p /opt/redis/node-${NODE_ID}/conf
mkdir -p /opt/redis/logs
mkdir -p /opt/redis/node-${NODE_ID}/data
touch /opt/redis/node-${NODE_ID}/conf/redis.conf
cat << EOF >/opt/redis/node-${NODE_ID}/conf/redis.conf
port 6379
bind 0.0.0.0
pidfile /var/run/redis_${NODE_ID}.pid
logfile /opt/redis/logs/redis_${NODE_ID}.log
dir /opt/redis/node-${NODE_ID}/data
cluster-enabled yes
cluster-config-file /opt/redis/node-${NODE_ID}/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
requirepass myRedis
EOF
done

###########################################################################
###########################################################################

## 启动六个服务:  -- 创建 redis 集群 需要先启动 6 个服务:
for port in $(seq -w 01 6); \
do \
redis-server /root/redis/node-${port}/conf/redis.conf
done

for port in $(seq -w 6371 6376); \
do \
redis-server /opt/redis/node-${port}/conf/redis.conf
done

## 查看:
ps -ef |grep redis-server
netstat -lantp | grep redis

## 停止:
redis-cli -h 127.0.0.1 -c -p 6371 shutdown

#################################
## 批量停止 redis 服务 :
ps -ef | grep redis
ps -ef | grep redis |grep -v grep | awk '{print $9}' | awk -F ":" '{print $2}' | xargs -n1 -t -I {} redis-cli -h 127.0.0.1 -c -p {} shutdown 
ps -ef | grep redis
#################################
 

5、创建集群
## 需要先启动6个redis :
redis-cli --cluster create 127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373 127.0.0.1:6374 127.0.0.1:6375 127.0.0.1:6376 --cluster-replicas 1

redis-cli --cluster create 192.168.56.11:6371 192.168.56.11:6372 192.168.56.11:6373 192.168.56.11:6374 192.168.56.11:6375 192.168.56.11:6376 --cluster-replicas 1

## 指定 密码: -a myRedis
redis-cli --cluster create 192.168.56.11:6371 192.168.56.11:6372 192.168.56.11:6373 192.168.56.11:6374 192.168.56.11:6375 192.168.56.11:6376 --cluster-replicas 1 -a myRedis


中途输入一次yes。


6、测试
## 无密码:
redis-cli -h 127.0.0.1 -c -p 6371 cluster info
redis-cli -h 127.0.0.1 -c -p 6371 cluster nodes

redis-cli -h 127.0.0.1 -c -p 6371 set test1 "who are you ?"
redis-cli -h 127.0.0.1 -c -p 6372 get test1
redis-cli -h 127.0.0.1 -c -p 6373 get test1

## 有密码:-a myRedis 
redis-cli -h 127.0.0.1 -c -p 6371 -a myRedis cluster info
redis-cli -h 127.0.0.1 -c -p 6371 -a myRedis cluster nodes

redis-cli -h 127.0.0.1 -c -p 6371 -a myRedis set test1 "who are you ?"
redis-cli -h 127.0.0.1 -c -p 6372 -a myRedis get test1
redis-cli -h 127.0.0.1 -c -p 6373 -a myRedis get test1


测试下集群:
# redis-cli -h 127.0.0.1 -c -p 6371
127.0.0.16371> set hello world

# redis-cli -h 127.0.0.1 -c -p 6372
127.0.0.16371> get hello

# redis-cli -h 127.0.0.1 -c -p 6373
127.0.0.16371> get hello

# redis-cli -h 127.0.0.1 -c -p 6374
127.0.0.16371> get hello


我们kill掉 127.0.0.1:6371 再来看一下: 

# redis-cli -h 127.0.0.1 -c -p 6372
127.0.0.16371> get hello


# redis-cli -h 127.0.0.1 -c -p 6373
127.0.0.16371> get hello


# redis-cli -h 127.0.0.1 -c -p 6374
127.0.0.16371> get hello


7、开机启动设置:
## 在/etc/rc.local  加入如下行:

cat /etc/rc.local
ls -l /etc/rc.d/rc.local

for PORT in $(seq -w 6371 6376); \
do \
echo "/opt/redis/bin/redis-server /opt/redis/node-${PORT}/conf/redis.conf" >> /etc/rc.local
done

cat /etc/rc.local
ls -l /etc/rc.d/rc.local
chmod u+x /etc/rc.d/rc.local
ls -l /etc/rc.d/rc.local

8、检查
输入命令查看集群节点状态   -c 表示登陆集群
# redis-cli -c -p 6371
127.0.0.1:7700> cluster info 
127.0.0.1:7700> cluster nodes 

## 无密码:
redis-cli -h 127.0.0.1 -c -p 6371 cluster info
redis-cli -h 127.0.0.1 -c -p 6371 cluster nodes

redis-cli -h 127.0.0.1 -c -p 6371 set test1 "who are you ?"
redis-cli -h 127.0.0.1 -c -p 6372 get test1
redis-cli -h 127.0.0.1 -c -p 6373 get test1

## 有密码:-a myRedis 
redis-cli -h 127.0.0.1 -c -p 6371 -a myRedis cluster info
redis-cli -h 127.0.0.1 -c -p 6371 -a myRedis cluster nodes

redis-cli -h 127.0.0.1 -c -p 6371 -a myRedis set test1 "who are you ?"
redis-cli -h 127.0.0.1 -c -p 6372 -a myRedis get test1
redis-cli -h 127.0.0.1 -c -p 6373 -a myRedis get test1


[root@dbserver01 logs]#  redis-cli -h 127.0.0.1 -c -p 6371 cluster nodes
30feb5f4063f78c7cf3ba56f18397036a064d1a7 127.0.0.1:6374@16374 slave fb4debd33d38ea17c9b6531e69459e50be6add8e 0 1631505989558 2 connected
97801d353acbf8581c1cdb626aaf881eb584694b 127.0.0.1:6373@16373 master - 0 1631505988927 3 connected 10923-16383
ebe56974728eb99dfbffd7d5a0b20f6e4d4e6406 127.0.0.1:6375@16375 slave 97801d353acbf8581c1cdb626aaf881eb584694b 0 1631505988000 3 connected
79b7abc3c1d1ae715de45ae23411b92bdf602914 127.0.0.1:6371@16371 myself,master - 0 1631505989000 1 connected 0-5460
fb4debd33d38ea17c9b6531e69459e50be6add8e 127.0.0.1:6372@16372 master - 0 1631505989000 2 connected 5461-10922
ec199979355e2eb203f2d96876ad61213e7daa22 127.0.0.1:6376@16376 slave 79b7abc3c1d1ae715de45ae23411b92bdf602914 0 1631505989961 1 connected
[root@dbserver01 logs]#
 
[root@dbserver01 logs]#  redis-cli -h 127.0.0.1 -c -p 6371
127.0.0.1:6371> 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_ping_sent:322
cluster_stats_messages_pong_sent:319
cluster_stats_messages_sent:641
cluster_stats_messages_ping_received:314
cluster_stats_messages_pong_received:322
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:641
127.0.0.1:6371>
127.0.0.1:6371> cluster nodes
30feb5f4063f78c7cf3ba56f18397036a064d1a7 127.0.0.1:6374@16374 slave fb4debd33d38ea17c9b6531e69459e50be6add8e 0 1631505561598 2 connected
97801d353acbf8581c1cdb626aaf881eb584694b 127.0.0.1:6373@16373 master - 0 1631505561000 3 connected 10923-16383
ebe56974728eb99dfbffd7d5a0b20f6e4d4e6406 127.0.0.1:6375@16375 slave 97801d353acbf8581c1cdb626aaf881eb584694b 0 1631505562607 3 connected
79b7abc3c1d1ae715de45ae23411b92bdf602914 127.0.0.1:6371@16371 myself,master - 0 1631505560000 1 connected 0-5460
fb4debd33d38ea17c9b6531e69459e50be6add8e 127.0.0.1:6372@16372 master - 0 1631505560539 2 connected 5461-10922
ec199979355e2eb203f2d96876ad61213e7daa22 127.0.0.1:6376@16376 slave 79b7abc3c1d1ae715de45ae23411b92bdf602914 0 1631505561497 1 connected
127.0.0.1:6371>


## 查看:
ps -ef |grep redis-server
netstat -lantp | grep redis

## 日志查看:
tail -f /opt/redis/logs/*.log
       

到这里,我们的Redis集群就部署成功了!!


###########################################################
###########################################################
redis可视化工具RDM连接不上服务器redis问题
一、首先确定服务器本地Redis可以访问

二、查看redis.conf配置文件
确保以下两项配置
1.bind 127.0.0.1改为 #bind 127.0.0.1
2.protected-mode yes 改为 protected-mode no

ps: protected-mode 是3.2 之后加入的新特性,是为了禁止公网访问redis cache,加强redis安全的。
它启用的条件,有两个:
1) 没有bind IP
2) 没有设置访问密码

解决:
刚好我们注释了bing 127.0.0.1 并且默认redis.conf也是没有设置密码的所以触发了protected-mode 我们将它关闭即可 生产环境请慎用。
###########################################################
###########################################################  

这篇关于Redis-6.0.7 For Linux7.* 集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!