如何从本地计算机建立与AWS ec2实例的MYSQL连接? [英] How do I establish MYSQL connection to AWS ec2 instance from local machine?

查看:373
本文介绍了如何从本地计算机建立与AWS ec2实例的MYSQL连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了项目,并试图将其部署到AWS.我有一个ec2实例作为我的Web服务器,具有以下配置详细信息:

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:

  • 使用端口5000的NodeJS
  • PM2(始终保持服务器处于活动状态)
  • 作为Web服务器的NGINX从我的构建文件中读取
  • ec2实例中的MySQL作为我的数据库. (使用端口3306)

我的问题是我无法建立从本地计算机到其中包含MYSQL数据库的AWS ec2实例的连接.我打开了MYSQL工作台,可以在那里很好地连接它,但是当我尝试从node.js建立到数据库的连接字符串时,这会给我一个错误.

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.

我能够在MYSQL工作台中成功连接到数据库,但是现在如何从nodejs连接字符串连接到数据库呢?

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

我已经尝试过的是:

1)在AWS安全组中,为端口5000处的所有传入流量打开TCP规则. 2)在AWS安全组中,为端口3306上的所有传入流量打开MYSQL/Aurora规则
3)向用户授予.上的所有特权,并刷新并重新启动mysql服务器.

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;
```

我希望能够从我的NodeJS成功连接到数据库实例

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

推荐答案

实际上,我认为我已经弄清楚了.

Actually I think I just figured it out.

因此,默认的mysql配置文件将ip绑定到127.0.0.1.但是,我将绑定更改为ec2公共ip,并将默认的"mysql"更改为"jordan",然后保存了配置文件,重新启动了mysql服务,现在可以使用了.

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.

这篇关于如何从本地计算机建立与AWS ec2实例的MYSQL连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