Skip to content

Instantly share code, notes, and snippets.

@carltondickson
Last active August 29, 2015 14:15
Show Gist options
  • Save carltondickson/dd291d5897d770c554c7 to your computer and use it in GitHub Desktop.
Save carltondickson/dd291d5897d770c554c7 to your computer and use it in GitHub Desktop.
Log MySQL output to file without restart - FILE and TABLE approach
# http://stackoverflow.com/a/20485975/682754
# Log to file
#### Turn on and configure
SET global log_output = 'FILE';
SET global general_log_file='/tmp/mysql_general.log';
SET global general_log = 1;
#### Turn off
SET global general_log = 0;
# Log to table
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';
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';
#### Enable Query logging on the database
SET GLOBAL general_log = 1;
SET GLOBAL log_output = 'table';
#### View the log
SELECT * FROM mysql.general_log
#### Disable Query logging on the database
SET GLOBAL general_log = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment