简体   繁体   English

如何在没有 SSH 的情况下从我的计算机本地远程访问 MySQL 数据库(通过 Node.js 应用程序)?

[英]How to remote access MySQL database locally from my computer(through Node.js app) without SSH?

I have a mysql server running on ec2 instance.我有一个在 ec2 实例上运行的 mysql 服务器。 I also have a Node.js app running locally from where I want to connect to my mysql database running on the ec2 instance.我还有一个 Node.js 应用程序在本地运行,我想从该应用程序连接到在 ec2 实例上运行的 mysql 数据库。 I'm using this following code to connect:我正在使用以下代码进行连接:

const mysql = require('mysql2/promise');

const mysqlHost = config.host;
const mysqlUser = config.mysql_user;
const mysqlPassword = config.mysql_password;
const mysqlDb = config.mysql_db;
const mysqlPort = config.mysql_port;
/*
*
* Connection Options
*
* */
const options = {
    connectionLimit: config.connection_pool,
    host: mysqlHost,
    user: mysqlUser,
    password: mysqlPassword,
    database: mysqlDb,
    port: mysqlPort,
    multipleStatements: true,
    Promise: bluebird,
};

/*
*
* Pool
*
* */
logger.warn({ mysqlHost, mysqlDb }, 'Establishing MySQL Connection Pool');
const pool = mysql.createPool(options);

Now, it doesn't connect to the instance and gives this error:现在,它没有连接到实例并给出此错误:

Unhandled rejection Error: connect ECONNREFUSED未处理的拒绝错误:连接 ECONNREFUSED

I'm able to ssh into the mysql database through MySQL Workbench and it's working fine.我能够通过 MySQL 工作台将 ssh 导入 mysql 数据库,并且工作正常。 Also, I have set the Inbound rules to ALL traffic and from anywhere so there's no problem from AWS side.此外,我已将入站规则设置为所有流量和来自任何地方,因此 AWS 方面没有问题。

I read a lot about it and here are my findings: Edit the my.cnf file in mysql to bind it to the IP of the host computer (which I tried but not able to do it...throwing error everytime) Here is my.cnf file我阅读了很多关于它的内容,这是我的发现:编辑 mysql 中的 my.cnf 文件,将其绑定到主机的 IP(我尝试过但无法做到...每次都抛出错误)这是我的.cnf 文件

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

Please someone help me out.请有人帮助我。

So After a lot of research I finally figured this out, So, the file my.cnf has contents as shown above and as said above I knew that somehow I had to use bind-address but couldn't figure out how to do it.所以经过大量研究,我终于弄清楚了,所以,文件my.cnf的内容如上所示,如上所述,我知道我必须以某种方式使用bind-address但不知道该怎么做。

Turns out the file I was looking for was located here:原来我正在寻找的文件位于此处:

/etc/mysql/mysql.conf.d 

Here, I edited the file as:在这里,我将文件编辑为:

#skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 0.0.0.0

Since, mysql in aws instance doesn't allow remote access we gotta make the bind-address as shown below which means we can access it from anywhere.因为,aws 实例中的 mysql 不允许远程访问,我们必须如下所示设置绑定地址,这意味着我们可以从任何地方访问它。 Also, we gotta comment the skip-external-locking .另外,我们必须评论skip-external-locking

That's it now without using ssh we can access the db with any user having correct privileges.现在就是这样,不使用 ssh 我们可以使用任何具有正确权限的用户访问数据库。 The user that I used was:我使用的用户是:

create user xyz identified by 'your-password' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebRTC,JS,node.js应用程序通过远程服务器在本地运行,但没有不同的连接 - WebRTC, JS, node.js App working locally through remote server but not different connections 如何使用 Node.js 连接到远程 mysql 数据库 - How to connect to remote mysql database using Node.js 如何在本地访问 Node.JS 项目? - How to access Node.JS project locally? 如何在浏览器而不是localhost上访问远程node.js应用 - How to access remote node.js app in browser, not on localhost Node.js WebStorm 远程调试不起作用(没有 SSH 隧道) - Node.js WebStorm remote debugging not working (without SSH tunnel) 仅从我的应用程序访问我的后端node.js - access my backend node.js only from my app 如何将node.js Web应用程序上传到openshift? 它不是在git上,而是在我的计算机上? - How do I upload my node.js web app to openshift? It is NOT on git, but just on my computer? 如何从 node.js 中的 MySQL 访问变量? - How to access a variable from MySQL in node.js? Node.js / OS X:当计算机进入睡眠状态时如何通知node.js应用 - Node.js / OS X: How to notify node.js app when computer enters sleep 通过node.js连接到mysql时访问被拒绝 - access denied while connecting to mysql through node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM