redis安装(单机/集群)

参考 Linux 安装 Redis 6.0.6

1.获取资源路径

redis官网

2.准备

bash 复制代码
yum install wget
yum install gcc
#***gcc默认安装的gcc是4.8.5,版本过低,需要升级***

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
#查看版本号
gcc -v

2.编译

bash 复制代码
cd redis-6.0.6
make
make  install  PREFIX=/usr/local/redis/

# 配置环境变量
#从源码中把可执行文件迁出
make  install  PREFIX=/usr/local/redis/
vi /etc/profile
#增加以下内容:
export REDIS_HOME=/usr/local/redis/
export PATH=$REDIS_HOME/bin:$PATH
#使配置生效
source /etc/profile
#验证
echo $PATH

3. 创建redis实例

bash 复制代码
cd /usr/local/redis-6.0.6/utils
./install_server.sh
css 复制代码
这步有可能会报错:`
`This systems seems to use systemd. Please take a look at the provided example service unit files in this directory, and adapt and install t hem. Sorry!

解决方案:

bash 复制代码
vi ./install_server.sh
1

注释下面的代码即可

shell 复制代码
	#bail if this system is managed by systemd
	#_pid_1_exe="$(readlink -f /proc/1/exe)"
	#if [ "${_pid_1_exe##*/}" = systemd ]
	#then
	#       echo "This systems seems to use systemd."
	#       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
	#       exit 1
	#fi

然后重新运行 ./install_server.sh即可。 按几下回车

makefile 复制代码
如果出现错误:
# Please select the redis executable path [] Mmmmm...  it seems like you don't have a redis executable..
需要在path || 后手动输入路径
perl 复制代码
#查看该实例的运行状态
service redis_6379 status
#停止
service redis_6379 stop
#启动
service redis_6379 start

[root@localhost utils]# ps -ef|grep redis
root       3831      1  0 10:03 ?        00:00:01 /usr/local/redis//bin/redis-server 127.0.0.1:6379
root      12076   1434  0 10:26 pts/0    00:00:00 grep --color=auto redis
#需要开启外部访问
1.找到redis中的redis.cof文件并打开
2.找到bind 127.0.0.1并注释掉
3.修改 protected-mode 属性值为no
#启动 redis-server /usr/local/redis-6.0.6/redis.conf
#注:redis默认是只能本地访问,注释掉并叫保护模式禁用以后可以IP访问

2.集群搭建

参考 CSDN 博客园

准备

复制源目录根目录下的redis.conf文件到生成的/usr/local/redis目录下

使用vi编辑配置redis.conf

yaml 复制代码
bind 0.0.0.0  设置绑定的主机ip,0.0.0.0表示监听所有地址

protected-mode no  关闭安全模式,允许外网访问

daemonize yes  后台启动

port 7001  修改端口号,各节点不重复

cluster-enabled yes  开启集群支持

复制redis实例

bash 复制代码
# 创建文件夹
mkdir redis-cluster
# 可以使用一台机器,我使用三台,每台两个
cp -r redis redis-cluster/redis01
cp -r redis redis-cluster/redis02
...

启动所有节点

bash 复制代码
[root@redis121 redis02] ./redis-server redis.conf
[root@redis121 redis01] ./redis-server redis.conf
...

创建集群

vbnet 复制代码
# 进入到集群中某个实例下的bin目录执行
[root@redis121 redis01] /usr/local/redis-cluster/redis01/redis-cli --cluster create 192.168.25.104:7001 192.168.25.104:7004 1            92.168.25.121:7002 192.168.25.121:7005 192.168.25.122:7003 192.168.25.122:7006 --cluster-replicas 1

>>> 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.25.121:7005 to 192.168.25.104:7001
Adding replica 192.168.25.122:7006 to 192.168.25.121:7002
Adding replica 192.168.25.104:7004 to 192.168.25.122:7003
M: 81e6d7cb24891849940d73f9e9c10ac0545cd65d 192.168.25.104:7001
   slots:[0-5460] (5461 slots) master
S: c2bbaeb68c172aa1fed55e519b86347f6bd2b9b7 192.168.25.104:7004
   replicates 63e280dae17aa3d980f6c4fe2c0532a11b83616f
M: f957b6e222f6a3c996a4ba0fa2a77dec01524266 192.168.25.121:7002
   slots:[5461-10922] (5462 slots) master
S: b80c9fcea4f6d2a9d1db68954848075311542598 192.168.25.121:7005
   replicates 81e6d7cb24891849940d73f9e9c10ac0545cd65d
M: 63e280dae17aa3d980f6c4fe2c0532a11b83616f 192.168.25.122:7003
   slots:[10923-16383] (5461 slots) master
S: 2e55f80db5784568a26b3ae2b3418e9700bfe811 192.168.25.122:7006
   replicates f957b6e222f6a3c996a4ba0fa2a77dec01524266
Can I set the above configuration? (type 'yes' to accept): yes  # 输入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.25.104:7001)
M: 81e6d7cb24891849940d73f9e9c10ac0545cd65d 192.168.25.104:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2e55f80db5784568a26b3ae2b3418e9700bfe811 192.168.25.122:7006
   slots: (0 slots) slave
   replicates f957b6e222f6a3c996a4ba0fa2a77dec01524266
M: f957b6e222f6a3c996a4ba0fa2a77dec01524266 192.168.25.121:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: c2bbaeb68c172aa1fed55e519b86347f6bd2b9b7 192.168.25.104:7004
   slots: (0 slots) slave
   replicates 63e280dae17aa3d980f6c4fe2c0532a11b83616f
M: 63e280dae17aa3d980f6c4fe2c0532a11b83616f 192.168.25.122:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: b80c9fcea4f6d2a9d1db68954848075311542598 192.168.25.121:7005
   slots: (0 slots) slave
   replicates 81e6d7cb24891849940d73f9e9c10ac0545cd65d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 如果一直在 Waiting for the cluster to join.....................
# 停掉所有节点,删除文件夹底下的nodes.conf,dump.rdb
# 修改 pidfile /var/run/redis_7001.pid  为端口
# 重启,重新执行

连接

bash 复制代码
# 使用redis-cli连接  -c 表示集群模式,不带的话无法执行操作  在本机的节点不用输入ip
[root@redis104 redis01] ./redis-cli -c -p 7002
# 如果节点在其他服务器 需要加 -h
[root@redis104 redis01] ./redis-cli -c -h 192.168.25.122 -p 7006
相关推荐
longlong int18 分钟前
【每日算法】Day 16-1:跳表(Skip List)——Redis有序集合的核心实现原理(C++手写实现)
数据库·c++·redis·算法·缓存
努力努力再努力wz1 小时前
【c++深入系列】:类与对象详解(中)
java·c语言·开发语言·c++·redis
西元.2 小时前
详解 Redis repl_backlog_buffer(如何判断增量同步)
数据库·redis·缓存
Kale又菜又爱玩8 小时前
深入探索Redisson:用法全解析及在微服务中的关键应用
redis·微服务·架构
LUCIAZZZ8 小时前
Redisson中BitMap位图的基本操作
java·spring boot·redis·spring
SimonKing9 小时前
3ms找出微信「共同好友」,揭秘其背后的技术推手
java·redis·后端
纪元A梦11 小时前
Redis最佳实践——用户会话管理详解
数据库·redis·缓存
码熔burning11 小时前
Redis 线程模型:单线程也能快如闪电?
数据库·redis·缓存
树下一少年12 小时前
通过ansible+docker-compose快速安装一主两从redis+三sentinel
redis·docker·ansible·sentinel·docker-compose