From 7eed88df0475c6c76386a3ea1df698c7bb4edd37 Mon Sep 17 00:00:00 2001 From: Emilien Mantel Date: Mon, 10 Aug 2015 23:45:01 +0200 Subject: [PATCH] New configuration method --- README.md | 6 +- defaults/main.yml | 67 +++++++++++++-- tasks/main.yml | 8 +- .../{90-config.cnf.j2 => 10-extra.cnf.j2} | 2 +- templates/etc/mysql/my.cnf.j2 | 84 +++++++++++++++++++ 5 files changed, 155 insertions(+), 12 deletions(-) rename templates/etc/mysql/conf.d/{90-config.cnf.j2 => 10-extra.cnf.j2} (88%) create mode 100644 templates/etc/mysql/my.cnf.j2 diff --git a/README.md b/README.md index 1cf3ce1..629f617 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,12 @@ Role Variables ### Configuration -- `mysql_cnf`: key/value hash see [defaults/main.yml](default vars file) - `mysql_root_password`: root password (should be protected with [vault](http://docs.ansible.com/playbooks_vault.html)) +If you need a feature you can't configure, you can use this list. These config will go to `/etc/mysql/conf.d/01-extra`. + +- `mysql_extra_config`: key/value hash see [defaults/main.yml](default vars file) + ### Databases - `mysql_databases`: list... @@ -69,4 +72,5 @@ Author Information - You can find many other roles in my GitHub "lab": https://github.com/HanXHX/my-ansible-playbooks - All issues, pull-request are welcome :) +- Few code come from [geerlingguy](https://github.com/geerlingguy) diff --git a/defaults/main.yml b/defaults/main.yml index 34c7ac3..6f686d2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -10,13 +10,65 @@ mysql_root_password: 'change_me_NOW' # ------------------------------------- # Configuration # ------------------------------------- -mysql_cnf: - - group_name: 'mysqld' - conf: - - key: 'innodb_file_per_table' - value: 1 - - key: 'innodb_buffer_pool_size' # 60% available RAM :) - value: '{{ (ansible_memtotal_mb * 0.6) | round | int }}M' + +# MySQL connection settings. +mysql_port: "3306" +mysql_bind_address: '127.0.0.1' +mysql_datadir: '/var/lib/mysql' +mysql_pid_file: '/var/run/mysqld/mysqld.pid' +mysql_socket: '/var/run/mysqld/mysqld.sock' + +# Slow query log settings. +mysql_slow_query_log_enabled: false +mysql_slow_query_log_file: '/var/log/mysql-slow.log' +mysql_slow_query_time: 2 + +# Memory settings (default values optimized ~512MB RAM). +mysql_key_buffer_size: '256M' +mysql_max_allowed_packet: '64M' +mysql_table_open_cache: '256' +mysql_sort_buffer_size: '1M' +mysql_read_buffer_size: '1M' +mysql_read_rnd_buffer_size: '4M' +mysql_myisam_sort_buffer_size: '64M' +mysql_thread_cache_size: '8' +mysql_query_cache_size: '16M' + +# Other settings. +mysql_wait_timeout: 28800 + +# Try number of CPU's * 2 for thread_concurrency. +mysql_thread_concurrency: 2 + +# InnoDB settings. +mysql_innodb_file_per_table: '1' +mysql_innodb_buffer_pool_size: "{{ (ansible_memtotal_mb * 0.6) | round | int }}M" +mysql_innodb_additional_mem_pool_size: '20M' +mysql_innodb_log_file_size: '64M' +mysql_innodb_log_buffer_size: '8M' +mysql_innodb_flush_log_at_trx_commit: '1' +mysql_innodb_lock_wait_timeout: 50 + +# mysqldump settings. +mysql_mysqldump_max_allowed_packet: '64M' + +# Logging settings. +mysql_log: '' +mysql_log_error: '/var/log/mysql.err' +mysql_syslog_tag: 'mysql' + +# ------------------------------------- +# Extra configuration +# ------------------------------------- +# +mysql_extra_configuration: [] + +# Example: +#mysql_extra_configuration: +# - group_name: 'mysqld' +# conf: +# - key: 'innodb_awsome_feature' +# value: 1 # ------------------------------------- # Database / Users @@ -36,4 +88,3 @@ mariadb_repository: "http://ftp.igh.cnrs.fr/pub/mariadb/repo/{{ mariadb_version # ------------------------------------- percona_version: '5.6' percona_repository: 'http://repo.percona.com/apt' - diff --git a/tasks/main.yml b/tasks/main.yml index 0d2cef7..026dfbc 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -11,8 +11,12 @@ - name: INCLUDE | Install include: install.yml -- name: TEMPLATE | Deploy daemon configuration - template: src=etc/mysql/conf.d/90-config.cnf.j2 dest=/etc/mysql/conf.d/90-config.cnf +- name: TEMPLATE | Deploy configuration + template: src=etc/mysql/my.cnf.j2 dest=/etc/mysql/my.cnf + notify: restart mysql + +- name: TEMPLATE | Deploy extra configuration + template: src=etc/mysql/conf.d/10-extra.cnf.j2 dest=/etc/mysql/conf.d/10-extra.cnf notify: restart mysql - name: TEMPLATE Create .my.cnf for root diff --git a/templates/etc/mysql/conf.d/90-config.cnf.j2 b/templates/etc/mysql/conf.d/10-extra.cnf.j2 similarity index 88% rename from templates/etc/mysql/conf.d/90-config.cnf.j2 rename to templates/etc/mysql/conf.d/10-extra.cnf.j2 index 2dd965a..b37ab9e 100644 --- a/templates/etc/mysql/conf.d/90-config.cnf.j2 +++ b/templates/etc/mysql/conf.d/10-extra.cnf.j2 @@ -2,7 +2,7 @@ # {{ ansible_managed }} # ------------------------------------------- -{% for i in mysql_cnf %} +{% for i in mysql_extra_configuration %} [{{ i.group_name }}] {% for c in i.conf %} {% if c.value is defined %} diff --git a/templates/etc/mysql/my.cnf.j2 b/templates/etc/mysql/my.cnf.j2 new file mode 100644 index 0000000..71676e4 --- /dev/null +++ b/templates/etc/mysql/my.cnf.j2 @@ -0,0 +1,84 @@ +# +# {{ ansible_managed }} +# + +[client] +port = {{ mysql_port }} +socket = {{ mysql_socket }} + +[mysqld] +port = {{ mysql_port }} +bind-address = {{ mysql_bind_address }} +datadir = {{ mysql_datadir }} +socket = {{ mysql_socket }} + +# Logging configuration. +{% if mysql_log_error == 'syslog' or mysql_log == 'syslog' %} +syslog +syslog-tag = {{ mysql_syslog_tag }} +{% else %} +{% if mysql_log %} +log = {{ mysql_log }} +{% endif %} +log-error = {{ mysql_log_error }} +{% endif %} + +{% if mysql_slow_query_log_enabled %} +# Slow query log configuration. +log_slow_queries = 1 +slow_query_log = 1 +slow_query_log_file = {{ mysql_slow_query_log_file }} +long_query_time = {{ mysql_slow_query_time }} +{% endif %} + + + +# Disabling symbolic-links is recommended to prevent assorted security risks +symbolic-links = 0 + +# User is ignored when systemd is used (fedora >= 15). +user = mysql + +# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html +;performance_schema + +# Memory settings. +key_buffer_size = {{ mysql_key_buffer_size }} +max_allowed_packet = {{ mysql_max_allowed_packet }} +table_open_cache = {{ mysql_table_open_cache }} +sort_buffer_size = {{ mysql_sort_buffer_size }} +read_buffer_size = {{ mysql_read_buffer_size }} +read_rnd_buffer_size = {{ mysql_read_rnd_buffer_size }} +myisam_sort_buffer_size = {{ mysql_myisam_sort_buffer_size }} +thread_cache_size = {{ mysql_thread_cache_size }} +query_cache_size = {{ mysql_query_cache_size }} + +# Other settings. +wait_timeout = {{ mysql_wait_timeout }} + +# Try number of CPU's * 2 for thread_concurrency. +thread_concurrency = {{ mysql_thread_concurrency }} + +# InnoDB settings. +innodb_file_per_table = {{ mysql_innodb_file_per_table }} +innodb_buffer_pool_size = {{ mysql_innodb_buffer_pool_size }} +innodb_additional_mem_pool_size = {{ mysql_innodb_additional_mem_pool_size }} +innodb_log_file_size = {{ mysql_innodb_log_file_size }} +innodb_log_buffer_size = {{ mysql_innodb_log_buffer_size }} +innodb_flush_log_at_trx_commit = {{ mysql_innodb_flush_log_at_trx_commit }} +innodb_lock_wait_timeout = {{ mysql_innodb_lock_wait_timeout }} + +[mysqldump] +quick +max_allowed_packet = {{ mysql_mysqldump_max_allowed_packet }} + +[mysqld_safe] +pid-file = {{ mysql_pid_file }} + +# +# * IMPORTANT: Additional settings that can override those from this file! +# The files must end with '.cnf', otherwise they'll be ignored. +# +!includedir /etc/mysql/conf.d/ + +# vim: set ft=dosini :