温馨提示×

如何查debian mysql日志

小樊
55
2026-09-20 03:36:01
栏目: 云计算

在 Debian 上查看 MySQL(或 MariaDB)日志的方法取决于你安装的是 MySQL 还是 MariaDB,以及日志配置方式。下面按常见情况说明:


一、先确认是 MySQL 还是 MariaDB

mysql --version
  • 显示 MySQL → MySQL
  • 显示 MariaDB → MariaDB(Debian 默认常是 MariaDB)

二、查看 MySQL / MariaDB 错误日志(最常用)

1. 查看日志文件路径

mysql -u root -p -e "SHOW VARIABLES LIKE 'log_error';"

或者直接查配置文件:

sudo grep -R "log_error" /etc/mysql/

常见路径:

  • /var/log/mysql/error.log
  • /var/log/mysql/mysql.err
  • /var/log/mysqld.log

2. 实时查看错误日志

sudo tail -f /var/log/mysql/error.log

三、查看慢查询日志(Slow Query Log)

1. 检查是否开启

mysql -u root -p -e "SHOW VARIABLES LIKE 'slow_query_log';"

2. 查看慢日志路径

mysql -u root -p -e "SHOW VARIABLES LIKE 'slow_query_log_file';"

3. 查看慢日志

sudo tail -f /var/log/mysql/mysql-slow.log

四、查看通用查询日志(General Log,一般不默认开)

mysql -u root -p -e "SHOW VARIABLES LIKE 'general_log%';"

若开启:

sudo cat /var/log/mysql/mysql.log

五、通过 systemd 查看(推荐)

sudo journalctl -u mysql
# 或 MariaDB
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

修改后重启:

sudo systemctl restart mysql

如果你愿意,可以告诉我:

  • Debian 版本
  • MySQL / MariaDB 版本
  • 具体想查哪种日志(报错 / 慢查询 / 登录记录)

我可以给你更精确的命令。

0 踩