Skip to content

redis 7

sh
sudo mkdir -p /opt/redis7/{data,conf}

sudo chown -R :docker /opt/redis7

sudo find /opt/redis7 -type d -exec chmod 2775 {} \;

cd /opt/redis7

docker network create redis7-net
sh
vim .env
env
REDIS_PASSWORD=strong_redis_password_here
TZ=Asia/Shanghai
sh
vim /opt/redis7/conf/redis.conf
conf
bind 0.0.0.0
port 6379

requirepass strong_redis_password_here

# 开启 AOF,提升数据安全
appendonly yes
# 最多丢 1 秒数据,性能和安全折中
appendfsync everysec

# 900 秒(15 分钟)至少有 1 次RDB 快照
save 900 1
save 300 10
save 60 10000

dir /data
# 持久化
dbfilename dump.rdb
appendfilename appendonly.aof

# 限制 Redis 最多用多少内存
maxmemory 512mb
# 内存满时淘汰最近最少使用的 key
maxmemory-policy allkeys-lru

protected-mode yes
sh
vim docker-compose.yml
yml
services:
  redis:
    image: redis:7.4-alpine
    container_name: redis7
    restart: unless-stopped

    env_file:
      - .env

    command:
      - redis-server
      - /usr/local/etc/redis/redis.conf
      - --requirepass
      - ${REDIS_PASSWORD}

    ports:
      - "127.0.0.1:6379:6379"

    volumes:
      - ./data:/data
      - ./conf/redis.conf:/usr/local/etc/redis/redis.conf:ro

    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a $$REDIS_PASSWORD ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 5

    networks:
      - redis7-net

networks:
  redis7-net:
    external: true

隧道

ssh -N -L 16379:127.0.0.1:6379 cc

一些记录

测试连接

docker exec -it redis7 redis-cli -a strong_redis_password_here PING

测试

redis-cli -u 'redis://:你的密码@10.2.0.128:6379' ping