linux安装redis&Docker安装redis集群

本文详细介绍了如何手动从源代码编译安装Redis,并配置密码和远程访问。接着,展示了使用Docker搭建Redis主从及Sentinel哨兵集群的步骤,包括启动命令、配置文件修改和哨兵配置。最后提到了Docker启动Redis 7.0.4版本的注意事项。
摘要由CSDN通过智能技术生成

安装redis
Installation
From source code
Download, extract and compile Redis with:

$ wget https://download.redis.io/releases/redis-6.2.6.tar.gz
$ tar xzf redis-6.2.6.tar.gz
$ cd redis-6.2.6
$ make

The binaries that are now compiled are available in the src directory. Run Redis with:

$ src/redis-server & (一般要指定配置文件)

修改配置文件 
关闭注释requirepass 并配置密码 requirepass 123456
下面两个修改是为了远程登录
注释 # bind 127.0.0.1 -::1
注释 # protected-mode yes

停止redis
./src/redis-cli shutdown
如果设置了密要关闭时要
./src/redis-cli -a ‘密码’ shutdown (如果密码有特殊符号,整个密码要用单引号或双引号包裹起来)
### 带配置文件的启动
./src/redis-server ./redis.conf &

You can interact with Redis using the built-in client:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

docker搭建redis sentinel集群
一:启动redis主从

  1. 下载镜像
docker pull redis

2.启动三个redis镜像 作为一主二从

docker run -d --env TZ=Asia/Shanghai -p 6379:6379 -v /opt/services/redis/redis6379:/data --name redis6379 redis redis-server --requirepass '1234!@#$' --masterauth '1234!@#$'

进入容器redis6379查看ip
cat /etc/hosts
我的时 172.17.0.4
然后启动从节点

docker run -d --env TZ=Asia/Shanghai -p 6380:6379 -v /opt/services/redis/redis6380:/data --name redis6380 redis redis-server --requirepass '1234!@#$' --slaveof 172.17.0.4 6379 --masterauth '1234!@#$'

docker run -d --env TZ=Asia/Shanghai -p 6381:6379 -v /opt/services/redis/redis6381:/data --name redis6381 redis redis-server --requirepass '1234!@#$' --slaveof 172.17.0.4 6379 --masterauth '1234!@#$'

另一种方式:
通过配置文件启动

bind 0.0.0.0
port 6379
# 指定数据库个数
databases 9
# redis的工作目录
dir "/data/data"
logfile "/data/logs/redis.log"
requirepass 1234!@#$
# 有可能变为从slave 所以要配置连接主节点时的主节点密码
masterauth 1234!@#$
# 表明自身身份信息
replica-announce-ip 121.4.63.94
replica-announce-port 6379

从节点

bind 0.0.0.0
port 6379
# 指定数据库个数
databases 9
# redis的工作目录
dir "/data/data"
logfile "/data/logs/redis.log"
requirepass 1234!@#$
masterauth 1234!@#$
# 表明自身身份信息
replica-announce-ip 121.4.63.94
replica-announce-port 6380
# 表明从哪里复制 replicaof是新版slaveof是旧版写法 replicaof和sentinel.conf中的monitor marster节点要么都配公网要么都配内网 否则failover会失败
replicaof 121.4.63.94 6379
bind 0.0.0.0
port 6379
# 指定数据库个数
databases 9
# redis的工作目录
dir "/data/data"
logfile "/data/logs/redis.log"
requirepass 1234!@#$
masterauth 1234!@#$
# 表明自身身份信息
replica-announce-ip 121.4.63.94
replica-announce-port 6381
# 表明从哪里复制 replicaof是新版slaveof是旧版写法 replicaof和sentinel.conf中的monitor marster节点要么都配公网要么都配内网 否则failover会失败
replicaof 121.4.63.94 6379

启动redis 镜像

