Skip to content

Instantly share code, notes, and snippets.

@ovaistariq
Created June 5, 2016 19:23
Show Gist options
  • Save ovaistariq/0165eb6991a15b6846b13ac62e5da57c to your computer and use it in GitHub Desktop.
Save ovaistariq/0165eb6991a15b6846b13ac62e5da57c to your computer and use it in GitHub Desktop.

For 5.1 and above when slow_query_log_file is a dynamic variable

  • file: start_queries_capture.sh
#!/bin/bash

mysql -e "set global slow_query_log_file='/var/log/mysql/slow_query_capture.log'"
mysql -e "set global long_query_time=0"
mysqladmin flush-logs
  • file: stop_queries_capture.sh
#!/bin/bash

mysql -e "set global long_query_time=10"
mysql -e "set global slow_query_log_file='/var/log/mysql/slow_query.log'"
mysqladmin flush-logs
mv /var/log/mysql/slow_query_capture.log /root/percona/

For 5.0 and below when slow query log filename cannot be changed

  • file: start_queries_capture.sh
#!/bin/bash

mv /data/mysql/log-slow-mysql /data/mysql/log-slow-mysql.bak
mysqladmin flush-logs
mysql -e "set global long_query_time=0"
  • file: stop_queries_capture.sh
#!/bin/bash

mysql -e "set global long_query_time=2"
mv /data/mysql/log-slow-mysql /root/percona/
mv /data/mysql/log-slow-mysql.bak /data/mysql/log-slow-mysql
mysqladmin flush-logs

create required directory and create the files as above

mkdir /root/percona
  • schedule the job such that query capture starts at 9am and ends at 10am
at -f /root/percona/start_queries_capture.sh -v 09:00
at -f /root/percona/stop_queries_capture.sh -v 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment