当前位置:网站首页>Linux下Redis安装及集群搭建
Linux下Redis安装及集群搭建
2022-06-29 09:28:00 【玄郭郭】
目录
Linux下Redis安装及集群搭建
Redis版本
Linux所有版本:http://download.redis.io/releases/
Windows所有版本:https://github.com/MicrosoftArchive/redis/releases
Linux下安装
注:由于redis是由C实现的,所以需要gcc来进行编译,执行下面的命令:
yum install -y gcc g++ gcc-c++ make
/*这是在linux联网的情况下,没有联网的情况下需要下载一些rpm包,安装gcc,这块自行百度。*/
1.将下载的tar包上传到服务器,或者通过wget命令进行下载。
tar -zxvf **.tar.gz 命令进行解压
[[email protected] redis]# tar -zxvf redis-5.0.5.tar.gz
2.进入解压后目录:
cd redis-5.0.5
然后输入make:
[[email protected] redis-5.0.5]# make
安装完c++编译器后 再次 make 若报此错
执行 make clean 后再次 执行 make ,再检查redis-x.x.x/src 目录下有没有 redis-server、redis-cli 和 /usr/local/bin下有没有
若无,则把redis-x.x.x 文件夹删掉,再解压一次redis的压缩包,cd进入 redis-x.x.x 中, make 一下 即可
如果make 出现如下情况,输入make MALLOC=libc

3.启动redis服务
make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:
下面启动redis服务.
cd src
./redis-server
注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动
cd src
./redis-server ../redis.conf
SpringBoot连接Redis服务问题
问题描述:SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enabled
解决方法:是说Redis服务处于保护模式,我们需要修改配置文件redis.conf。将NETWORK下的protected-mode yes修改
为protected-mode no,将bind 127.0.0.1 给注释掉
然后重启服务(./redis-server ../redis.conf)
设置redis后台启动,在redis.conf中将daemonize设置为daemonize yes
集群搭建
第一步:新建文件夹
在第一台机器的/usr/local下创建文件夹redis‐cluster,然后在其下面分别创建文件夾如下
(1)mkdir ‐p /usr/local/redis‐cluster
(2)mkdir 8001 8002 8003 8004 8005 8006
第二步:复制配置文件,修改配置参数
把之前的redis.conf配置文件copy到8001下,修改如下内容:
(1)daemonize yes
(2)port 8001(分别对每个机器的端口号进行设置)
(3)dir /usr/local/redis‐cluster/8001/(指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据)
(4)cluster‐enabled yes(启动集群模式)
(5)cluster‐config‐file nodes‐8001.conf(集群节点信息文件,这里800x最好和port对应上)
(6)cluster‐node‐timeout 5000
(7)# bind 127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
(8) protected‐mode no (关闭保护模式)
(9)appendonly yes
如果要设置密码需要增加如下配置:
(10)requirepass chenxuan(设置redis访问密码)
(11)masterauth chenxuan(设置集群节点间访问密码,跟上面一致)
第三步:修改集群所有配置
把修改后的配置文件,copy到8002,修改第2、3、5项里的端口号
第四步:启动redis
按照操作替换所有配置,然后启动redis
启动redis服务,不同的端口号就不同的redis服务
/opt/redis/redis-5.0.5/src/redis-server /usr/local/redis-cluster/8001/redis.conf


第五步:创建集群
创建集群 (‐a访问服务端密码,-cluster表示开启集群模式, create表示创建 --cluster-replicas:集群的redis服务们)
例:/opt/redis/redis-5.0.5/src/redis-cli -a chenxuan --cluster create --cluster-replicas 1 192.168.1.101:8001 192.168.1.101:8002 192.168.1.101:8003 192.168.1.101:8004 192.168.1.101:8005 192.168.1.101:8006
一般情况下会将前几个节点作为主节点,比如此时的8001,8002,8003就应该是主节点

第六步:登录集群,查看集群相关信息
登录集群中任意一个节点,连接任意一个客户端即可:./redis‐cli ‐c ‐h ‐p (‐a访问服务端密码,‐c表示集群模式,指定ip地址和端口号)
例:/opt/redis/redis-5.0.5/src/redis-cli -a chenxuan -c -h 192.168.1.101 -p 8001

cluster info(查看集群信息)

cluster nodes(查看节点列表)
最开头的那一串是这个节点的id,master 的是主节点 , slave的是子节点,子节点后面的那一串是主节点的id
默认一个redis集群会分16384个分片,比如下图的数据空间分片:一共三个主节点,那就是每个节点的数据空间分片大致为(16383/3 )=5461个分片。 端口是8001的redis的空间是0-5460,端口是8002的redis的空间是5461-10922,端口是8003的redis的空间是10923-16383。如果给redis分8G的内存的话,那么一个分片的内存就是0.5M,每个节点大概就是2730.5M的内存。

到这里我们的集群就已经搭建完毕了,这个时候就可以正常的使用redis了。
第八步:关闭集群
关闭集群的话,就要逐一关闭了
例:/opt/redis/redis-5.0.5/src/redis-cli -a chenxuan -c -h 192.168.1.101 -p 8002 shutdown

边栏推荐
- Beautiful ruins around Kiev -- a safe guide to Chernobyl!
- C语言库函数--strstr()
- Nacos registry cluster
- Installing and configuring wmware esxi 6.5.0 in VMware Workstation
- To 3 -- the last programming challenge
- DevExpress的双击获取单元格数据
- F5 big IP Icontrol rest command execution (cve-2022-1388)
- Analyze in detail the PBOT mining virus family behavior and the principle of exploited vulnerabilities, and provide detailed protection suggestions for the blue army
- How can I get the stock account opening discount? Also, is it safe to open an account online?
- 2019.11.3 learning summary
猜你喜欢

两个栈的模拟题

View CSDN blog rankings

Solve the problem that zxing's QR code contains Chinese garbled code

Call another interface button through win32API

How to quickly complete disk partitioning

C#中Linq常用用法

HDU 6778 car (group enumeration -- > shape pressure DP)

打印1000~2000年之间的闰年(C语言)

在VMware workstation中安装WMware ESXi 6.5.0并进行配置

软件测试模型(V模型和W模型)
随机推荐
MySQL innodb每行数据长度的限制
《CLR via C#》读书笔记-单实例应用程序
Solve the problem that zxing's QR code contains Chinese garbled code
Ural1517 freedom of choice [suffix array: longest common continuous substring]
51nod1277 maximum value in string [KMP]
HDU 4578 transformation (segment tree + skillful lazy tag placement)
指针数组、数组指针和传参的相关问题
L2-3 is this a binary search tree- The explanation is wonderful
函数指针、函数指针数组、计算器+转移表等归纳总结
2020-09-21 visual studio header file and Library Directory configuration
Win32exception (0x80004005): This program is blocked by group policy. For more information, contact your system administrator.
QGIS mapping
2019.10.16 training summary
Talk about threads and concurrency
C#中Linq常用用法
std::unique_ptr<T>与boost::scoped_ptr<T>的特殊性
L2-025 divide and rule (25 points)
Ce projet Open source est super wow, des photos manuscrites sont générées en ligne
2019.11.3 learning summary
BUUCTF--reverse2