MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
MySQL has several types of log files that provide information about different aspects of MySQL's activity. Each type of log serves a different purpose and can help you in various ways, such as debugging issues, analyzing performance, or auditing database activities. Here's a brief tutorial on the types of logs in MySQL and their classifications.
Error Log: The error log contains information about any issues encountered during the startup, running, or shutdown of mysqld. If mysqld notices a table that needs to be checked or repaired, it writes a message to the error log.
General Query Log: This is a general record of what mysqld is doing. The server writes information to this log when clients connect or disconnect, and it logs each SQL statement received from clients. The general query log can be very useful when you suspect an error in a client and want to see exactly what the client sent to mysqld.
Binary Log (Binlog): The binary log contains all statements that update data or potentially could have updated it. For example, a DELETE or UPDATE which matched no rows. It does not contain SELECT statements or statements with only a runtime effect such as SET @a=1 or DO SLEEP(1). The binary log is used for replication and data recovery operations.
Slow Query Log: The slow query log consists of SQL statements that took more than long_query_time
seconds to execute and required at least min_examined_row_limit
rows to be examined. The slow query log can be used to find queries that take a long time to execute and are thus candidates for optimization.
Relay Log: The relay log is used in replication. When a slave is replicating from a master, the events read from the master's binary log are written to the slave's relay log. The slave's SQL thread then reads these events and applies them.
DDL Log: The DDL (Data Definition Language) log is a crash-safe storage area used during the process of building and executing DDL statements.
To configure these logs, you'll generally need to modify your MySQL configuration file (my.cnf or my.ini). For example, to enable the general query log, you would add the following lines:
general_log = 1 general_log_file = /var/log/mysql/mysql.log
Remember to replace '/var/log/mysql/mysql.log' with the path where you want the log file to be stored.
Note: It's crucial to manage your logs properly. Logs can consume a lot of disk space over time, so make sure to implement log rotation and purge old logs periodically. Furthermore, be aware that logging incurs some overhead, so enable only those logs that you need. For instance, the general query log can be very resource-intensive on a busy server, so it's typically turned off unless you're debugging a specific issue.
MySQL error log location:
/var/log/mysql/error.log
C:\Program Files\MySQL\MySQL Server 8.0\data\hostname.err
Query log in MySQL:
[mysqld] general_log = 1 general_log_file = /path/to/query.log
[mysqld] general_log = 0
Slow query log in MySQL:
[mysqld] slow_query_log = 1 slow_query_log_file = /path/to/slow-query.log long_query_time = 1 # Time in seconds
[mysqld] slow_query_log = 0
Enable and disable MySQL logs:
[mysqld] general_log = 1
Viewing MySQL log files:
cat /var/log/mysql/error.log
cat /path/to/query.log
MySQL general query log:
[mysqld] general_log = 1 general_log_file = /path/to/general-query.log
[mysqld] general_log = 0
MySQL binary log vs error log:
/var/lib/mysql/mysql-bin.000001
/var/log/mysql/error.log