The Puppet Labs Issue Tracker has Moved: https://tickets.puppetlabs.com

Install MySQL server and set root password

You will need to modify this recipe for the package names that you want installed, and the password you want to set.

The recipe

class mysql-server {
  $password = "insert_password_here"
  package { "MySQL-client": ensure => installed }
  package { "MySQL-server": ensure => installed }
  package { "MySQL-shared": ensure => installed }

  exec { "Set MySQL server root password":
    subscribe => [ Package["MySQL-server"], Package["MySQL-client"], Package["MySQL-shared"] ],
    refreshonly => true,
    unless => "mysqladmin -uroot -p$password status",
    path => "/bin:/usr/bin",
    command => "mysqladmin -uroot password $password",
  }
}

Detailed explanation

We are first of all setting a variable that contains the password:

  $password = "insert_password_here"

Then specifying that the various packages should be installed:

  package { "MySQL-client": ensure => installed }
  package { "MySQL-server": ensure => installed }
  package { "MySQL-shared": ensure => installed }

The next task is to set the MySQL server’s root password following installation. To do this we set up an Exec which subscribes to the packages, so if one of the packages is installed, the exec will be triggered:

  exec { "Set MySQL server root password":
    subscribe => [ Package["MySQL-server"], Package["MySQL-client"], Package["MySQL-shared"] ],

We want this to run only once after the packages are installed, so we set ‘refreshonly’:

      refreshonly => true,

And it should only run if the password is not already set. You can test this by running ‘mysqladmin -uroot -pPASSWORD status’, and if this succeeds, we know the password is set to PASSWORD.

    unless => "mysqladmin -uroot -p$password status",

Finally the actual command that should be run by ‘exec’:

    path => "/bin:/usr/bin",
    command => "mysqladmin -uroot password $password",

We now have a class ‘mysql-server’ which can be included by any node which you want to run a MySQL server on. For example:

node pigeon {
  include mysql-server
}