MySQL Automatic Install with Salt – Preseed Database Root Password

MySQL is probably the most popular multi-user database in the world*. It’s part of the most popular web development stack, LAMP (Linux Apache MySQL PHP).
This article shows a Salt state to automatically install MySQL on Ubuntu.
As the package installation strangely asks for database root password interactively, we must answer the question before installing mysql-server. We use preseed to answer the questions beforehand.
Following this article requires Salt installation and understanding states.


In this example, we’ll put the password directly on the state file in /srv/salt/ where any slave (“minion”) could read it. This only makes sense when applying the state locally (without master). When you promote this state to master with multiple slaves, you should put the password in a pillar.
You can generate random passwords with ‘pwgen 20 1’.
MySQL database users are not related to Linux users. Thus, the MySQL database root user is not related to system root user in any way.
Preseed only affects package installation, so you must preseed before installing mysql-server for the first time. Don’t lose your MySQL password, fixing it is currently annoying. Preseeding can’t change existing MySQL password.

Create MySQL State

On the master,

$ sudo mkdir -p /srv/salt/mysql
$ sudoedit /srv/salt/mysql/init.sls

Contents of the state:

debconf-utils: pkg.installed
mysqlroot:
  debconf.set:
    - data: # four space indent below.
        'mysql-server/root_password': {'type': 'password', 'value': 'xeBee5roijeiph7Xeich'}
        'mysql-server/root_password_again': {'type': 'password', 'value': 'xeBee5roijeiph7Xeich'}
mysql-server: pkg.installed
mysql-client: pkg.installed
# TODO: All slaves see password in /srv/salt/, use pillars for production

Consider the state (mysql/init.sls) above. Debconf.set function requires a four space indent for values of data. If you mistakenly only indent with two spaces as is required in all the other rows, you’ll get an error message when running ‘salt-call –local’. The Python traceback ends with “AttributeError: ‘NoneType’ object has no attribute ‘iteritems'”
Preseeding requires debconf-utils. If you forget to install this package, you’ll get an error “Reason: ‘debconf.set’ is not available.”. As debconf-utils is sometimes available by default, a state that doesn’t install debconf-utils works only sometimes.

Apply the State

Run your state locally

$ sudo salt-call --local state.apply mysql

Test & Enjoy the Result

$ mysql -u root -p
Enter password:
mysql>

You’re in! To make sure you have database root, try something only the db root can do

mysql> CREATE DATABASE terokarvinencom;
Query OK, 1 row affected (0.01 sec)

If you got OK, you have now successfully and automatically installed mysql server.

What next?

Put your password into salt pillar, so you can use this state on a typical master-slave setup.
Have a look at other interesting debconf settings to preseed

$ sudo debconf-get-selections

See also

Karvinen 2015: Preseed MySQL Server Password with Salt Stack
[Official] Salt Documentation: salt.states.debconfmod
Salt official documentation: Salt Getting Started Guide: Pillar

Adminstrivia

This article is has been updated many times.
These commands have been tested with Salt 2017.7.4, Ubuntu 16.04.1 LTS xenial amd64, Vagrant, Virtualbox and mysql-server 5.7.21-0ubuntu0.16.04.1.
* MySql is the most popular multi-user database based on my non-scientific guess, including MariaDB installations as MySQL installs and considering SQLite a single user database.

Posted in Uncategorized | Tagged , , , , , , , , , | Comments Off on MySQL Automatic Install with Salt – Preseed Database Root Password

Comments are closed.