docker compose up 生成的 mysql 和 redis 容器一直restarting 问题排查

docker compose 生成的 mysql 和 redis 容器一直restarting 问题排查

查看容器信息

PS D:\coder\go-coder\go-training-camp\webook> docker container ls -a  
CONTAINER ID   IMAGE                  COMMAND                  CREATED        STATUS                          PORTS     NAMES
fa46e0b3edab   mysql:8.0              "docker-entrypoint.s…"   20 hours ago   Restarting (1) 14 seconds ago             webook-mysql8-1
1a9b3c1083ea   bitnami/redis:latest   "/opt/bitnami/script…"   20 hours ago   Restarting (1) 31 seconds ago             webook-redis-1
ba41d679a2fd   2461b2698dcd           "/usr/bin/dumb-init …"   21 hours ago   Up 21 hours                               k8s_controller_ingress-nginx-controller-6c646f59bb-7lsx2_ingress
-nginx_8bb52d7c-eb44-4c47-95af-a0a385325e59_14

webook-mysql8-1 容器ID fa46e0b3edab 根据 容器ID 查容器日志

2024-09-09 00:38:54+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.39-1.el9 started.
2024-09-09 00:39:57+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2024-09-09 00:39:57+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.39-1.el9 started.
2024-09-09 00:39:57+00:00 [Note] [Entrypoint]: Initializing database files
2024-09-09T00:39:57.777179Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size
=0 instead.
2024-09-09T00:39:57.777281Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy ins
tead.
2024-09-09T00:39:57.777300Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.39) initializing of server in progress as process 81
2024-09-09T00:39:57.779271Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2024-09-09T00:39:57.779278Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2024-09-09T00:39:57.779355Z 0 [ERROR] [MY-010119] [Server] Aborting
2024-09-09T00:39:57.779672Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.39)  MySQL Community Server - GPL.

###
###error 日志翻译
2024-09-08T03:49:24.253417Z 0[错误][MY-010457][服务器]–指定了初始化,但数据目录中有文件。正在中止。
2024-09-08T03:49:24.253423Z 0[错误][MY-013236][服务器]指定的数据目录/var/lib/mysql/不可用。您可以删除服务器添加到其中的所有文件。
2024-09-08T03:49:24.253496Z 0[错误][MY-010119][服务器]正在中止

安装的 指定的数据目录/var/lib/mysql/不可用。您可以删除服务器添加到其中的所有文件。docker 尝试登录数据库 清空下 /var/lib/mysql/ 文件

思路一进去 mysql 容器 清空 /var/lib/mysql/ 这个文件不就可以了吗
PS D:\coder\go-coder\go-training-camp\webook> docker exec -it  6e0f7b255786 /bin/bash 
Error response from daemon: Container 6e0f7b255786354307deb7289f6b22143523a7163861979eedf8480654c7b0c3 is restarting, wait until the container is running

连接容器失败,容器一直在重启状态,没启动的容器不能进入,这种方法思路不行

思路二 通过修改 docker-compose.yaml 文件mysql配置

网上找了很多办法 github.com 和 stackoverflow.com 搜索了很多
说需要 尝试docker run 的方式启动mysql 容器 通过 -v 的 命令将本地目录映射及数据卷挂载 /var/lib/mysql 目录

PS D:\coder\go-coder\go-training-camp\webook> docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -p 13316:3306 -v D:\k8s\mysql\mysql-data:/var/lib/mysql -d mysql:8.0
bbdd26a596444ec243b6287a5f91cda8acfc6786af35d8d6dec6beed0e3a5695

PS D:\coder\go-coder\go-training-camp\webook> docker container ls -a                                                                                             
CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS                     PORTS     NAMES
570b941fd1a5   mysql:8.0      "docker-entrypoint.s…"   7 seconds ago       Exited (1) 6 seconds ago             mysql
c606de360981   2461b2698dcd   "/usr/bin/dumb-init …"   About an hour ago   Up About an hour                     k8s_controller_ingress-nginx-controller-6c646f59bb-7lsx2_ingress-nginx_8

看日志还是失败,mysql restarting 问题一直没解决 先解决下 redis restarting 问题 缓缓太焦虑了

