• MySQL知识库 :: replication
  • How is logging enabled?

  • Enabling logging is very simple. To enable the general query log, just add the --log option at the command-line when starting the server daemon (mysqld). Or, add it to the configuration file for MySQL(i.e., my.cnf or my.ini, depending on your system), but without the leading dashes like so:

    [mysqld]
    log = /var/logs/mysql/queries.log
    ...
    

    After this option is added to the mysqld group, the daemon will need to be restarted for it to take effect. When MySQL starts up again, a simple text file will be created in the directory given. It will contain all of the SQL queries received by the server. They will be in the order received, not necessarily in the order that they were executed. This means that SQL statements that use the LOW_PRIORITY flag will be logged immediately, even if they must wait a long time before being executed. And, an SQL statement that is marked with the DELAY flag will be logged even if it is never executed due to the server being restarted before it can be.

    More Logs

    MySQL has many other logging capabilities. The binary log is used to log SQL statements in which data is changed (e.g., UPDATE and INSERT statements). The log will include any binary data inserted into a table as well as text. To log low performance queries, the slow query log can be enabled. These are queries that take more time than they should. The maximum number of seconds a query should take to execute is stored in the server variable long_query_time. Queries that do not use an index will also be logged in the slow query log. Use the SHOW VARIABLES statement to see the value of this variable. Below is an example of how these logs may be enabled in the MySQL configuration file:

    [mysqld]
    log-bin = /var/logs/mysql/bin.log
    log-slow-queries = /var/log/mysql/slow.log
    ...
    

    These two lines enable binary logging and slow query logging. For the binary log file, the system will replace the extension (i.e., .log) with a six digit number, .000001 being the first. The system will create new files when the server is restarted. To force a new log to be created without restarting the server, use the FLUSH LOGS statement. When new logs are created, the server will create a new file in the numeric sequence. To manually rotate the logs like the slow query log, use the mysql-log-update script or a copy command.