mysql – log queries


You can either put a log file for all queries (mysqld --log=log_file_name or log = log_file_name in your my.cnf) or write the queries in table mysql.general_log.
The use of the table can be done without restarting mysqld and the stored values could be filtered much easier.

The table can create (if not already present) as follows:
CREATE TABLE `general_log` (
`event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
`user_host` mediumtext NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`command_type` varchar(64) NOT NULL,
`argument` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log';

To activate the query logs simple run

SET global general_log = 1;
SET global log_output = 'table';

Showing the log is, for example, done with

select * from mysql.general_log

To disable logging again:

SET global general_log = 0;

The log is cleared by

TRUNCATE TABLE 'mysql.general_log;

This also works for slow-querys. The corresponding table mysql.slow_log can be applied:
CREATE TABLE `slow_log` (
`start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_host` mediumtext NOT NULL,
`query_time` time NOT NULL,
`lock_time` time NOT NULL,
`rows_sent` int(11) NOT NULL,
`rows_examined` int(11) NOT NULL,
`db` varchar(512) NOT NULL,
`last_insert_id` int(11) NOT NULL,
`insert_id` int(11) NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`sql_text` mediumtext NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';

Possibly related posts: (automatically generated)

Leave a comment

Your email address will not be published. Required fields are marked *