【发布时间】:2021-04-24 10:47:18
【问题描述】:
我有一个 nodejs 应用程序一直运行良好,直到我打开 MySQL Workbench 并且还在我的项目中包含 .env 文件。我正在将系统部署到 Digital Ocean 托管数据库服务时,我打开 MySQL Workbench 以可视化该过程而不使用 mysql shell。一切正常,我将我的数据库迁移到 DO 数据库集群。
我也想让我的应用程序更安全,所以我碰到了.env 文件方法,并尽我所能坚持下去,我想出了这个:
第 1 步:
npm i dotenv --save
第 2 步:将 require('dotenv').config() 添加到我的 server.js 文件中
第 3 步:更新我的数据库连接文件
const mysql = require("mysql");
const conn = mysql.createConnection({
host: process.env.HOST,
user: process.env.USERNAME,
password: process.env.PASSWORD,
//rsport : (process.env.PORT),
database: process.env.DATABASE_NAME,
multipleStatements: true,
});
conn.connect((err) => {
if (err) {
console.log("Oops!, Failed to connect to the database.");
} else {
console.log("Database connection succesfull!");
}
});
module.exports = conn;
第 4 步:我相应地设置了本地 .env 文件和远程 .env 文件
第 5 步:我运行 nodemon 它返回以下错误:
body-parser deprecated undefined extended: provide extended option server.js:27:17
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3306
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at Server.listen (net.js:1452:7)
at Function.listen (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\server.js:47:5)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3306
}
[nodemon] app crashed - waiting for file changes before starting...
我做错了什么?老实说,我认为 .env 与此无关,但我肯定知道 MySQL Workbench 建立了自己的连接。那么我是否相信一次只有一个应用程序可以连接到数据库?这对我来说也不对。
【问题讨论】:
标签: node.js mysql-workbench digital-ocean