Question

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:

#cloud-config

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

packages:
    - mysql-client
    - libmysqlclient-dev
    - mysql-server

package-update: true
package_upgrade: true

runcmd:

#
# SSH
#
    - 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:
 /var/cache/apt/archives/mysql-server-5.7_5.7.17-0ubuntu0.16.04.1_amd64.deb
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!


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.

@nmdias

Have you tried adding it to runcmd:?

i.e.

runcmd:
  - sudo apt-get -y install mysql-server

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.