Wednesday, 31 October 2018

Log all queries in mysql

Is it possible for me to turn on audit logging on my mysql database?
I basically want to monitor all queries for an hour, and dump the log to a file.

 Answers


Start mysql with the --log option:
mysqld --log=log_file_name
or place the following in your my.cnf file:
log = log_file_name
Either one will log all queries to log_file_name.
You can also log only slow queries using the --log-slow-queries option instead of --log. By default, queries that take 10 seconds or longer are considered slow, you can change this by setting long_query_time to the number of seconds a query must take to execute before being logged.



Besides what i came across here, running the following was the simplest way to dump queries to a log file without restarting
SET global log_output = 'FILE';
SET global general_log_file='/Applications/MAMP/logs/mysql_general.log';
SET global general_log = 1;
can be turned off with
SET global general_log = 0;



Enable the log for table
mysql> SET GLOBAL general_log = 'ON';
mysql> SET global log_output = 'table';
View log by select query
select * from mysql.general_log



For the record, general_log and slow_log were introduced in 5.1.6:
5.2.1. Selecting General Query and Slow Query Log Output Destinations
As of MySQL 5.1.6, MySQL Server provides flexible control over the destination of output to the general query log and the slow query log, if those logs are enabled. Possible destinations for log entries are log files or the the general_log and slow_log tables in the mysql database



In case using AWS RDS MYSQL, step by step guide here.
When set as 'file' output you can view the log directly from AWS RDS "Log" Console.

0 comments:

Post a Comment