222

I've read that Mysql server creates a log file where it keeps a record of all activities - like when and what queries execute.

Can anybody tell me where it exists in my system? How can I read it?

Basically, I need to back up the database with different input [backup between two dates] so I think I need to use log file here, that's why I want to do it...

I think this log must be secured somehow because sensitive information such as usernames and password may be logged [if any query require this]; so may it be secured, not easily able to be seen?

I have root access to the system, how can I see the log?

When I try to open /var/log/mysql.log it is empty.

This is my config file:

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
log = /var/log/mysql/mysql.log 
binlog-do-db=zero



user        = mysql
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
skip-external-locking

bind-address        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer      = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8

general_log_file        = /var/log/mysql/mysql.log
general_log             = 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
Arjun
  • 3,491
  • 4
  • 25
  • 47

9 Answers9

216

Here is a simple way to enable them. In mysql we need to see often 3 logs which are mostly needed during any project development.

  • The Error Log. It contains information about errors that occur while the server is running (also server start and stop)

  • The General Query Log. This is a general record of what mysqld is doing (connect, disconnect, queries)

  • The Slow Query Log. Ιt consists of "slow" SQL statements (as indicated by its name).

By default no log files are enabled in MYSQL. All errors will be shown in the syslog (/var/log/syslog).

To Enable them just follow below steps:

step1: Go to this file (/etc/mysql/conf.d/mysqld_safe_syslog.cnf) and remove or comment those line.

step2: Go to mysql conf file (/etc/mysql/my.cnf) and add following lines

To enable error log add following

[mysqld_safe]
log_error=/var/log/mysql/mysql_error.log

[mysqld]
log_error=/var/log/mysql/mysql_error.log

To enable general query log add following

general_log_file        = /var/log/mysql/mysql.log
general_log             = 1

To enable Slow Query Log add following

log_slow_queries       = /var/log/mysql/mysql-slow.log
long_query_time = 2
log-queries-not-using-indexes

step3: save the file and restart mysql using following commands

service mysql restart

To enable logs at runtime, login to mysql client (mysql -u root -p) and give:

SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';

Finally one thing I would like to mention here is I read this from a blog. Thanks. It works for me.

Click here to visit the blog

Nik
  • 2,885
  • 2
  • 25
  • 25
loyola
  • 3,905
  • 2
  • 24
  • 18
  • 2
    I think than you 'forgot' to write some things. Step 1 is incomplete and the sentence the 'blog' link is missing – Athafoud Apr 07 '15 at 14:01
  • Hi, @Athafoud, please find the link now. – loyola Apr 29 '15 at 05:48
  • 3
    The linked blog article mentions modifying `/etc/mysql/conf.d/mysqld_safe_syslog.cnf` only if error log is to be enabled. However, this answer as it’s currently written seems to suggest it for any log. – Melebius Jul 20 '16 at 08:19
  • 2
    My server-version is 5.6.35. The answer of Ross Smith II [here](https://stackoverflow.com/questions/11606972/how-can-i-enable-mysql-slow-query-log-on-my-server) works for me. I have to set `slow_query_log = 1;`, `slow_query_log_file = /var/log/mysql/mysql-slow-query.log` and `long_query_time = 60` under [mysqld]. When I try to start with your config, the server is starting, but sluggishly slow and with errors. I really guess the versions differ and in later versions oracle wanted to unify the variable-names. Also: IMHO 2 seconds is not that long. – JackLeEmmerdeur Jun 14 '17 at 12:16
  • 1
    What about the permission on those files? I have seen in many cases log files are not created. – MagePsycho Oct 09 '17 at 06:47
  • [ERROR] unknown variable 'log_slow_queries=/var/log/mysql/mysql-slow.log' – Vasilii Suricov Jan 29 '19 at 11:35
  • @MagePsycho, good point. I wasn't seeing my general log file at all. So I first had to `touch /var/log/mysqld.log`, then `chown mysql /var/log/mysqld.log`. (I am using an old MySQL rpm in a docker container) – laifukang Mar 07 '19 at 06:02
  • 1) in step 1, exactly what is it we need to "remove or comment"? 2) I can't find the file in either step, even when I resort to a computer-wide search. Does this answer only pertain to Linux, maybe? – donutguy640 Jun 28 '19 at 19:50
  • 1
    Important : log-slow-queries is deprecated See http://dev.mysql.com/doc/refman/5.7/en/slow-query-log.html and https://stackoverflow.com/questions/36893799/mysql-5-7-log-slow-queries-error – Rakesh Soni Dec 05 '19 at 13:12
  • @loyola Why don't talk about normal logging message ? During mysqld start, I can see `2022-12-13T11:57:25.953343Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started` such output, these are also log. And I want to add my custom log message. Can I use printf or stdout ? – Lewis Chan Dec 13 '22 at 12:23
  • In mysql 8.0, there's such file like share\errmsg_readme.txt, and it's belong to part of logging message. We mustn't ignore them. – Lewis Chan Jul 23 '23 at 07:02
61

The MySQL logs are determined by the global variables such as:

To see the settings and their location, run this shell command:

mysql -se "SHOW VARIABLES" | grep -e log_error -e general_log -e slow_query_log

To print the value of error log, run this command in the terminal:

