Node.js连接MySQL报错connect ECONNREFUSED 127.0.0.1:3306求助
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, typeservices.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 listin terminal to check status. Start it withbrew services start mysql. - Linux: Use
systemctl status mysqlto check, then start it withsudo systemctl start mysqlif 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 :3306in Command Prompt. You should see a process ID (PID) associated with the port, which should match MySQL's PID. - macOS/Linux: Run
lsof -i :3306in terminal. Look for a process namedmysqldin the results.
If no results come up, edit your MySQL config file (my.cnformy.ini, location varies by OS) and make sure theportline is set to3306, 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:
- Log into your MySQL console (run
mysql -u your_username -pand enter your password). - 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; - Restart your Node.js app with
nodemon app.jsand 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

