Home » How To » How To Enable Logs in MySQL Server

How To Enable Logs in MySQL Server

The log file plays an important role during the troubleshooting of any application. It’s also useful to find details about running applications. This article will show you How To Enable Logs in MySQL Server? Also, we will try to understand how to change log file locations in the MySQL server.

There are mainly 3 types of log files in MySQL as follows:

  • Error Log – This contains all the information about errors generated by the MySQL server. This helps to debug any issue that occurred with MySQL service or database
  • General Log – This contains all the general logs of MySQL activity like query, user connection or disconnect, etc. It helps to find what’s happening on the MySQL server.
  • Slow Query Log – This contains “slow” SQL statements, All the queries which are taking more time than expected as the result of that application performance going down

Error Log in MySQL

To Enable MySQL error log or change MySQL error log location. Edit MySQL configuration file and update the following setting under [mysqld] as per your requirements. This file contains an error generated in the MySQL server.

log_error = /var/log/mysql/error.log

General Log in MySQL

To enable general logs in MySQL or to change the location of general log files, edit the configuration file and make the following changes. Uncomment the following lines to enable general logs and change the log file path to create logs on a different directory. This file contains all general logs of MySQL server for eg: queries, user connect or disconnect, etc.

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

Slow Query Log in MySQL

To enable or change the file path of MySQL slow query logs. Edit MySQL configuration file and uncomment or add the following line in [mysqld] section. This file contains logs of those queries which is taking more time to complete. Which queries taking time more than defined as long_query_time are logged.

log_slow_queries        = /var/log/mysql/mysql-slow.log
long_query_time 	= 2

Restart MySQL Service

After making any of the above changes in MySQL configuration, You must have to restart MySQL services to apply changes. Basically, the service management differs from most of the Linux operating systems. Here is the command used for the popular Linux operating systems to restart the MySQL service.
This method requires a server restart.

sudo service mysql restart
sudo systemctl restart mysql.service

Leave a Comment