0

I am new to AWS and Node js. I want to query a MySQL server in a private ec2 instance in a testing project which I am building using Node js. How should I go about it? Thanks

EDIT: I want to access it through my local computer. The way I came up with was:

  1. Start a terminal from node js - don't know which method would be best

  2. Use the terminal to login into public ec2

  3. connect to private ec2 through public instance

  4. launch the MySQL client through private instance and query it.

I wanted to know if there is a better way to do this. And any advice on how to achieve the same

2
  • Need to read up about VPC, private subnets, Internet gateways, NAT gateways, VPC interface endpoints, security groups, nacles, and more. AWS docs is good place to start. Commented Mar 3, 2020 at 10:20
  • Are you going to query the MySQL from outside (internet) or from another EC2 instance in the same VPC? Because that point is important to either use a NAT Gateway or any other approach Commented Mar 3, 2020 at 10:42

2 Answers 2

2
  1. install the package: jm-ez-mysql

    npm install jm-ez-mysql --save

    use this document url: https://www.npmjs.com/package/jm-ez-mysql

  2. configuration and connection databse database.js file

    const My = require('jm-ez-mysql');

    // Init DB Connection
    const connection = My.init({
      host: process.env.DBHOST,
      user: process.env.DBUSER,
      password: process.env.DBPASSWORD,
      database: process.env.DATABASE,
      dateStrings: true,
      charset: 'utf8mb4',
      timezone: 'utc',
      multipleStatements: true,
      connectTimeout: 100 * 60 * 1000,
      acquireTimeout: 100 * 60 * 1000,
      timeout: 100 * 60 * 1000,
    });
    
    module.exports = {
      connection,
    };
    

In express js project to configuration database

/config
  /database.js
/server.js
/.env

const http = require('http');
const app = require('express')();
require('./config/database.js');
const bodyParser = require('body-parser');
const server = http.createServer(app);

server.listen(process.env.ServerPort, '0.0.0.0', () => {
  logger.info(`Express server listening on port ${process.env.ServerPort}`);
});
  1. server.js file

    const My = require('jm-ez-mysql');

    My.first("psu_project", ["id"], "1=1 ").then(function (r) { console.log(r); });

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Install the package: mysql

    npm install -save mysql

  2. Connect MySQL to the server.

Example:

const mysqlConnection = require('mysql').createConnection;

mysqlConnection = mysqlConnection({
        host: HOST,
        user: DB_USER,
        password: DB_PASSWORD,
        database: DB_NAME,
      });
mysqlConnection.connect();

mysqlConnection.query('SELECT * FROM table_name');

NOTE: query() is an asynchronous function.

1 Comment

Thank you for the response. Unfortunately this is not what i was looking for. Please refer to the addition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.