永夜-Evernight

永夜降临之前,你都有改变的资格

Docker安装Redis

安装redis

docker pull redis

安装好之后使用查看镜像

docker images
truedei@truedei:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              987b78fc9e38        10 days ago         104MB
httpd               latest              a8a9cbaadb0c        2 weeks ago         166MB
fjudith/draw.io     latest              7b136fc80d31        3 weeks ago         683MB
mysql               5.7.29              f5829c0eee9e        5 weeks ago         455MB

新建挂载文件

因为 redis 默认配置你会发现只能够本地连接,不能进行远程访问,使用 Redis Desktop Manager连接都会报错,因此需要手动挂载 redis 配置文件

  1. 新建配置文件

    因为需要redis的配置文件,这里最好还是去redis的官方去下载一个redis使用里面的配置文件即可
    redis中文官方网站:http://www.redis.cn/download.html

147352021ddf3f3c176decfb.webp

下载后解压出来

14735202d4605e4a41d40503.webp

  1. 配置redis.conf配置文件

    bind 127.0.0.1 #注释掉这部分,使redis可以外部访问

    daemonize no#用守护线程的方式启动

    requirepass 你的密码#给redis设置密码

    appendonly yes#redis持久化  默认是no

    tcp-keepalive 300 #防止出现远程主机强迫关闭了一个现有的连接的错误 默认是300

  2. 创建本地与docker映射的目录,即本地存放的位置

    truedei@truedei:redis-5.0.5$ sudo mkdir /usr/local/docker/
    

把配置文件拷贝到刚才创建好的文件夹里

启动redis

sudo docker run -p 6379:6379 --name redis --restart always -v /usr/local/docker/redis.conf:/etc/redis/redis.conf -v /usr/local/docker/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes

参数解释:

-p 6379:6379:把容器内的6379端口映射到宿主机6379端口

-v /usr/local/docker/redis.conf:/etc/redis/redis.conf:把宿主机配置好的redis.conf放到容器内的这个位置中

-v /usr/local/docker/data:/data:把redis持久化的数据在宿主机内显示,做数据备份

redis-server /etc/redis/redis.conf:这个是关键配置,让redis不是无配置启动,而是按照这个redis.conf的配置启动

–appendonly yes:redis启动后数据持久化

查看是否启动成功

docker ps

truedei@truedei:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
85cb7d83a2ff        redis               "docker-entrypoint.s…"   7 minutes ago       Up 7 minutes        0.0.0.0:6379->6379/tcp              redis
0a122a08125f        mysql:5.7.29        "docker-entrypoint.s…"   5 weeks ago         Up About an hour    0.0.0.0:3306->3306/tcp, 33060/tcp   mysql57
truedei@truedei:~$

可以查看一下日志:sudo docker logs redis

truedei@truedei:~$ sudo docker logs redis
1:C 29 May 2020 01:16:22.107 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 29 May 2020 01:16:22.107 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 29 May 2020 01:16:22.107 # Configuration loaded
                _._                                                
           _.-``__ ''-._                                           
      _.-``    `.  `_.  ''-._           Redis 6.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                 
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                 
 |`-._`-._    `-.__.-'    _.-'_.-'|                                
 |    `-._`-._        _.-'_.-'    |           http://redis.io      
  `-._    `-._`-.__.-'_.-'    _.-'                                 
 |`-._`-._    `-.__.-'    _.-'_.-'|                                
 |    `-._`-._        _.-'_.-'    |                                
  `-._    `-._`-.__.-'_.-'    _.-'                                 
      `-._    `-.__.-'    _.-'                                     
          `-._        _.-'                                         
              `-.__.-'                                             

1:M 29 May 2020 01:16:22.108 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 29 May 2020 01:16:22.108 # Server initialized
1:M 29 May 2020 01:16:22.108 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 29 May 2020 01:16:22.108 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 29 May 2020 01:16:22.108 * Ready to accept connections
truedei@truedei:~$

147352021c2631c720528606.webp


标题:Docker安装Redis
作者:luomuren
地址:http://luomuren.top/articles/2021/04/08/1617859497873.html