下载镜像

docker pull redis:7.2.3

之后可以使用 docker image 来查看已经下载下来的镜像。

创建持久化文件夹

需要找个地方放一下文件夹:

└─Redis
    ├─conf
    └─data

下载并修改 redis 配置

找对应版本的配置文件内容:Redis configuration 全文复制,到刚才新创建的 conf 文件夹中新建一个 redis.conf 文件并粘贴进去。

接着需要修改两个地方:

  • 监听连接设置改为所有设备
  • 设置连接密码

注释掉 bind 127.0.0.1 -::1 即可实现监听所有设备

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# COMMENT OUT THE FOLLOWING LINE.
#
# You will also need to set a password unless you explicitly disable protected
# mode.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1 -::1
 
# By default, outgoing connections (from replica to master, from Sentinel to
# instances, cluster bus, etc.) are not bound to a specific local address. In
# most cases, this means the operating system will handle that based on

接着全局搜索 requirepass 来找到修改密码的地方,取消注释并加上自己的密码即可:

# if they follow the new protocol: both will work.
#
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
requirepass he77495161
 
# New users are initialized with restrictive permissions by default, via the
# equivalent of this ACL rule 'off resetkeys -@all'. Starting with Redis 6.2, it
# is possible to manage access to Pub/Sub channels with ACL rules as well. The

启动容器

docker run -p 6379:6379 --name redis2 -v E:\Study\Projects\XiaoHaShu\Redis\conf\redis.conf:/etc/redis/redis.conf  -v E:\Study\Projects\XiaoHaShu\Redis\data:/data -d redis:7.2.3 redis-server /etc/redis/redis.conf --appendonly yes

以上命令是创建并启动一个名为 redis2 的容器,开放端口为 6379,并挂在本机的 E:\Study\Projects\XiaoHaShu\Redis\conf\redis.conf 文件到容器的 /etc/redis/redis.conf 中,这使得配置得以应用到 redis中,以及data目录。

-d 标识让容器运行到后台,不阻塞当前终端。

redis-server /etc/redis/redis.conf --appendonly yes 是容器启动后执行的命令,其作用是启动 Redis 服务器,并制定使用的配置文件,以及设置 AOF 持久化模式,即确保 Redis 服务器将每个写操作都追加到持久化文件中,以提高文件可靠性。

Redis 图形化客户端

最后再下个 Redis 图形化客户端,便于操作:Releases · qishibo/AnotherRedisDesktopManager