[英]How do I connect local nodeJS server with MySQL which is running on AWS?
Here is my code to connect with MYSQL, I am getting connection timeout
error : 这是我的与MYSQL连接的代码,出现
connection timeout
错误:
This is my connection file to connect with MYSQL : 这是我与MYSQL连接的连接文件:
/*
* @sqlConnection
* Creates the connection, makes the query and close it to avoid concurrency conflicts.
*/
var mysql = require('mysql');
var sqlConnection = function sqlConnection(sql, values, next) {
// It means that the values hasnt been passed
if (arguments.length === 2) {
next = values;
values = null;
}
var connection = mysql.createConnection({
"host": process.env.dbHost,
"user": process.env.dbUser,
"password": process.env.dbPass,
"database": process.env.dbName,
"port":process.env.dbPort
});
connection.connect(function (err) {
if (err !== null) {
console.log("[MYSQL] Error connecting to mysql:" + err + '\n');
}
else {
console.log("Connection is available");
}
});
connection.query(sql, values, function (err) {
connection.end(); // close the connection
if (err) {
throw err;
}
// Execute the callback
next.apply(this, arguments);
});
};
module.exports = sqlConnection;
This is the error what I am getting : 这是我得到的错误:
Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/home/himanshu/node_modules/mysql/lib/Connection.js:411:13)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at Socket.emit (events.js:208:7)
at Socket._onTimeout (net.js:420:8)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)
--------------------
I think I am doing something wrong in the config file which looks like, I don't know what to provide in db HOST or How actually I can connect with aws mysql db : 我认为我在配置文件中似乎做错了什么,我不知道在db主机中提供什么或实际上如何与aws mysql db连接:
dbHost=00.00.000.000 // aws instance ip
dbName=HIMANSHU
dbUser=ROOT
dbPass=PASSWORD
dbPort=3360
@Himanshu Just check the Inbound rule of your Instance. @Himanshu只需检查实例的入站规则。 As per your code, it looks like you are running MySQL in your EC2 machine.
根据您的代码,看起来您在EC2计算机上正在运行MySQL。
So just make sure, you have allowed 3306 port in your Inbound rules and outbound rules also 因此,只需确保在入站规则和出站规则中都允许3306端口
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.