docker run -d --env TZ=Asia/Shanghai -p 6379:6379 -v /opt/services/redis/redis6379/data:/data/data -v /opt/services/redis/redis6379/logs:/data/logs -v /opt/services/redis/redis6379/conf:/etc/redis --name redis6379 redis redis-server /etc/redis/redis.conf
docker run -d --env TZ=Asia/Shanghai -p 6380:6379 -v /opt/services/redis/redis6380/data:/data/data -v /opt/services/redis/redis6380/logs:/data/logs -v /opt/services/redis/redis6380/conf:/etc/redis --name redis6380 redis redis-server /etc/redis/redis.conf
docker run -d --env TZ=Asia/Shanghai -p 6381:6379 -v /opt/services/redis/redis6381/data:/data/data -v /opt/services/redis/redis6381/logs:/data/logs -v /opt/services/redis/redis6381/conf:/etc/redis --name redis6381 redis redis-server /etc/redis/redis.conf

二.启动sentinel哨兵集群
新建 /opt/services/redis/sentinel26379/sentinel.conf
内容为,此内容sentinel启动后会重写

bind 0.0.0.0
port 26379
requirepass "1234!@#$"
# 哨兵sentinel的工作目录
dir "/data/data"
logfile "/data/logs/sentinel.log"
# daemonize yes打开后sentinel启动自动退出
# daemonize yes
sentinel monitor redismaster 121.4.63.94 6379 2
# 当在redis实例中开启了requirepass授权密码 这样所有连接redis实例的客户端都要提供密码
# 设置哨兵sentinel连接主从的密码 注意必须为主从设置一样的验证密码
sentinel auth-pass redismaster 1234!@#$
sentinel down-after-milliseconds redismaster 5000
# 表明自身身份
sentinel announce-ip 121.4.63.94
sentinel announce-port 26379
bind 0.0.0.0
port 26379
requirepass "1234!@#$"
# 哨兵sentinel的工作目录
dir "/data/data"
logfile "/data/logs/sentinel.log"
# daemonize yes打开后sentinel启动自动退出
# daemonize yes
sentinel monitor redismaster 121.4.63.94 6379 2
# 当在redis实例中开启了requirepass授权密码 这样所有连接redis实例的客户端都要提供密码
# 设置哨兵sentinel连接主从的密码 注意必须为主从设置一样的验证密码
sentinel auth-pass redismaster 1234!@#$
sentinel down-after-milliseconds redismaster 5000
# 表明自身身份
sentinel announce-ip 121.4.63.94
sentinel announce-port 26380
bind 0.0.0.0
port 26379
requirepass "1234!@#$"
# 哨兵sentinel的工作目录
dir "/data/data"
logfile "/data/logs/sentinel.log"
# daemonize yes打开后sentinel启动自动退出
# daemonize yes
sentinel monitor redismaster 121.4.63.94 6379 2
# 当在redis实例中开启了requirepass授权密码 这样所有连接redis实例的客户端都要提供密码
# 设置哨兵sentinel连接主从的密码 注意必须为主从设置一样的验证密码
sentinel auth-pass redismaster 1234!@#$
sentinel down-after-milliseconds redismaster 5000
# 表明自身身份
sentinel announce-ip 121.4.63.94
sentinel announce-port 26381

然后以redis-sentinel命令启动redis镜像
指定sentinel.conf配置文件映射到容器内时直接使用文件映射, 这么做有可能导致哨兵没有写入配置文件的权限, 表现为WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy. 解决方案:使用文件夹映射

docker run -d --env TZ=Asia/Shanghai -p 26379:26379 -v /opt/services/redis/sentinel26379/data:/data/data -v /opt/services/redis/sentinel26379/logs:/data/logs -v /opt/services/redis/sentinel26379/conf:/etc/redis-sentinel --name sentinel26379 redis redis-sentinel /etc/redis-sentinel/sentinel.conf

docker run -d --env TZ=Asia/Shanghai -p 26380:26379 -v /opt/services/redis/sentinel26380/data:/data/data -v /opt/services/redis/sentinel26380/logs:/data/logs -v /opt/services/redis/sentinel26380/conf:/etc/redis-sentinel --name sentinel26380 redis redis-sentinel /etc/redis-sentinel/sentinel.conf

