简体   繁体   English

无法从 Node.js 应用程序连接到 MySQL DB

[英]Cant connect to MySQL DB from Node.js app

im new to MYSQL and database programming in general.我是MYSQL和数据库编程的新手。 using mac im setting the mysql server with docker image by running this command:使用 mac im 通过运行以下命令使用 docker 镜像设置mysql服务器:

docker run -d --name sql_server_demo -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=123123123' -p 1433:1433 microsoft/mssql-server-linux

the container is running and when i try to connect from command line like so:容器正在运行,当我尝试从命令行连接时,如下所示:

mssql -u sa -p 123123123

it connects successfully and i can start typing queries.它连接成功,我可以开始输入查询。 im also able to connect via Azure Data Studio as well.我也可以通过Azure Data Studio进行连接。

the problem is when im trying to connect from node.js app like so:问题是当我尝试从node.js应用程序连接时,如下所示:

var mysql = require('mysql');

const config = {
host: 'localhost',
user: 'sa',
password: 'reallyStrongPwd123',
database: 'logger',
port: '1433',
};

var con = mysql.createConnection(config);

con.connect(function (err) {
  if (err) throw err;
  console.log("Connected!");
});

i specify the port according to the docker mysql container and the callback from connect never happens.我根据 docker mysql容器指定端口, connect的回调从未发生。 when i delete the port i get this error: Error: connect ECONNREFUSED 127.0.0.1:3306当我删除端口时,我收到此错误: Error: connect ECONNREFUSED 127.0.0.1:3306

any help would be appreciated here:) thanks!任何帮助将不胜感激:) 谢谢!

您使用的是 MsSQL 服务器而不是 MySQL,因此您必须将 docker 映像更改为 mysql(或使用: https : //hub.docker.com/_/mysql )此 NPM 模块https://www.npmjs.com/package /mssql

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM