You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js连接MySQL报错connect ECONNREFUSED 127.0.0.1:3306求助

Fixing ECONNREFUSED 127.0.0.1:3306 Error in Node.js MySQL Connection

Hey there! Let's work through this MySQL connection problem you're hitting. That Error: connect ECONNREFUSED 127.0.0.1:3306 message tells us your Node.js app can't establish a connection to your MySQL server on port 3306, even after switching the host from localhost to 127.0.0.1. Here are actionable steps to resolve this:

1. Verify MySQL Service is Running

First things first—make sure your MySQL server is actually up and running:

  • Windows: Open the Services manager (press Win + R, type services.msc), find the MySQL service, and check if its status is Running. If not, right-click it and select Start.
  • macOS: If you installed MySQL via Homebrew, run brew services list in terminal to check status. Start it with brew services start mysql.
  • Linux: Use systemctl status mysql to check, then start it with sudo systemctl start mysql if it's stopped.

2. Confirm MySQL is Listening on Port 3306

It's possible MySQL isn't configured to listen on the default 3306 port. To check:

  • Windows: Run netstat -ano | findstr :3306 in Command Prompt. You should see a process ID (PID) associated with the port, which should match MySQL's PID.
  • macOS/Linux: Run lsof -i :3306 in terminal. Look for a process named mysqld in the results.
    If no results come up, edit your MySQL config file (my.cnf or my.ini, location varies by OS) and make sure the port line is set to 3306, then restart the MySQL service.

3. Check MySQL User Permissions

Your MySQL user might not have permission to connect from 127.0.0.1 or localhost. To fix this:

  1. Log into your MySQL console (run mysql -u your_username -p and enter your password).
  2. Run these SQL commands (replace placeholders with your actual details):
    GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_mysql_user'@'127.0.0.1' IDENTIFIED BY 'your_mysql_password';
    FLUSH PRIVILEGES;
    
  3. Restart your Node.js app with nodemon app.js and test again.

4. Double-Check Your Connection Configuration

Head to routes.js (line 8 is where the error originates) and verify every parameter in your MySQL connection object is correct:

const mysql = require('mysql');
const connection = mysql.createConnection({
  host: '127.0.0.1', // Or 'localhost'—match what you used in the GRANT command
  port: 3306, // Must match MySQL's listening port
  user: 'your_mysql_user', // Correct MySQL username
  password: 'your_mysql_password', // Correct password
  database: 'your_database_name' // Ensure this database exists in MySQL
});

Watch out for typos (e.g., passwd instead of password, misspelled database name) that can break the connection.

5. Rule Out Firewall/Antivirus Interference

Sometimes local firewalls or antivirus software block Node.js from accessing port 3306. Try temporarily disabling your firewall/antivirus and running nodemon app.js again. If the connection works, add an exception rule for Node.js or MySQL in your security software to allow traffic on port 3306.

Go through these steps one at a time—chances are one of them will fix the issue. Let me know if you need clarification on any step!

内容的提问来源于stack exchange,提问作者Sonu Sourav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:02:54