docker 查看redis 容器日志
PS D:\coder\go-coder\go-training-camp\webook> docker logs cba5310b0235
redis 02:26:36.11 INFO  ==> ** Starting Redis **
1:C 11 Sep 2024 02:26:36.126 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 11 Sep 2024 02:26:36.126 * Redis version=7.4.0, bits=64, commit=00000000, modified=1, pid=1, just started
1:C 11 Sep 2024 02:26:36.126 * Configuration loaded
1:M 11 Sep 2024 02:26:36.126 * monotonic clock: POSIX clock_gettime
1:M 11 Sep 2024 02:26:36.127 * Running mode=standalone, port=6379.
1:M 11 Sep 2024 02:26:36.127 # Fatal: Can't initialize Background Jobs. Error message: Operation not permitted

docker-compose.yaml redis 增加privileged: true 配置

# 我这个docker compose 有几个服务组成
services:
  mysql8:
    image: mysql:8.0
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: root
    volumes:
      #- 初始化脚本
      - ./script/mysql/:/docker-entrypoint-initdb.d/
    ports:
     # - 外部访问端口
      - 13316:3306
  redis:
    image: "bitnami/redis:latest"
    privileged: true
    restart: always
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    ports:
      - 6379:6379
果真有效 redis 启动 成功
PS D:\coder\go-coder\go-training-camp\webook> docker container ls -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED              STATUS                          PORTS                    NAMES
0b4ed9358f72   mysql:8.0              "docker-entrypoint.s…"   About a minute ago   Restarting (1) 23 seconds ago                            webook-mysql8-1
0876b0a28552   bitnami/redis:latest   "/opt/bitnami/script…"   About a minute ago   Up 58 seconds                   0.0.0.0:6379->6379/tcp   webook-redis-1
c606de360981   2461b2698dcd           "/usr/bin/dumb-init …"   2 hours ago          Up 2 hours                                               k8s_controller_ingress-nginx-controller-6c6
46f59bb-7lsx2_ingress-nginx_8bb52d7c-eb44-4c47-95af-a0a385325e59_17
mysql restart 的问题还没有解决,尝试是不是加上 privileged: true 配置就可以了
# 我这个docker compose 有几个服务组成
services:
  mysql8:
    image: mysql:8.0
    privileged: true
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: root
    volumes:
      #- 初始化脚本
      - ./script/mysql/:/docker-entrypoint-initdb.d/
    ports:
     # - 外部访问端口
      - 13316:3306
  redis:
    image: "bitnami/redis:latest"
    privileged: true
    restart: always
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    ports:
      - 6379:6379
我去mysql 也居然启动成功了
PS D:\coder\go-coder\go-training-camp\webook> docker container ls -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS         PORTS                                NAMES
6238c6d5ec5c   mysql:8.0              "docker-entrypoint.s…"   10 seconds ago   Up 8 seconds   33060/tcp, 0.0.0.0:13316->3306/tcp   webook-mysql8-1
f26220b5934d   bitnami/redis:latest   "/opt/bitnami/script…"   11 seconds ago   Up 9 seconds   0.0.0.0:6379->6379/tcp               webook-redis-1
c606de360981   2461b2698dcd           "/usr/bin/dumb-init …"   2 hours ago      Up 2 hours                                          k8s_controller_ingress-nginx-controller-6c646f59bb-7
lsx2_ingress-nginx_8bb52d7c-eb44-4c47-95af-a0a385325e59_17

了解一下 Privileged :true 配置

在使用 Docker Compose 构建和启动容器时,我们可能会遇到需要使用 Privileged 模式的情况。那么,什么是 Privileged 模式呢?Privileged 模式是 Docker 容器的一种特殊运行模式,当容器以 Privileged 模式运行时,它将拥有与宿主机几乎相同的权限。这意味着容器内的进程可以访问宿主机的所有设备,并且可以执行一些特权操作,如加载内核模块、修改网络配置等。

本人是window10 学习 docker 安全问题没那摩严重,生产环境请慎重考虑,或找其他解决方案

引用连接

Docker启动 Redis提示:Can’t initialize Background Jobg https://www.cnblogs.com/lovelyli/p/18199186
Docker Compose 中的 Privileged 模式详解:
https://cloud.baidu.com/article/3253817

docker 命令学习:
https://www.runoob.com/docker/docker-container-usage.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值