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

同一EC2实例中Node.js连接MySQL出现ECONNREFUSED错误求助

Fixing ECONNREFUSED 127.0.0.1:3306 When Connecting Node.js to MySQL on EC2

Hey, that ECONNREFUSED error is a classic "something's blocking the connection" issue—let's work through the most likely fixes since both services are on the same EC2 instance:

1. First, make sure MySQL is actually running

This is the most common culprit. On your EC2 instance, run the appropriate command for your OS:

  • For Ubuntu/Debian:
    sudo systemctl status mysql
    
  • For RHEL/CentOS/Amazon Linux:
    sudo systemctl status mysqld
    

If it shows "inactive", start it up:

# Ubuntu/Debian
sudo systemctl start mysql
# RHEL/CentOS/Amazon Linux
sudo systemctl start mysqld

And set it to start on boot so you don't hit this again:

sudo systemctl enable mysql # or mysqld

2. Check if MySQL is listening on 127.0.0.1

Sometimes MySQL is configured to only bind to your EC2's private IP instead of localhost. To verify:

sudo netstat -tulpn | grep 3306

You should see something like 127.0.0.1:3306 in the output. If not, edit your MySQL config file:

  • Ubuntu/Debian: /etc/mysql/mysql.conf.d/mysqld.cnf
  • RHEL/CentOS/Amazon Linux: /etc/my.cnf or /etc/mysql/my.cnf

Find the line starting with bind-address and change it to:

bind-address = 127.0.0.1

Or use 0.0.0.0 if you want MySQL to accept connections from any IP (safe for local use, but be cautious with public access). Then restart MySQL:

sudo systemctl restart mysql # or mysqld

3. Verify your MySQL user has permission to connect from 127.0.0.1

MySQL treats localhost (socket connections) and 127.0.0.1 (TCP/IP connections) as separate hosts. If your user was created with user@localhost, it might not work with 127.0.0.1.

Log into MySQL:

mysql -u root -p

Then grant access to your user from 127.0.0.1:

GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_mysql_user'@'127.0.0.1' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;

Replace your_database_name, your_mysql_user, and your_password with your actual values.

4. Double-check your Node.js connection config

Make sure your pool setup has the correct values—here's a complete example to reference:

var mysql = require('mysql');
var pool = mysql.createPool({
  connectionLimit: 10,
  host: '127.0.0.1', // Confirm this is set correctly
  user: 'your_mysql_user',
  password: 'your_mysql_password',
  database: 'your_database_name',
  port: 3306 // Optional, since 3306 is the default, but explicit is better
});

// Test the connection to debug
pool.getConnection((err, connection) => {
  if (err) {
    console.error('Connection failed:', err.stack);
    return;
  }
  console.log('Connected successfully with ID:', connection.threadId);
  connection.release(); // Don't forget to release the connection back to the pool
});

Start with the first step—9 times out of 10, MySQL just isn't running or isn't listening on the right address.

内容的提问来源于stack exchange,提问作者Eric Walters

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:46:36