mysql -e "SELECT @@GLOBAL.log_error"

To read content of the error log file in real time, run:

sudo tail -f $(mysql -Nse "SELECT @@GLOBAL.log_error")

Note: Hit Control-C when finish

When general log is enabled, try:

sudo tail -f $(mysql -Nse "SELECT CONCAT(@@datadir, @@general_log_file)")

To use mysql with the password access, add -p or -pMYPASS parameter. To to keep it remembered, you can configure it in your ~/.my.cnf, e.g.

[client]
user=root
password=root

So it'll be remembered for the next time.

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 1
    Or if you need to enter a password, try this: mysql -p -se "SHOW VARIABLES" | grep -e log_error -e general_log -e slow_query_log – Paul Chernoch Dec 21 '16 at 14:57
  • Great response, except you didn't explain *how* to set the global variables. – Luke Oct 27 '21 at 21:44
45

You have to activate the query logging in mysql.

  1. edit /etc/my.cnf

    [mysqld]
    log=/tmp/mysql.log
    
  2. restart the computer or the mysqld service

    service mysqld restart
    
  3. open phpmyadmin/any application that uses mysql/mysql console and run a query

  4. cat /tmp/mysql.log ( you should see the query )

Nik
  • 2,885
  • 2
  • 25
  • 25
johnlemon
  • 20,761
  • 42
  • 119
  • 178
11

From the MySQL reference manual:

By default, all log files are created in the data directory.

Check /var/lib/mysql folder.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Mark Nenadov
  • 6,421
  • 5
  • 24
  • 32
  • 1
    but i am unable to open mysql folder. i think path of log file is /var/log/mysql/mysql.log but i m unable to open it, what should i do? – Arjun Mar 26 '11 at 11:46
  • 2
    how i make myself root, i think i m owner, i install the server , i set the password, so i should be the owner, still it is locked for me,how can i open it? – Arjun Mar 28 '11 at 06:06
  • 1
    I forced cd into this directory (I had to create root user, which I deleted later), but when I see this files, I thought that they are encrypted :) So I think that's not a good way. – Line Apr 02 '14 at 13:07
  • use `sudo -s` then try to go to mysql folder. – Rashedul Islam Aug 10 '15 at 20:50
  • the MySQL manual is filled with numerous inconsistencies, either way there is no such directory on windows. – user3338098 Nov 02 '15 at 16:01
9

In my (I have LAMP installed) /etc/mysql/my.cnf file I found following, commented lines in [mysqld] section:

general_log_file        = /var/log/mysql/mysql.log
general_log             = 1

I had to open this file as superuser, with terminal:

sudo geany /etc/mysql/my.cnf

(I prefer to use Geany instead of gedit or VI, it doesn't matter)

I just uncommented them & save the file then restart MySQL with

sudo service MySQL restart

Run several queries, open the above file (/var/log/mysql/mysql.log) and the log was there :)

Andhi Irawan
  • 456
  • 8
  • 15
Line
  • 1,529
  • 3
  • 18
  • 42
  • while it may work for you, it doesn't for me, even major exceptions like "Index Corrupted" don't get logged. Only startup and shutdown issues. – user3338098 Nov 02 '15 at 16:02
7

Enter MySQL/MariaDB server command-line tool as root

  1. Set file path (you can replace general.log with the file name of your choice).

SET GLOBAL general_log_file='/var/log/mysql/general.log';

  1. Set log file format

SET GLOBAL log_output = 'FILE';

  1. Enable the server general log

SET GLOBAL general_log = 'ON';

  1. Check your configurations in global configuration variables.

SHOW VARIABLES LIKE "general_log%";

This will give results as this

  1. Enter exit to leave MySQL command-line and Tail your queries by

tail -f /var/log/mysql/general.log

or

less /var/log/mysql/general.log

  1. To disable the general server log

SET GLOBAL general_log = 'OFF';

felecian aidan
  • 150
  • 2
  • 4
4

To complement loyola's answer it is worth mentioning that as of MySQL 5.1 log_slow_queries is deprecated and is replaced with slow-query-log

Using log_slow_queries will cause your service mysql restart or service mysql start to fail

Community
  • 1
  • 1
3

In addition to the answers above you can pass in command line parameters to the mysqld process for logging options instead of manually editing your conf file. For example, to enable general logging and specifiy a file:

mysqld --general-log --general-log-file=/var/log/mysql.general.log

Confirming other answers above, mysqld --help --verbose gives you the values from the conf file (so running with command line options general-log is FALSE); whereas mysql -se "SHOW VARIABLES" | grep -e log_error -e general_log gives:

general_log     ON
general_log_file        /var/log/mysql.general.log

Use slightly more compact syntax for the error log:

mysqld --general-log --general-log-file=/var/log/mysql.general.log --log-error=/var/log/mysql.error.log
br3w5
  • 4,403
  • 5
  • 33
  • 42
1
shell> mysqladmin flush-logs


shell> mv host_name.err-old backup-directory
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
Test
  • 19
  • 1
  • 7
    Welcome to stackoverflow. Please consider giving more of an explanation of how the code in your answer is relevant to the OP's question. Also, code should be indented by 4 spaces to enable proper formatting. – Politank-Z Apr 15 '15 at 13:32