docker run -d --env TZ=Asia/Shanghai -p 26381:26379 -v /opt/services/redis/sentinel26381/data:/data/data -v /opt/services/redis/sentinel26381/logs:/data/logs -v /opt/services/redis/sentinel26381/conf:/etc/redis-sentinel --name sentinel26381 redis redis-sentinel /etc/redis-sentinel/sentinel.conf

7.0.4版本
参考:https://hub.docker.com/_/redis
配置文件完全没变,只是redis启动加了两个参数

docker pull redis:7.0.4-alpine

启动redis 镜像

--privileged=true                                    容器内的root拥有真正root权限,否则容器内root只是外部普通用户权限
docker run -d --privileged=true --env TZ=Asia/Shanghai -p 6379:6379 -v /opt/services/redis/redis6379/data:/data/data -v /opt/services/redis/redis6379/logs:/data/logs -v /opt/services/redis/redis6379/conf:/etc/redis --name redis6379 redis:7.0.4-alpine redis-server /etc/redis/redis.conf --save 60 1
docker run -d --privileged=true --env TZ=Asia/Shanghai -p 6380:6379 -v /opt/services/redis/redis6380/data:/data/data -v /opt/services/redis/redis6380/logs:/data/logs -v /opt/services/redis/redis6380/conf:/etc/redis --name redis6380 redis:7.0.4-alpine redis-server /etc/redis/redis.conf --save 60 1
docker run -d --privileged=true --env TZ=Asia/Shanghai -p 6381:6379 -v /opt/services/redis/redis6381/data:/data/data -v /opt/services/redis/redis6381/logs:/data/logs -v /opt/services/redis/redis6381/conf:/etc/redis --name redis6381 redis:7.0.4-alpine redis-server /etc/redis/redis.conf --save 60 1

sentinel启动和6.2.6版本没变化,只是镜像版本号变化了

docker run -d --env TZ=Asia/Shanghai -p 26379:26379 -v /opt/services/redis/sentinel26379/data:/data/data -v /opt/services/redis/sentinel26379/logs:/data/logs -v /opt/services/redis/sentinel26379/conf:/etc/redis-sentinel --name sentinel26379 redis:7.0.4-alpine redis-sentinel /etc/redis-sentinel/sentinel.conf

docker run -d --env TZ=Asia/Shanghai -p 26380:26379 -v /opt/services/redis/sentinel26380/data:/data/data -v /opt/services/redis/sentinel26380/logs:/data/logs -v /opt/services/redis/sentinel26380/conf:/etc/redis-sentinel --name sentinel26380 redis:7.0.4-alpine redis-sentinel /etc/redis-sentinel/sentinel.conf

docker run -d --env TZ=Asia/Shanghai -p 26381:26379 -v /opt/services/redis/sentinel26381/data:/data/data -v /opt/services/redis/sentinel26381/logs:/data/logs -v /opt/services/redis/sentinel26381/conf:/etc/redis-sentinel --name sentinel26381 redis:7.0.4-alpine redis-sentinel /etc/redis-sentinel/sentinel.conf
要在docker安装redis集群,首先需要确保已经安装docker并启动了docker服务。可以按照引用中的步骤来卸载之前可能存在的残留文件,并通过阿里云docker yum源配置好docker安装源。 接下来,需要准备一个Linux系统机器,并在该机器上安装docker。确保已经对docker的基本知识有所了解,并熟悉docker的常用命令,可以参考引用中的博文来学习docker的基础知识。 一旦docker安装完成并启动了服务,就可以开始搭建redis集群了。可以在docker容器中安装redis,具体的安装步骤可以通过查阅相关文档或教程来获取。在安装redis之前,可以使用以下命令来启动docker服务: ``` systemctl enable docker systemctl start docker ``` 通过以下命令可以查看docker的版本信息: ``` docker version ``` 以上是在docker安装redis集群的大致步骤,具体的安装和配置过程可能会因为不同的环境和需求而有所差异。建议在安装之前仔细阅读相关的文档和教程,以确保正确地安装和配置redis集群。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [docker搭建redis集群详细篇](https://blog.csdn.net/m0_55116875/article/details/122220918)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [docker下搭建redis集群](https://blog.csdn.net/weixin_44947701/article/details/128161008)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值