April 30, 2015

Installing MySQL (with Debconf)

Learn how to install MySQL 5.5 or 5.6. We'll cover install MySQL non-interactively (no prompts), useful for scripting installs. This will show the use of debconf-set-selections and debconf-get-selections to configure MySQL setting that might normally be prompted for during installation.

Learn how to install MySQL 5.5 or 5.6. We'll cover install MySQL non-interactively (no prompts), useful for scripting installs. This will show the use of debconf-set-selections and debconf-get-selections to configure MySQL setting that might normally be prompted for during installation.### Installing MySQL

We're using an Ubuntu server here, but this should work for Debian as well.

We can install mysql-server to get MySQL 5.5, but the MySQL 5.6 package is likely also available:

sudo apt-get install -y mysql-server-5.6

We can see more information about the MySQL package using apt-cache:

sudo apt-cache show mysql-server

Installing Without Prompts (Scriptable)

Before we go and install MySQL, however, let's see how to remote prompts, so we aren't asked a password during installation. This is useful for scripting the installation of MySQL.

In your terminal export the DEBIAN_FRONTEND variable:

export DEBIAN_FRONTEND="noninteractive"

This will turn off "frontend" (prompts) during installations. If we install MySQL with this set to noninteractive, then no root password will be set, and we'll be able to log into MySQL as root without a password.

We can take this a step forward and also set a root password for MySQL to use ahead of time:

export DEBIAN_FRONTEND="noninteractive"

sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $1"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $1"

If you're on a system using yum over apt-get, you just won't get a password prompt. You'll need to set a root password post-install.

Securing/Cleaning up the install

This should always be run after installing MySQL, especially on systems using yum, where you generally don't set a root password ahead of time.

mysql_secure_installation

Whole Thing

The whole process put together is simply this:

export DEBIAN_FRONTEND="noninteractive"

sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password rootpw"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password rootpw"

sudo apt-get install -y mysql-server-5.6

mysql_secure_installation

Deb-Conf

We can install the debconf-get-selections tool to see what possible selections a package may have.

sudo apt-get install -y debconf-utils

Then find debconf related to MySQL:

sudo debconf-get-selections | grep mysql

We'll see a few listed, including the configuration used for setting the root password. This is useful for installing any package that may have prompts or configuration asked. They can be set ahead of time!

Resources

Side note, If you're interested in a service to help you manage MySQL-optimized, backup and (eventually) replication-enabled database servers, sign up here to let me know! The idea is to allow you to better manage your MySQL servers, taking advantage of many of MySQL's more advanced options, especially around backup and recovery.

All Topics