MySQL Server provides flexible control over the destination for
log output. Log entries can be written to log files or to the
general_log and slow_log
tables in the mysql database. If logging is
enabled, either or both destinations can be selected.
The log tables are created during the installation procedure along with the other system tables. If you upgrade MySQL from a release older than 5.1.6 to MySQL 5.1.6 or higher, you must upgrade the system tables after upgrading to make sure that the log tables exist. See Section 4.4.8, “mysql_upgrade — Check Tables for MySQL Upgrade”.
Currently, logging to tables incurs significantly more server overhead than logging to files. If you enable the general log or slow query log and require highest performance, you should log to files and not to tables.
The --log-output option specifies the
destination for log output, if logging is enabled, but the
option does not in itself enable the logs. The syntax for this
option is
--log-output[=:
value,...]
If --log-output is given with a value, the
value can be a comma-separated list of one or more of the
words TABLE (log to tables),
FILE (log to files), or
NONE (do not log to tables or files).
NONE, if present, takes precedence over
any other specifiers.
If --log-output is omitted or given without
a value, the effect is the same as
--log-output=FILE. That is, the file
destination is selected.
If --log-output option also sets the value
of the global log_output system variable,
which can be modified at runtime to change the logging
destination for the server while it executes.
The
--log[=
option, if given, enables logging to the general query log for
the selected log destinations. Similarly, the
file_name]--log-slow-queries[=
option, if given, enables logging to the slow query log for the
selected destinations. If you specify either option, the server
opens the corresponding log file and writes startup messages to
it. However, logging of queries to the file does not occur
unless the file_name]FILE log destination is selected.
Examples:
To write general query log entries to the log table and the
log file, use --log-output=TABLE,FILE to
select both log destinations and the --log
option to enable the general query log.
To write general and slow query log entries only to the log
tables, use --log-output=TABLE to select
tables as the log destination and the --log
and --log-slow-queries options to enable
both logs.
To write slow query log entries only to the log file, use
--log-output=FILE to select files as the
log destination and the --log-slow-queries
option to enable the slow query log. (In this case, because
the default log destination is FILE, you
could omit the --log-output option.)
Several system variables are associated with log tables and files:
The global general_log and
slow_query_log variables indicate whether
the general query log and slow query log are enabled
(ON) or disabled
(OFF). You can set these variables at
runtime to control whether the logs are enabled.
The global general_log_file and
slow_query_log_file variables indicate
the names of the general query log and slow query log files.
You can set these variables at runtime to change the names
of the log files. (If the --log and
--log-slow-queries options were not
given, the initial variable values are the default log
filenames.)
The session sql_log_off variable can be
set to ON or OFF to
disable or enable general query logging for the current
connection.
The use of tables for log output offers the following benefits:
Log entries have a standard format. To display the current structure of the log tables, use these statements:
SHOW CREATE TABLE mysql.general_log; SHOW CREATE TABLE mysql.slow_log;
Log contents are accessible via SQL statements. This enables the use of queries that select only those log entries that satisfy specific criteria. For example, to select log contents associated with a particular client (which can be useful for identifying problematic queries from that client), it is easier to do this using a log table than a log file.
Logs are accessible remotely through any client that can connect to the server and issue queries (if the client has the appropriate log table privileges). It's not necessary to log in to the server host and directly access the filesystem.
The log table implementation has the following characteristics:
In general, the primary purpose of log tables is to provide an interface for users to observe the runtime execution of the server, not to interfere with its runtime execution.
CREATE TABLE, ALTER
TABLE, and DROP TABLE are valid
operations on a log table. For ALTER
TABLE and DROP TABLE, the log
table cannot be in use and must be disabled, as described
later.
By default, the log tables use the CSV
storage engine that writes data in comma-separated values
format. For users who have access to the
.CSV files that contain log table data,
the files are easy to import into other programs such as
spreadsheets that can process CSV input.
The log tables can be altered to use the
MyISAM storage engine. You cannot use
ALTER TABLE to alter a log table that is
in use. The log must be disabled first. No engines other
than CSV or MyISAM are
legal for the log tables.
To disable logging so that you can alter (or drop) a log
table, you can use the following strategy. The example uses
the general query log; the procedure for the slow query log
is similar but uses the slow_log table
and slow_query_log system variable.
SET @old_log_state = @@global.general_log; SET GLOBAL general_log = 'OFF'; ALTER TABLE mysql.general_log ENGINE = MyISAM; SET GLOBAL general_log = @old_log_state;
TRUNCATE TABLE is a valid operation on a
log table. It can be used to expire log entries.
RENAME TABLE is a valid operation on a
log table. You can atomically rename a log table (to perform
log rotation, for example) using the following strategy:
USE mysql; CREATE TABLE IF NOT EXISTS general_log2 LIKE general_log; RENAME TABLE general_log TO general_log_backup, general_log2 TO general_log;
LOCK TABLES cannot be used on a log
table.
INSERT, DELETE, and
UPDATE cannot be used on a log table.
These operations are allowed only internally to the server
itself.
The global read lock and the state of the global
read_only system variable have no effect
on log tables. The server can always write to the log
tables.
Entries written to the log tables are not written to the binary log and thus are not replicated to slave servers.
To flush the log tables or log files, use FLUSH
TABLES or FLUSH LOGS,
respectively.
It is not allowed to partition log tables.

User Comments
Add your own comment.