mysql - 从 NodeJS (Express App) 连接到 MySQL 会抛出错误

标签 mysql node.js express node-mysql

编辑: 解决了我的问题。删除线后

connection.connect(function(err) {
    if (err) throw err;
});

我收到原始 MySQL 错误消息,指出 MySQL 的登录凭据错误。我已经检查过我的密码,但没有注意到错误的用户名。非常抱歉我的愚蠢问题。

<小时/>

我有一个 NodeJS/Express 应用程序。它已经工作正常,但知道我不再连接到 MySQL (MariaDB)。我总是尝试启动应用程序(使用 pm2)启动失败,因为抛出异常。

我已经检查了 MySQL 是否正在运行,并且还重新启动了 MySQL 和 NodeJS 应用程序并更新了 node-mysql。这是我连接 mysql 的代码:


//local mysql db connection
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'xxx',
    database : 'lizenzverwaltung'
});

connection.connect(function(err) {
    if (err) throw err;
});

module.exports = connection;

启动时的错误消息是:

0|www      |     at Socket.<anonymous> (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:525:10)
0|www      |     at Socket.emit (events.js:182:13)
0|www      |     at Socket.EventEmitter.emit (domain.js:442:20)
0|www      |     --------------------
0|www      |     at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:144:48)
0|www      |     at Protocol.handshake (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:51:23)
0|www      |     at Connection.connect (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:119:18)
0|www      |     at Object.<anonymous> (/var/www/html/lizenzverwaltung/models/db.js:11:12)
0|www      |     at Module._compile (internal/modules/cjs/loader.js:702:30)
0|www      |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
0|www      |     at Module.load (internal/modules/cjs/loader.js:612:32)
0|www      |     at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
0|www      |     at Function.Module._load (internal/modules/cjs/loader.js:543:3)
0|www      |     at Module.require (internal/modules/cjs/loader.js:650:17)

如果我注释掉该行 如果(错误)抛出错误; 我可以启动 NodeJS 应用程序,但当我尝试运行 SQL 查询时,立即出现以下错误:

0|www      |     at Protocol._validateEnqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:212:16)
0|www      |     at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:138:13)
0|www      |     at Connection.query (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:201:25)
0|www      |     at Function.verifyLicense [as verify] (/var/www/html/lizenzverwaltung/models/licenseModel.js:29:9)
0|www      |     at Function.LicenseController.veryLicense (/var/www/html/lizenzverwaltung/controllers/licenseController.js:51:18)
0|www      |     at /var/www/html/lizenzverwaltung/routes/license.js:33:21
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5)
0|www      |     at next (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:137:13)
0|www      |     at Route.dispatch (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:112:3)
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5) code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }
0|www      | error { Error: Cannot enqueue Query after fatal error.
0|www      |     at Protocol._validateEnqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:212:16)
0|www      |     at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:138:13)
0|www      |     at Connection.query (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:201:25)
0|www      |     at Function.verifyLicense [as verify] (/var/www/html/lizenzverwaltung/models/licenseModel.js:29:9)
0|www      |     at Function.LicenseController.veryLicense (/var/www/html/lizenzverwaltung/controllers/licenseController.js:51:18)
0|www      |     at /var/www/html/lizenzverwaltung/routes/license.js:33:21
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5)
0|www      |     at next (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:137:13)
0|www      |     at Route.dispatch (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:112:3)
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5) code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false } result null
0|www      | POST /v1/license/verify/ 200 19.128 ms - 16

我已经搜索过,但没有找到解决方案。你有什么建议吗?我的 MySQL 服务器应该运行良好,PHPMyAdmin 和我的 Wordpress 站点运行良好。

谢谢, 克劳斯

最佳答案

解决了我的问题。删除线后

connection.connect(function(err) {
    if (err) throw err;
});

我收到原始 MySQL 错误消息,指出 MySQL 的登录凭据错误。我已经检查过我的密码,但没有注意到错误的用户名。非常抱歉我的愚蠢问题。

关于mysql - 从 NodeJS (Express App) 连接到 MySQL 会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56523769/

相关文章:

node.js - coffeescript sourcemap 未在 chrome 中加载

javascript - super 表达式必须为 null 或函数,而不是未定义的 - reactjs

node.js - 无法在NodeJS中解决: 'TypeError: Converting circular structure to JSON'

mysql - 使用计数的问题

php - Mysql:只显示最合适的LIKE请求

javascript - 读取 json 文件忽略自定义注释

angularjs - 如何将 Krakenjs 与 Angularjs 结合使用?

node.js - Nunjucks:如何在html文件中使用相对文件路径?

javascript - 用于检索的 Node.js mysql 查询语法

MySQL 触发器 : table to table