Isaac Bosca
Isaac Bosca

Reputation: 1648

How to ensure mysql service is running after installation using Ansible over an Ubuntu Docker container?

I'm currently learning Ansible. I created a project with a docker-compose file where I have two docker containers running Ubuntu.

I'm trying to install the needed packages to run an application I developed. For this, I need Nginx, MySQL, Nodejs, and PHP among others.

This is a portion of my current playbook, where I have multiple plays for each package installation:

---

- 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

As you can see, I duplicated the Restart mysql, having a handler and a task. I was using the task for debugging purposes. This is the output:

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

Quite promising, isn't it?

But once the playbook finishes, and I access using ssh to the target container I realize the MySQL service is not running:

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

At this point, I can start the service manually:


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

Any clue on what's going on here?

Thanks a lot!

EDIT

Adding the information found in the logs:

root@target:/var/log/mysql# cat error.log 
2023-01-18T17:55:40.313446Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.31-0ubuntu0.22.04.1) initializing of server in progress as process 12776
2023-01-18T17:55:40.320475Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-18T17:55:41.059973Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-18T17:55:43.854944Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2023-01-18T17:55:45.667927Z 6 [System] [MY-013172] [Server] Received SHUTDOWN from user boot. Shutting down mysqld (Version: 8.0.31-0ubuntu0.22.04.1).
2023-01-18T17:55:49.500456Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31-0ubuntu0.22.04.1) starting as process 12830
2023-01-18T17:55:49.521912Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-18T17:55:49.698343Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-18T17:55:49.897830Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-01-18T17:55:49.897857Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-01-18T17:55:49.904066Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/tmp' in the path is accessible to all OS users. Consider choosing a different directory.
2023-01-18T17:55:49.919253Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2023-01-18T17:55:49.919258Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.31-0ubuntu0.22.04.1'  socket: '/tmp/tmp.M6y3okq6vU/mysqld.sock'  port: 0  (Ubuntu).
2023-01-18T17:55:49.947857Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.31-0ubuntu0.22.04.1).
2023-01-18T17:55:51.499791Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31-0ubuntu0.22.04.1)  (Ubuntu).
2023-01-18T17:55:52.294286Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31-0ubuntu0.22.04.1) starting as process 12903
2023-01-18T17:55:52.298944Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-18T17:55:52.466479Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-18T17:55:52.599430Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-01-18T17:55:52.599477Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-01-18T17:55:52.605573Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/tmp' in the path is accessible to all OS users. Consider choosing a different directory.
2023-01-18T17:55:52.628496Z 7 [System] [MY-013172] [Server] Received SHUTDOWN from user boot. Shutting down mysqld (Version: 8.0.31-0ubuntu0.22.04.1).
2023-01-18T17:55:52.631706Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '127.0.0.1' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-01-18T17:55:54.186079Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31-0ubuntu0.22.04.1)  (Ubuntu).

Upvotes: 0

Views: 729

Answers (1)

Kevin C
Kevin C

Reputation: 5760

I can't reproduce. When I take your code, mysql on my vm runs just fine.
To answer your question:

How to ensure mysql service is running after installation using Ansible over an Ubuntu Docker container?

Simply use:

- name: ensure mysql is running
  service:
    name: mysql
    state: started

If the system cannot start mysql, then that task will fail.
If the system starts mysql, that's fine.

But keep in mind, you're trying to re-invent the wheel here. If you just want to gain experience and fiddle w Ansible, that's great.
But if you're serious about installing nginx/mysql on production servers and whatnot, please use Ansible roles for each application and test them properly.

P.s, I feel like you're beginning w Ansible. I've simplified your code a bit, take a look.

Upvotes: 2

Related Questions