How to enable MySQL Query Log?

后端 未结 14 1407
渐次进展
渐次进展 2020-11-22 09:38

How do I enable the MySQL function that logs each SQL query statement received from clients and the time that query statement has submitted? Can I do that in phpmyadmin or N

相关标签:
14条回答
  • 2020-11-22 10:09

    I had to drop and recreate the general log at one point. During the recreation, character sets got messed up and I ended up having this error in the logs:

    [ERROR] Incorrect definition of table mysql.general_log: expected the type of column 'user_host' at position 1 to have character set 'utf8' but found character set 'latin1'

    So if the standard answer of "check to make sure logging is on" doesn't work for you, check to make sure your fields have the right character set.

    0 讨论(0)
  • 2020-11-22 10:12

    There is bug in MySQL 5.6 version. Even mysqld show as :

        Default options are read from the following files in the given order:
    C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.ini c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.cnf 
    

    Realy settings are reading in following order :

        Default options are read from the following files in the given order:
    C:\ProgramData\MySQL\MySQL Server 5.6\my.ini C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.ini c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.cnf
    

    Check file: "C:\ProgramData\MySQL\MySQL Server 5.6\my.ini"

    Hope it help somebody.

    0 讨论(0)
  • 2020-11-22 10:13

    On Windows you can simply go to

    C:\wamp\bin\mysql\mysql5.1.53\my.ini
    

    Insert this line in my.ini

    general_log_file = c:/wamp/logs/mysql_query_log.log
    

    The my.ini file finally looks like this

    ...
    ...
    ...    
    socket      = /tmp/mysql.sock
    skip-locking
    key_buffer = 16M
    max_allowed_packet = 1M
    table_cache = 64
    sort_buffer_size = 512K
    net_buffer_length = 8K
    read_buffer_size = 256K
    read_rnd_buffer_size = 512K
    myisam_sort_buffer_size = 8M
    basedir=c:/wamp/bin/mysql/mysql5.1.53
    log = c:/wamp/logs/mysql_query_log.log        #dump query logs in this file
    log-error=c:/wamp/logs/mysql.log
    datadir=c:/wamp/bin/mysql/mysql5.1.53/data
    ...
    ...
    ...
    ...
    
    0 讨论(0)
  • 2020-11-22 10:16

    Take a look on this answer to another related question. It shows how to enable, disable and to see the logs on live servers without restarting.

    Log all queries in mysql


    Here is a summary:

    If you don't want or cannot restart the MySQL server you can proceed like this on your running server:

    • Create your log tables (see answer)

    • Enable Query logging on the database (Note that the string 'table' should be put literally and not substituted by any table name. Thanks Nicholas Pickering)

    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;
    
    0 讨论(0)
  • 2020-11-22 10:17

    Not exactly an answer to the question because the question already has great answers. This is a side info. Enabling general_log really put a dent on MySQL performance. I left general_log =1 accidentally on a production server and spent hours finding out why performance was not comparable to a similar setup on other servers. Then I found this which explains the impact of enabling general log. http://www.fromdual.com/general_query_log_vs_mysql_performance.

    Gist of the story, don't put general_log=1 in the .cnf file. Instead use set global general_log =1 for a brief duration just to log enough to find out what you are trying to find out and then turn it off.

    0 讨论(0)
  • 2020-11-22 10:23

    I use this method for logging when I want to quickly optimize different page loads. It's a little tip...

    Logging to a TABLE

    SET global general_log = 1;
    SET global log_output = 'table';
    

    You can then select from my mysql.general_log table to retrieve recent queries.

    I can then do something similar to tail -f on the mysql.log, but with more refinements...

    select * from mysql.general_log 
    where  event_time  > (now() - INTERVAL 8 SECOND) and thread_id not in(9 , 628)
    and argument <> "SELECT 1" and argument <> "" 
    and argument <> "SET NAMES 'UTF8'"  and argument <> "SHOW STATUS"  
    and command_type = "Query"  and argument <> "SET PROFILING=1"
    

    This makes it easy to see my queries that I can try and cut back. I use 8 seconds interval to only fetch queries executed within the last 8 seconds.

    0 讨论(0)
提交回复
热议问题