gpt4 book ai didi

mysql - 如何使用nodeJS连接azure mysql服务器

转载 作者:行者123 更新时间:2023-12-03 06:47:55 25 4
gpt4 key购买 nike

我正在尝试使用nodejs连接到远程mysql服务器

到目前为止我能够连接mysql workbench 8.0,所以mysql服务器没有问题。我可以轻松连接到本地 mysql 服务器(使用“mysql2”模块)。

这是我一直在尝试连接 Azure 提供的远程 mysql 服务器的内容。我按照提供的指南进行操作。

const mysql = require("mysql");
const fs = require("fs")

var connection = mysql.createConnection({
host: process.env.MYSQL_SERVER_HOST,
user: process.env.MYSQL_SERVER_USER,
password: process.env.MYSQL_SERVER_PASSWORD,
database: process.env.MYSQL_SERVER_DATABASE,
port:3306,
ssl: { ca: fs.readFileSync("./certificates/DigiCertGlobalRootCA.crt.pem")}
});

// This is how I am connecting to my local mysql-server which is working fine
// // const connection = mysql.createConnection({
// host: 'localhost',
// user: 'root',
// password: process.env.LOCAL_MYSQL_SERVER_PASSWORD,
// database: 'db',
// port: '3306'
// });

connection.connect((error) => {
if (error) {
console.log("mysql error")
console.log(error)
}
else {
console.log("Mysql Ready")
}
});

module.exports = connection;

这里我使用mysql包,因为网上很多文章都建议它,但我什至使用mysql2但没有任何反应。我什至更改了网络设置并允许访问我的 IP 地址(但这不是必需的)。

错误日志:

mysql error
Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (path\node_modules\mysql\lib\Connection.js:409:13)
at Object.onceWrapper (node:events:641:28)
at Socket.emit (node:events:527:28)
at Socket._onTimeout (node:net:516:8)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)
--------------------
at Protocol._enqueue (path\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (path\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (path\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (path\packnary\database\mysql.js:26:12)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}

最佳答案

我在没有使用代码的情况下将 Azure SQL 数据库连接到了 Nodejs。

我遵循以下程序:

我在 Visual Studio 中创建了 azure 空白 NodeJs Web 应用程序。

enter image description here

运行成功。之后,我转到 Web 应用程序和所选连接服务的概述,然后单击添加服务依赖项选项并搜索 azure sql 数据库。

引用图片:

enter image description here

enter image description here

选择 azure sql 数据并单击下一步按钮。登录Azure帐户并选择我们需要连接的数据库。

引用图片:

enter image description here

在下一个窗口中输入所需的详细信息,然后单击“下一步”。

引用图片:

enter image description here

根据要求选择更改,然后点击下一步。

引用图片:

enter image description here

连接成功。

引用图片:

enter image description here

enter image description here

关于mysql - 如何使用nodeJS连接azure mysql服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73960040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com