DataGrip连接MySQL出现Connection refused错误,请求技术协助
DataGrip连接MySQL报Connection refused的解决方案
问题信息
连接错误提示:
DBMS: MySQL (no ver.)
Case sensitivity: plain=mixed, delimited=exact
Connection refused: connect.
idea.log关键日志片段:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 2023-08-15 18:14:29,862 [ 139990] INFO - #c.i.d.d.DatabaseConnectionEstablisher - Connecting to: jdbc:mysql://localhost:3306 2023-08-15 18:14:29,862 [ 139990] INFO - #c.i.d.d.DatabaseConnectionEstablisher - Auth provider: user-pass 2023-08-15 18:14:29,863 [ 139991] INFO - #c.i.d.d.DatabaseCredentialsAuthProvider - Connecting as: root 2023-08-15 18:14:29,869 [ 139997] WARN - #c.i.d.d.DatabaseConnectionEstablisher - Connecting to: jdbc:mysql://localhost:3306 2023-08-15 18:14:29,869 [ 139997] WARN - #c.i.d.d.DatabaseConnectionEstablisher - [08S01] Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
解决步骤
检查MySQL服务是否运行
- Windows:打开「服务」面板,找到MySQL服务确认状态为「正在运行」;未运行则右键启动,或执行命令
net start mysql - Linux/macOS:执行命令
systemctl status mysql查看状态,未运行则执行systemctl start mysql启动服务
- Windows:打开「服务」面板,找到MySQL服务确认状态为「正在运行」;未运行则右键启动,或执行命令
验证连接地址与端口正确性
- 确认DataGrip连接配置中URL为
jdbc:mysql://localhost:3306,检查MySQL配置文件(my.cnf/my.ini):port参数是否设置为3306bind-address本地连接时设为127.0.0.1或localhost,需远程连接则设为0.0.0.0(允许所有IP访问)
- 确认DataGrip连接配置中URL为
检查MySQL用户权限
- 登录MySQL客户端:
mysql -u root -p - 执行查询查看用户权限范围:
SELECT host, user FROM mysql.user; - 若root用户的host不是localhost或%,执行权限修改命令:
- 本地连接:
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码'; - 远程连接:
ALTER USER 'root'@'%' IDENTIFIED BY '你的密码';
- 本地连接:
- 刷新权限生效:
FLUSH PRIVILEGES;
- 登录MySQL客户端:
排查防火墙/安全软件拦截
- Windows:临时关闭Windows防火墙测试,或添加3306端口的入站允许规则
- Linux:执行
ufw allow 3306(Ubuntu/Debian)或firewall-cmd --add-port=3306/tcp --permanent && firewall-cmd --reload(CentOS/RHEL)
匹配MySQL驱动版本
- 在DataGrip连接设置的「驱动」选项卡中,确认驱动版本与MySQL服务器版本匹配:MySQL 8.0+使用8.x版本驱动,5.7及以下使用5.x版本驱动,可直接在DataGrip内点击「下载」更新驱动
内容的提问来源于stack exchange,提问作者mmt
相关产品推荐
相关产品推荐

