How to setup MySQL with cloud-init?

January 29, 2017 941 views
MySQL Ubuntu 16.04

How can I do this?

From what I've been able to understand, I can't setup MySQL without a root account. Is this true?

Here's my cloud-init configuration:


    - name: dev
        - ssh-rsa AB3NzaC1yc...
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    groups: sudo
    shell: /bin/bash

    - mysql-client
    - libmysqlclient-dev
    - mysql-server

package-update: true
package_upgrade: true


    - sed -i -e '/^Port/s/^.*$/Port 22/' /etc/ssh/sshd_config
    - sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
    - sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
    - sed -i -e '$aAllowUsers dev' /etc/ssh/sshd_config

    - sudo service ssh restart

# Firewall

    # defaults
    - sudo ufw default deny incoming
    - sudo ufw default allow outgoing

    # exceptions
    - sudo ufw allow ssh
    - sudo ufw allow http
    - sudo ufw allow https

    # start on boot
    - sed -i -e '/^ENABLED/s/^.*$/ENABLED=yes/' /etc/ufw/ufw.conf

    - sudo ufw enable

Nothing very fancy,

With this config, root login is disabled and I get remote access with my dev user account. So far, so good.

However, MySQL installations fails with:

Errors were encountered while processing:
E: Sub-process /usr/bin/dpkg returned an error code (1)
Cloud-init v. 0.7.8 running 'modules:final' at Sun, 29 Jan 2017 12:09:56 +0000. Up 14.23 seconds.
2017-01-29 12:11:24,114 - util.py[WARNING]: Failed to install packages: ['mysql-client', 'libmysqlclient-dev', 'mysql-server']
2017-01-29 12:11:24,125 - cc_package_update_upgrade_install.py[WARNING]: 1 failed with exceptions, re-raising the last one
2017-01-29 12:11:24,128 - util.py[WARNING]: Running module package-update-upgrade-install (<module 'cloudinit.config.cc_package_update_upgrade_install' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_package_update_upgrade_install.py'>) failed

If I don't specify any user, and perform a typical setup through apt-get the installation runs fine.

Is it not possible to setup MySQL with cloud-init while having my root account disabled? And if not, can I somehow tell cloud-init to setup my users after my packages have been installed/updated?

Thank you!

1 Answer
jtittle1 January 29, 2017
Accepted Answer


Have you tried adding it to runcmd:?


  - sudo apt-get -y install mysql-server
  • I haven't thought of that. It worked!

    Anyone with the same issue will probably also want to take a look into installing mysql non-interactively.

    - echo "mysql-server mysql-server/root_password password pwd" | sudo debconf-set-selections
    - echo "mysql-server mysql-server/root_password_again password pwd" | sudo debconf-set-selections
    - sudo apt-get -y install mysql-server

    Thank you @jtittle, I was already considering dumping cloud-init and use a bash script.

    • @nmdias - No problem at all :-).

      I actually use the same thing you posted when setting up MySQL or MariaDB and had run in to a similar issue when using cloud-init.

      I pretty much dumped defining packages and strictly ran them from runcmd as it seemed to be less problematic, especially when it comes to more custom setups that require more than just installing a package.

Have another answer? Share your knowledge.