From 2d178fe83a66e326465f56f14ef840d7b1577d7b Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Fri, 24 Apr 2015 12:20:27 -0500 Subject: [PATCH] Fixes #17: Allow configuration of mysql slow query log. --- README.md | 9 ++++++++- defaults/main.yml | 10 ++++++++-- tasks/configure.yml | 13 +++++++++++++ templates/my.cnf.j2 | 29 ++++++++++++++++++++--------- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3d942c8..dfa8814 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,17 @@ The MySQL users and their privileges. A user has the values `name`, `host` (defa Default MySQL connection configuration. + mysql_log: "" mysql_log_error: /var/log/mysqld.log mysql_syslog_tag: mysqld -MySQL logging configuration. Setting `mysql_log_error` to `syslog` will make MySQL log to syslog using the `mysql_syslog_tag`. +MySQL logging configuration. Setting `mysql_log` (the general query log) or `mysql_log_error` to `syslog` will make MySQL log to syslog using the `mysql_syslog_tag`. + + mysql_slow_query_log_enabled: no + mysql_slow_query_log_file: /var/log/mysql-slow.log + mysql_slow_query_time: 2 + +Slow query log settings. Note that the log file will be created by this role, but if you're running on a server with SELinux or AppArmor, you may need to add this path to the allowed paths for MySQL, or disable the mysql profile. For example, on Debian/Ubuntu, you can run `sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/usr.sbin.mysqld && sudo service apparmor restart`. mysql_key_buffer_size: "256M" mysql_max_allowed_packet: "64M" diff --git a/defaults/main.yml b/defaults/main.yml index d373ff6..cf31060 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -24,6 +24,11 @@ mysql_port: "3306" mysql_bind_address: '0.0.0.0' mysql_datadir: /var/lib/mysql +# Slow query log settings. +mysql_slow_query_log_enabled: no +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" @@ -56,8 +61,9 @@ mysql_innodb_lock_wait_timeout: 50 mysql_mysqldump_max_allowed_packet: "64M" # Logging settings. -mysql_log_error: /var/log/mysqld.log -mysql_syslog_tag: mysqld +mysql_log: "" +mysql_log_error: /var/log/mysql.err +mysql_syslog_tag: mysql # Databases. mysql_databases: [] diff --git a/tasks/configure.yml b/tasks/configure.yml index f2bb5a6..01206a5 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -9,5 +9,18 @@ force: "{{ overwrite_global_mycnf }}" notify: restart mysql +- name: Create slow query log file (if configured). + shell: "touch {{ mysql_slow_query_log_file }} creates={{ mysql_slow_query_log_file }}" + when: mysql_slow_query_log_enabled + +- name: Set ownership on slow query log file (if configured). + file: + path: "{{ mysql_slow_query_log_file }}" + state: file + owner: mysql + group: mysql + mode: 0644 + when: mysql_slow_query_log_enabled + - name: Ensure MySQL is started and enabled on boot. service: "name={{ mysql_daemon }} state=started enabled={{ mysql_enabled_on_startup }}" diff --git a/templates/my.cnf.j2 b/templates/my.cnf.j2 index 4adb695..476e6e5 100644 --- a/templates/my.cnf.j2 +++ b/templates/my.cnf.j2 @@ -9,6 +9,25 @@ 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 %} + {% if mysql_replication_master %} # Replication server-id = {{ mysql_server_id }} @@ -38,7 +57,7 @@ relay-log-index = relay-bin.index # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links = 0 -# User is ignored when systemd is used (fedora <= 15). +# User is ignored when systemd is used (fedora >= 15). user = mysql # http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html @@ -57,7 +76,6 @@ query_cache_size = {{ mysql_query_cache_size }} # Other settings. wait_timeout = {{ mysql_wait_timeout }} -max_allowed_packet = {{ mysql_max_allowed_packet }} # Try number of CPU's * 2 for thread_concurrency. thread_concurrency = {{ mysql_thread_concurrency }} @@ -76,11 +94,4 @@ quick max_allowed_packet = {{ mysql_mysqldump_max_allowed_packet }} [mysqld_safe] -{% if mysql_log_error == 'syslog' %} -syslog -syslog-tag = {{ mysql_syslog_tag }} -{% else %} -skip-syslog -log-error = {{ mysql_log_error }} -{% endif %} pid-file = /var/run/mysqld/mysqld.pid