How to setup MySQL with cloud-init?

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 -[WARNING]: Failed to install packages: ['mysql-client', 'libmysqlclient-dev', 'mysql-server']
2017-01-29 12:11:24,125 -[WARNING]: 1 failed with exceptions, re-raising the last one
2017-01-29 12:11:24,128 -[WARNING]: Running module package-update-upgrade-install (<module 'cloudinit.config.cc_package_update_upgrade_install' from '/usr/lib/python3/dist-packages/cloudinit/config/'>) 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!

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.


Have you tried adding it to runcmd:?


  - sudo apt-get -y install mysql-server

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel