15

Logging enabled

I enabled logging using:

SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';

All executed queries was logging to mysql.general_log table. It is fine.

Attempt to clear the table

Then the table became large and I wanted to delete all records from the table. I executed:

DELETE FROM general_log

But this caused to an error that says I cannot lock log tables. So, I dropped the table after disabling logging:

SET GLOBAL general_log = 'OFF';
DROP TABLE general_log;

I hope that enabling logging again will create the table, but I couldn't enable it. When I execute this:

SET GLOBAL general_log = 'ON';

It gives this error:

Table 'mysql.general_log' doesn't exist

Questions

  1. How to create mysql.general_log again?
  2. How to clear mysql.general_log safely and in a painless way?
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
mmdemirbas
  • 9,060
  • 5
  • 45
  • 53

6 Answers6

19

Recreate:

USE mysql;

CREATE TABLE IF NOT EXISTS `general_log` (
  `event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `user_host` mediumtext NOT NULL,
  `thread_id` int(11) 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';

Clear table:

TRUNCATE table mysql.general_log;
mmdemirbas
  • 9,060
  • 5
  • 45
  • 53
pQd
  • 116
  • 4
  • 20
5

For 1.:

USE mysql;
CREATE TABLE `general_log` (
    `event_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `user_host` MEDIUMTEXT NOT NULL,
    `thread_id` INT(11) NOT NULL,
    `server_id` INT(10) UNSIGNED NOT NULL,
    `command_type` VARCHAR(64) NOT NULL,
    `argument` MEDIUMTEXT NOT NULL
)
COMMENT='General log'
COLLATE='utf8_general_ci'
ENGINE=CSV;

For 2.:

TRUNCATE mysql.general_log;
m4573r
  • 992
  • 7
  • 17
  • Thanks for your answer. Unfortunately, only one answer can be accepted altough the answers almost all the same. I accepted first answer. – mmdemirbas Sep 03 '12 at 12:52
5

This is the updated version, the 2012 answers do not work anymore:

CREATE TABLE mysql.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'
glasspill
  • 1,290
  • 4
  • 21
  • 36
5

MySQL 5.7.18

I have dropped the mysql.general_log table too, but the other answers did not help me.

Then, I noticed errors about the table structure cause it's a bit different on MySQL 5.7.18, so I have changed some field and now it works.

In case anybody needs it:

CREATE TABLE `general_log` (
  `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  `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` mediumblob
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='General log'
hey
  • 2,643
  • 7
  • 29
  • 50
DrKey
  • 3,365
  • 2
  • 29
  • 46
3

Ok, having first hand experience, this is what worked for me, if your table gets corrupted by any reason whatsoever, works with MySQL 5.6.11

use mysql;
SET GLOBAL general_log = 'OFF';

DROP TABLE general_log;

CREATE TABLE IF NOT EXISTS `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,  -- Be careful with this one.
  `server_id` int(10) unsigned NOT NULL,
  `command_type` varchar(64) NOT NULL,
  `argument` mediumtext NOT NULL
);

SET GLOBAL general_log = 'ON';

SET GLOBAL log_output = 'TABLE';

select * from mysql.general_log
order by event_time desc;
nDCasT
  • 71
  • 2
0

Recreate table in MySQL 8.0.17:

use mysql;
CREATE TABLE general_log(
event_time timestamp(6) NOT NULL ,
user_host mediumtext NOT NULL,
thread_id bigint(21) unsigned NOT NULL,
server_id int unsigned NOT NULL,
command_type varchar(64) NOT NULL,
argument mediumblob NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log';
Ben
  • 1
  • 1