如何从本地计算机建立到AWS ec2实例的MYSQL连接?
我的项目已经完成,我正在尝试将其部署到AWS。我有一个ec2实例作为我的Web服务器,具有以下配置详细信息:如何从本地计算机建立到AWS ec2实例的MYSQL连接?,mysql,node.js,amazon-ec2,production,Mysql,Node.js,Amazon Ec2,Production,我的项目已经完成,我正在尝试将其部署到AWS。我有一个ec2实例作为我的Web服务器,具有以下配置详细信息: 使用端口5000的NodeJS PM2(使服务器始终处于活动状态) NGINX作为web服务器从我的构建文件中读取 ec2实例中的MySQL作为我的数据库。(使用端口3306) 我的问题是,我无法从本地计算机建立到AWS ec2实例的连接,该实例中包含MYSQL db。我打开了MYSQL workbench,我可以很好地连接到它,但是当我尝试从node.js建立到DB的连接字符串时,
- 使用端口5000的NodeJS
- PM2(使服务器始终处于活动状态)
- NGINX作为web服务器从我的构建文件中读取
- ec2实例中的MySQL作为我的数据库。(使用端口3306)
2) 在AWS安全组中,为端口3306处的所有传入流量打开MYSQL/Aurora规则
3) 正在授予上的所有特权。发送给用户,刷新并重新启动mysql服务器。
它在控制台中给了我一个错误
`{ 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 }`
以下是我尝试建立连接的代码:
```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成功连接到db实例再次确保,我认为您的安全组出了问题,可能是您的服务器在内部侦听,所以发生了问题。转到您的
EC2安全组
,选择入站并添加规则,如type=mysql
,proto=tcp
,port range=3306
,source=0.0.0/0,::/0
(允许所有)发生这种情况的原因有两个-
- 配置MySQL数据库
Enter current password for the root account: press Enter key
Set root password? Y
New password: yourpassword
Re-enter new password: yourpassword
Remove anonymous users? Y
Disallow root login remotely? n
Remove test database and access to it? Y
Reload privilege tables now? Y
- 如果您使用的是RDS,则必须提供对保存数据库的VPC的NAT访问。有关更多信息,请参阅
Enter current password for the root account: press Enter key
Set root password? Y
New password: yourpassword
Re-enter new password: yourpassword
Remove anonymous users? Y
Disallow root login remotely? n
Remove test database and access to it? Y
Reload privilege tables now? Y