15

启用日志记录

我启用了日志记录

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

所有执行的查询都记录到mysql.general_log表中。没事。

尝试清理表

然后表变大了,我想从表中删除所有记录。我执行了:

DELETE FROM general_log

但这导致了一个错误,说我无法锁定日志表。因此,我在禁用日志记录后删除了表:

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

我希望再次启用日志记录将创建表,但我无法启用它。当我执行这个:

SET GLOBAL general_log = 'ON';

它给出了这个错误:

表 'mysql.general_log' 不存在

问题

  1. 如何重新创建mysql.general_log
  2. 如何mysql.general_log安全无痛地清理?
4

6 回答 6

19

重新创建:

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';

清空表:

TRUNCATE table mysql.general_log;
于 2012-09-03T11:38:45.870 回答
5

对于 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;

对于 2.:

TRUNCATE mysql.general_log;
于 2012-09-03T11:39:54.130 回答
5

这是更新版本,2012 年的答案不再起作用:

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'
于 2015-01-28T12:39:48.787 回答
5

MySQL 5.7.18

我也丢了mysql.general_log桌子,但其他答案对我没有帮助。

然后,我注意到有关表结构的错误,因为它有点不同MySQL 5.7.18,所以我更改了一些字段,现在它可以工作了。

如果有人需要它:

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'
于 2017-06-26T11:34:58.507 回答
3

好的,有第一手经验,这对我有用,如果您的表因任何原因损坏,可与 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;
于 2016-02-12T20:52:59.927 回答
0

在 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';
于 2020-11-14T09:04:29.833 回答