简体   繁体   English

如何将本地nodeJS服务器与在AWS上运行的MySQL连接?

[英]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.

相关问题 如何获取phpMyAdmin连接到本地mysql服务器? - How do I get phpMyAdmin to connect to a local mysql server? 如何将本地数据库连接到服务器MYSQL数据库 - How do I Connect to the Local database to the Server MYSQL database 如何在运行NODEJS服务器的AMAZON(AWS)上设置Web服务? - How to set a web service on AMAZON (AWS) which is running a NODEJS server? 如何在 AWS 中编写一个 Lamdda function 以允许我连接到运行 MySQL 的 EC2 实例 - How do I write a Lamdda function in AWS that allows me to connect to an EC2 instance that is running MySQL 在NodeJS / Sequelize / MySQL中,如何根据服务器上的本地时区插入时间? - In NodeJS/Sequelize/MySQL, how do I insert time as per local time zone on the server? 从本地运行的 NodeJS / TypeORM 连接到 Google Cloud MySQL 实例 - Connect to Google Cloud MySQL instance from local running NodeJS / TypeORM 如何在外部托管服务器上将android应用程序连接到正在运行的MySQL数据库 - How do I Connect an android application to a running MySQL Database, on an external hosted server 如何从我的主机连接到在 docker 容器中运行的 mysql 服务器? - How do I connect from my host to a mysql server running in a docker container? 如何使用 SSL 从本地主机上的 node.js 连接到运行在 PlanetScale 上的 MySQL 数据库服务器? - How do I connect to a MySQL database server running on PlanetScale with SSL from node.js on localhost? 是否可以将本地 django 项目连接到在 AWS EC2 上运行的 MySql? 如何? - Is it possible to connect local django project to MySql running on AWS EC2? How?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM