在 Debian 系统上查看 MySQL(或 MariaDB,Debian 默认有时用 MariaDB)日志,通常有以下几种方式,取决于你的 MySQL 版本和配置。
systemctl status mysql
# 或
systemctl status mariadb
通常位于:
/var/log/mysql/error.log
查看:
sudo tail -f /var/log/mysql/error.log
如果找不到,可登录 MySQL 查看:
SHOW VARIABLES LIKE 'log_error';
默认可能未开启。
查看是否开启:
SHOW VARIABLES LIKE 'slow_query_log';
SHOW VARIABLES LIKE 'slow_query_log_file';
查看慢日志文件(示例):
sudo tail -f /var/log/mysql/mysql-slow.log
SHOW VARIABLES LIKE 'general_log';
SHOW VARIABLES LIKE 'general_log_file';
MySQL / MariaDB 的启动和运行错误也会写入 systemd:
sudo journalctl -u mysql
# 或
sudo journalctl -u mariadb
实时查看:
sudo journalctl -u mysql -f
日志相关配置通常在:
/etc/mysql/my.cnf
/etc/mysql/mysql.conf.d/mysqld.cnf
/etc/mysql/mariadb.conf.d/
示例配置:
[mysqld]
log_error = /var/log/mysql/error.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
修改后重启:
sudo systemctl restart mysql
# 查看最近错误
sudo tail -n 100 /var/log/mysql/error.log
# 实时跟踪
sudo tail -f /var/log/mysql/error.log
# 搜关键字
sudo grep -i error /var/log/mysql/error.log
如果你能告诉我:
我可以给你更精确的命令。