问题描述
我是AWS和Node js的新手.我想在我使用Node js构建的测试项目中的私有ec2实例中查询MySQL服务器.我应该怎么做?谢谢
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
我想通过我的本地计算机访问它.我想出的方法是:
I want to access it through my local computer. The way I came up with was:
-
从节点js启动终端-不知道哪种方法最好
Start a terminal from node js - don't know which method would be best
使用终端登录公共ec2
Use the terminal to login into public ec2
通过公共实例连接到私有ec2
connect to private ec2 through public instance
通过私有实例启动MySQL客户端并对其进行查询.
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
推荐答案
-
安装软件包: jm-ez-mysql
npm install jm-ez-mysql --save
使用此文档的网址: https://www.npmjs.com/package/jm-ez-mysql
配置和连接数据库database.js文件
configuration and connection databsedatabase.js file
const My = require('jm-ez-mysql');
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,
};
在Express js项目中到配置数据库
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}`);
});
-
server.js文件
server.js file
const My = require('jm-ez-mysql');
const My = require('jm-ez-mysql');
My.first("psu_project",["id"],"1 = 1").then(函数(r){console.log(r);});
My.first("psu_project", ["id"], "1=1 ").then(function (r){ console.log(r);});
这篇关于在Node.js中的私有EC2实例中访问MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!