You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何确保在Ubuntu Docker容器中通过Ansible安装MySQL后服务正常运行?

问题:Ansible安装MySQL后服务自动停止

环境与场景

  • controller容器:部署Ansible客户端及待执行playbook
  • target容器:Ubuntu系统,作为playbook执行目标,需安装Nginx、MySQL等依赖

问题现象

执行包含MySQL安装的playbook后,调试输出显示服务启动成功,但登录target容器发现MySQL已停止,手动启动可正常运行。

相关Playbook片段

---

- name: nginx
  gather_facts: true
  hosts: target
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
      notify: Restart nginx
  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted

- name: mysql
  gather_facts: true
  hosts: target
  tasks:
    - name: Install mysql
      apt:
        name: mysql-server
        state: present
      notify: Restart mysql

    - name: Restart mysql
      become: true
      service:
        name: mysql
        state: restarted
      register: output

    - name: Debug
      debug:
        msg: "{{ output }}"

  handlers:
    - name: Restart mysql
      service:
        name: mysql
        state: restarted

调试输出

ok: [target] => {
    "msg": {
        "changed": true,
        "failed": false,
        "name": "mysql",
        "state": "started"
    }
}

容器内状态检查

ubuntu@target:~$ sudo service mysql status
[sudo] password for ubuntu: 
 * MySQL is stopped.

手动启动后正常:

ubuntu@target:~$ sudo service mysql start
[sudo] password for ubuntu: 
 * Starting MySQL database server mysqld 
su: warning: cannot change directory to /nonexistent: No such file or directory
[ OK ]  

ubuntu@target:~$ sudo service mysql status
 * /usr/bin/mysqladmin  Ver 8.0.31-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version          8.0.31-0ubuntu0.22.04.1
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 10 sec

Threads: 2  Questions: 8  Slow queries: 0  Opens: 119  Flush tables: 3  Open tables: 38  Queries per second avg: 0.800

MySQL错误日志(翻译后)

