进学阁

业精于勤荒于嬉,行成于思毁于随

0%

Redis Linux安装

1.下载官方镜像

1
docker pull redis:7.0.5

2.准备安装位置

执行命令

1
2
3
4
5
mkdir -p /opt/server/redis/conf
chmod -R 777 /opt/server/redis
cd /opt/server/redis/conf
wget http://download.redis.io/redis-stable/redis.conf #下载redis的配置文件 (跳此步,直接下一步,我已经写好配置了)
vim redis.conf #更改配置文件, 让宿主机可以访问redis-server #保存并退出

3.编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#开启保护
protected-mode yes
#开启远程连接
#bind 127.0.0.1
#自定义密码
requirepass 12345678
port 6379
timeout 0
# 900s内至少一次写操作则执行bgsave进行RDB持久化
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec

参数说明:

1.修改保护模式protected-mode yes 默认为yes 可以跳过这一步

Redis protected-mode属性解读

设置外部网络连接redis服务,设置说明如下:

a.关闭protected-mode模式,此时外部网络可以直接访问

b.开启protected-mode保护模式,需配置bind ip 和设置访问密码 redis3.2版本后新增protected-mode配置,默认是yes,即开启。

2.把bind 127.0.0.1 注释掉 #bind 127.0.0.1, 这样所有的ip都可以访问了

3.设置密码(根据自己的需要)

设置永久密码的方法

找到requirepass foobared 把foobared改为自己的登陆密码 这里我设置为123456

requirepass 123456

设置临时密码的方法

在连接上redis后,config set设置临时密码,redis重启后,设置的密码就失效了

127.0.0.1:6379> config set requirepass “123456” # 设置密码为123456
OK
127.0.0.1:6379> config get requirepass # 获取密码的值

“requirepass”
“123456”

4.appendonly yes #开启AOF模式

4.书写docker-compose文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: '3.8'
services:
myredis:
container_name: myredis
image: redis:7.0.5
restart: always
ports:
- 6379:6379
privileged: true
command: redis-server /etc/redis/redis.conf --appendonly yes
volumes:
- ./redis/data:/data
- ./redis/conf/redis.conf:/etc/redis/redis.conf
networks:
- myweb
networks:
myweb:
driver: bridge

启动容器

1
2
3

# docker-compose up -d # 后台启动
# docker ps -a #查看启动的容器