aws mysql链接,如何从本地计算机建立与AWS ec2实例的MYSQL连接?

I'm finished with my project and I'm trying to deploy it to AWS. I have an ec2 instance as my webserver with the following configuration details:

NodeJS using port 5000

PM2 (keeping server alive at all times)

NGINX as web server reading from my build file

MySQL within ec2 instance as my database. (using port 3306)

My problem is I'm having trouble establishing a connection from my local machine to my AWS ec2 instance that has the MYSQL db inside of it. I opened up MYSQL workbench and I can connect to it just fine there but when I try and establish a connection string to the DB from node.js it gives me an error.

I was able to successfully connect to the DB within MYSQL workbench but how can I connect to it now from nodejs connection string?

What I already tried was the following:

1) In AWS security group opening up TCP Rule for all incoming traffic at port 5000

2) In AWS security group opening up MYSQL/Aurora Rule for all incoming traffic at port 3306

3) Granting all privileges on . to user and flushing and restarting mysql server.

Error it gives me in the console.

`{ Error: connect ECONNREFUSED 14.54.xxx.xx:3306

at Object._errnoException (util.js:1019:11)

at _exceptionWithHostPort (util.js:1041:20)

at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1175:14)

--------------------

code: 'ECONNREFUSED',

errno: 'ECONNREFUSED',

syscall: 'connect',

address: '14.54.xxx.xxx',

port: 3306,

fatal: true }`

Here is my code trying to establish the connection:

```var mysql = require("mysql");

// Local Works just fine

// var connection = mysql.createConnection({

// host: "localhost",

// user: "root",

// password: "xxx",

// database: "devdb",

// charset: "utf8mb4"

// });

// Production Connection to AWS MYSQL instance (stuck on)

var connection = mysql.createConnection({

host: "14.54.xxx.xxx",

port: "3306",

user: "jordan",

password: "xxx",

database: "productiondb"

charset: "utf8mb4"

});

// Test the db connection

connection.connect(function(err) {

if (err) {

console.log(err);

} else {

console.log("Connected!");

}

});

module.exports = connection;

```

I expect to be able to successfully connect to the db instance from my NodeJS

解决方案

Actually I think I just figured it out.

So the default mysql configuration file has the ip binded to 127.0.0.1. However, I changed the binding to my ec2 public ip and also changed the default "mysql" to "jordan" and I saved the configuration file, restarted the mysql service and it now works.

Thank you for the suggestions I'll write this down in my documentation to check for in the future.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值