root@target:/var/log/mysql# cat error.log 
2023-01-18T17:55:40.313446Z 0 [系统] [MY-013169] [服务器] /usr/sbin/mysqld (mysqld 8.0.31-0ubuntu0.22.04.1) 正在初始化服务器,进程ID 12776
2023-01-18T17:55:40.320475Z 1 [系统] [MY-013576] [InnoDB] InnoDB初始化已开始。
2023-01-18T17:55:41.059973Z 1 [系统] [MY-013577] [InnoDB] InnoDB初始化已完成。
2023-01-18T17:55:43.854944Z 6 [警告] [MY-010453] [服务器] root@localhost 创建时密码为空!请考虑关闭 --initialize-insecure 选项。
2023-01-18T17:55:45.667927Z 6 [系统] [MY-013172] [服务器] 收到来自用户 boot 的 SHUTDOWN 指令。正在关闭 mysqld (版本: 8.0.31-0ubuntu0.22.04.1)。
2023-01-18T17:55:49.500456Z 0 [系统] [MY-010116] [服务器] /usr/sbin/mysqld (mysqld 8.0.31-0ubuntu0.22.04.1) 启动为进程 12830
2023-01-18T17:55:49.521912Z 1 [系统] [MY-013576] [InnoDB] InnoDB初始化已开始。
2023-01-18T17:55:49.698343Z 1 [系统] [MY-013577] [InnoDB] InnoDB初始化已完成。
2023-01-18T17:55:49.897830Z 0 [警告] [MY-010068] [服务器] CA证书 ca.pem 是自签名的。
2023-01-18T17:55:49.897857Z 0 [系统] [MY-013602] [服务器] 通道 mysql_main 已配置为支持TLS。该通道现在支持加密连接。
2023-01-18T17:55:49.904066Z 0 [警告] [MY-011810] [服务器] --pid-file 的配置不安全:路径中的位置 '/tmp' 对所有操作系统用户都可访问。请选择其他目录。
2023-01-18T17:55:49.919253Z 0 [系统] [MY-011323] [服务器] X插件已准备好接受连接。套接字: /var/run/mysqld/mysqlx.sock
2023-01-18T17:55:49.919258Z 0 [系统] [MY-010931] [服务器] /usr/sbin/mysqld: 已准备好接受连接。版本: '8.0.31-0ubuntu0.22.04.1'  套接字: '/tmp/tmp.M6y3okq6vU/mysqld.sock'  端口: 0  (Ubuntu)。
2023-01-18T17:55:49.947857Z 0 [系统] [MY-013172] [服务器] 收到来自用户 <通过用户信号> 的 SHUTDOWN 指令。正在关闭 mysqld (版本: 8.0.31-0ubuntu0.22.04.1)。
2023-01-18T17:55:51.499791Z 0 [系统] [MY-010910] [服务器] /usr/sbin/mysqld: 关闭完成 (mysqld 8.0.31-0ubuntu0.22.04.1)  (Ubuntu)。
2023-01-18T17:55:52.294286Z 0 [系统] [MY-010116] [服务器] /usr/sbin/mysqld (mysqld 8.0.31-0ubuntu0.22.04.1) 启动为进程 12903
2023-01-18T17:55:52.298944Z 1 [系统] [MY-013576] [InnoDB] InnoDB初始化已开始。
2023-01-18T17:55:52.466479Z 1 [系统] [MY-013577] [InnoDB] InnoDB初始化已完成。
2023-01-18T17:55:52.599430Z 0 [警告] [MY-010068] [服务器] CA证书 ca.pem 是自签名的。
2023-01-18T17:55:52.599477Z 0 [系统] [MY-013602] [服务器] 通道 mysql_main 已配置为支持TLS。该通道现在支持加密连接。
2023-01-18T17:55:52.605573Z 0 [警告] [MY-011810] [服务器] --pid-file 的配置不安全:路径中的位置 '/tmp' 对所有操作系统用户都可访问。请选择其他目录。
2023-01-18T17:55:52.628496Z 7 [系统] [MY-013172] [服务器] 收到来自用户 boot 的 SHUTDOWN 指令。正在关闭 mysqld (版本: 8.0.31-0ubuntu0.22.04.1)。
2023-01-18T17:55:52.631706Z 0 [系统] [MY-011323] [服务器] X插件已准备好接受连接。绑定地址: '127.0.0.1' 端口: 33060, 套接字: /var/run/mysqld/mysqlx.sock
2023-01-18T17:55:54.186079Z 0 [系统] [MY-010910] [服务器] /usr/sbin/mysqld: 关闭完成 (mysqld 8.0.31-0ubuntu0.22.04.1)  (Ubuntu)。

原因分析

从错误日志和现象可以定位核心问题:

  1. MySQL运行用户家目录缺失:手动启动时的警告su: warning: cannot change directory to /nonexistent说明,默认运行MySQL的mysql用户家目录未创建,导致系统初始化脚本在安装完成后自动发送停止指令。
  2. 重复启停触发终止逻辑:playbook中同时用handler和独立任务触发MySQL重启,多次启停触发了系统的安全/初始化逻辑,最终导致服务被终止。

解决方案

1. 修复MySQL用户家目录

在playbook中添加任务,创建并配置mysql用户的家目录:

- name: Create mysql user home directory
  become: true
  file:
    path: /var/lib/mysql
    state: directory
    owner: mysql
    group: mysql
    mode: '0750'

2. 简化Playbook,避免重复启停

移除重复的Restart mysql任务,仅保留handler处理安装后的重启,并设置开机自启:

- name: mysql
  gather_facts: true
  hosts: target
  tasks:
    - name: Install mysql
      apt:
        name: mysql-server
        state: present
      notify: Restart mysql

    - name: Create mysql user home directory
      become: true
      file:
        path: /var/lib/mysql
        state: directory
        owner: mysql
        group: mysql
        mode: '0750'

  handlers:
    - name: Restart mysql
      become: true
      service:
        name: mysql
        state: restarted
        enabled: true  # 设置开机自启,确保容器重启后服务自动运行

3. 验证服务状态(可选)

在playbook末尾添加任务,确保MySQL服务稳定运行:

- name: Check mysql service status
  become: true
  service:
    name: mysql
    state: started
  register: mysql_status
  until: mysql_status.state == "started"
  retries: 3
  delay: 5

内容的提问来源于stack exchange,提问作者Isaac Bosca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 13:50:26