MySQL Puppet Module / Manifests + Types & Providers
MySQL Puppet Module / Manifests + Types & Providers
The mysql module installs, configures, and manages the MySQL service.
This module manages both the installation and configuration of MySQL, as well as extending Puppet to allow management of MySQL resources, such as databases, users, and grants.
To install a server with the default options:
include mysql::server.
To customize options, such as the root password or /etc/my.cnf settings, you must also pass in an override hash:
class { 'mysql::server':
root_password => 'strongpassword',
remove_default_accounts => true,
restart => true,
override_options => $override_options,
}Nota bene: Configuration changes will only be applied to the running MySQL server if you pass true as restart to mysql::server.
See Customize Server Options below for examples of the hash structure for $override_options.
All interaction for the server is done via mysql::server. To install the client, use mysql::client. To install bindings, use mysql::bindings.
To define server options, structure a hash structure of overrides in mysql::server. This hash resembles a hash in the my.cnf file:
$override_options = {
'section' => {
'item' => 'thing',
},
}For options that you would traditionally represent in this format:
[section]
thing = XEntries can be created as thing => true, thing => value, or thing => "" in the hash. Alternatively, you can pass an array as thing => ['value', 'value2'] or list each thing => value separately on individual lines.
You can pass a variable in the hash without setting a value for it; the variable would then use MySQL's default settings. To exclude an option from the my.cnf file --- for example, when using override_options to revert to a default value --- pass thing => undef.
If an option needs multiple instances, pass an array. For example,
$override_options = {
'mysqld' => {
'replicate-do-db' => ['base1', 'base2'],
},
}produces
[mysqld]
replicate-do-db = base1
replicate-do-db = base2To implement version specific parameters, specify the version, such as [mysqld-5.5]. This allows one config for different versions of MySQL.
If you don’t want to use the default configuration, you can also supply your options to the $options parameter instead of $override_options.
Please note that $options and $override_options are mutually exclusive, you can only use one of them.
By default, the puppet won't reload/restart mysqld when you change an existing
configuration. If you want to do that, you can set
mysql::server::reload_on_config_change to true.
To create a database with a user and some assigned privileges:
mysql::db { 'mydb':
user => 'myuser',
password => 'mypass',
host => 'localhost',
grant => ['SELECT', 'UPDATE'],
}To use a different resource name with exported resources:
@@mysql::db { "mydb_${fqdn}":
user => 'myuser',
password => 'mypass',
dbname => 'mydb',
host => ${fqdn},
grant => ['SELECT', 'UPDATE'],
tag => $domain,
}Then you can collect it on the remote DB server:
Mysql::Db >If you set the sql parameter to a file when creating a database, the file is imported into the new database.
For large sql files, increase the import_timeout parameter, which defaults to 300 seconds.
If you have installed the mysql client in a non standard bin/sbin path you can set this with mysql_exec_path .
mysql::db { 'mydb':
user => 'myuser',
password => 'mypass',
host => 'localhost',
grant => ['SELECT', 'UPDATE'],
sql => ['/path/to/sqlfile.gz'],
import_cat_cmd => 'zcat',
import_timeout => 900,
mysql_exec_path => '/opt/rh/rh-myql57/root/bin',
}To add custom MySQL configuration, place additional files into includedir. This allows you to override settings or add additional ones, which is helpful if you don't use override_options in mysql::server. The includedir location is by default set to /etc/mysql/conf.d.
If you want the password managed by puppet for 127.0.0.1 and ::1 as an end user you would need to explicitly manage them with additional manifest entries. For example:
mysql_user { '[[email protected]]':
ensure => present,
password_hash => mysql::password($mysql::server::root_password),
}
mysql_user { 'root@::1':
ensure => present,
password_hash => mysql::password($mysql::server::root_password),
}Note: This module is not designed to carry out additional DNS and aliasing.
To instantiate databases and users on an existing MySQL server, you need a .my.cnf file in root's home directory. This file must specify the remote server address and credentials. For example:
[client]
user=root
host=localhost
password=secretThis module uses the mysqld_version fact to discover the server version being used. By default, this is set to the output of mysqld -V. If you're working with a remote MySQL server, you may need to set a custom fact for mysqld_version to ensure correct behaviour.
When working with a remote server, do not use the mysql::server class in your Puppet manifests.
In addition to passing passwords as plain text, you can input them as hashes. For example:
mysql::db { 'mydb':
user => 'myuser',
password => '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4',
host => 'localhost',
grant => ['SELECT', 'UPDATE'],
}If required, the password can also be an empty string to allow connections without an password.
This feature works only for the MySQL Community Edition >= 5.6.6.
A login path is a set of options (host, user, password, port and socket) that specify which MySQL server to connect to and which account to authenticate as. The authentication credentials and the other options are stored in an encrypted login file named .mylogin.cnf typically under the users home directory.
More information about MySQL login paths: https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html.
Some example for login paths:
mysql_login_path { 'client':
owner => root,
host => 'localhost',
user => 'root',
password => Sensitive('secure'),
socket => '/var/run/mysqld/mysqld.sock',
ensure => present,
}
mysql_login_path { 'remote_db':
owner => root,
host => '10.0.0.1',
user => 'network',
password => Sensitive('secure'),
port => 3306,
ensure => present,
}See examples/mysql_login_path.pp for further examples.
This example shows how to do a minimal installation of a Percona server on a CentOS system. This sets up the Percona server, client, and bindings (including Perl and Python bindings). You can customize this usage and update the version as needed.
This usage has been tested on Puppet 4.4, 5.5 and 6.3.0 / CentOS 7 / Percona Server 5.7.
Note: The installation of the yum repository is not part of this package and is here only to show a full example of how you can install.
…In this example, we'll use the latest stable (currently 10.3) from the official MariaDB repository, not the one from the distro repository. You could instead use the package from the Ubuntu repository. Make sure you use the repository corresponding to the version you want.
Note: sfo1.mirrors.digitalocean.com is one of many mirrors available. You can use any official mirror.
include apt
apt::source { 'mariadb':
location => 'http://sfo1.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu',
release => $::facts['os']['codename'],
repos => 'main',
key => {
id => '177F4010FE56CA3336300305F1656F24C74CD1D8',
server => 'hkp://keyserver.ubuntu.com:80',
},
include => {
src => false,
deb => true,
},
}This example shows MariaDB server installation on Ubuntu Xenial. Adjust the version and the parameters of my.cnf as needed. All parameters of the my.cnf can be defined using the override_options parameter.
The folders /var/log/mysql and /var/run/mysqld are created automatically, but if you are using other custom folders, they should exist as prerequisites for this code.
All the values set here are an example of a working minimal configuration.
Specify the version of the package you want with the package_ensure parameter.
…This example shows how to install the MariaDB client and all of the bindings at once. You can do this installation separately from the server installation.
Specify the version of the package you want with the package_ensure parameter.
class { 'mysql::client':
package_name => 'mariadb-client',
package_ensure => '1:10.3.21+maria~xenial',
bindings_enable => true,
}
# Dependency management. Only use that part if you are installing the repository as shown in the Preliminary step of this example.
Apt::Source['mariadb'] ~>
Class['apt::update'] ->
Class['mysql::client']You can install MySQL Community Server on CentOS using the mysql module and Hiera. This example was tested with the following versions:
In Puppet:
include mysql::server
create_resources(yumrepo, hiera('yumrepo', {}))
Yumrepo['repo.mysql.com'] -> Anchor['mysql::server::start']
Yumrepo['repo.mysql.com'] -> Package['mysql_client']
create_resources(mysql::db, hiera('mysql::server::db', {}))In Hiera:
…Plugins can be installed by using the mysql_plugin defined type. See examples/mysql_plugin.pp for futher examples.
This example shows how to configure MySQL backups with Percona XtraBackup. This sets up a weekly cronjob to perform a full backup and additional daily cronjobs for incremental backups. Each backup will create a new directory. A cleanup job will automatically remove backups that are older than 15 days.
…If the daily or weekly backup was successful, then the empty file /tmp/mysqlbackup_success is created, which makes it easy to monitor the status of the database backup.
After two weeks the backup directory should look similar to the example below.
/tmp/backups/2019-11-10_full
/tmp/backups/2019-11-11_23-15-01
/tmp/backups/2019-11-13_23-15-01No open issues yet, or sync has not completed.