mysql-connection-failed

MySQL 连接失败的定位与修复

现象

客户端报 Can't connect to MySQL server、Access denied for user、Too many connections、Lost connection during query 或 SSL 相关错误。

快速定位

mysqladmin -h <host> -P 3306 ping
ss -lntp | grep 3306
mysql -h <host> -P 3306 -u <user> -p -e 'SELECT 1'
mysql -e 'SHOW PROCESSLIST'
mysql -e "SHOW VARIABLES LIKE 'max_connections'"
mysql -e "SHOW STATUS LIKE 'Threads_connected'"

服务端日志:

journalctl -u mysqld --since '30 min ago'
tail -100 /var/log/mysql/error.log

常见根因

  1. MySQL 未启动、端口错误或 bind-address 只监听本地。
  2. 用户授权的主机不匹配(user@host),或密码错误。
  3. max_connections 达到上限,连接池泄漏。
  4. 防火墙/安全组拦截,或 TLS 配置不匹配。
  5. DNS 反向解析慢或 skip-name-resolve 配置影响授权。
  6. 磁盘满、临时表空间不足导致连接中断。

修复

相关命令