【问题标题】:cPanel MariaDB connection fails with node.js mysql connectorcPanel MariaDB 连接失败,node.js mysql 连接器
【发布时间】:2020-12-30 22:00:31
【问题描述】:

我正在尝试托管一个连接到 maria db 的 express.js 应用程序。在我的本地计算机上,我可以在本地 MariaDB 上成功执行查询。但是当我尝试在主机上做同样的事情时(使用 cPanel)它失败了这个错误。

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
    --------------------
    at Protocol._enqueue (/home/gaziokcu/nodevenv/express/10/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/gaziokcu/nodevenv/express/10/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/home/gaziokcu/nodevenv/express/10/lib/node_modules/mysql/lib/Connection.js:116:18)
    at /home/gaziokcu/express/routes/index.js:32:12
    at Layer.handle [as handle_request] (/home/gaziokcu/nodevenv/express/10/lib/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/gaziokcu/nodevenv/express/10/lib/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/gaziokcu/nodevenv/express/10/lib/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/gaziokcu/nodevenv/express/10/lib/node_modules/express/lib/router/layer.js:95:5)
    at /home/gaziokcu/nodevenv/express/10/lib/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/gaziokcu/nodevenv/express/10/lib/node_modules/express/lib/router/index.js:335:12)
/home/gaziokcu/express/routes/index.js:33
  if (err) throw err;

我用于连接的代码:

const mysql = require('mysql');

    var connection = mysql.createConnection({
        host: 'localhost',
        user:'user', 
        password: '<password>',
        database:'<database>',
    });

即使将“localhost”或“127.0.0.1”写入主机,连接器也会尝试与“127.0.0.1”连接。我的数据库管理员说由于网络配置,我必须使用“localhost”关键字连接。

我该如何解决这个问题?

编辑:

我可以使用 localhost 和 3306 设置通过 PHP 连接到数据库。

【问题讨论】:

  • 您需要获取数据库连接的 IP。本地主机不是正确的。
  • @Talg123 但我可以使用 localhost 在本地 PC 上连接。

标签: mysql node.js express mariadb cpanel


【解决方案1】:

大多数其他连接器将 localhost 映射到 unix 套接字。

nodejs 有一个socketPath 选项连接到 mariadb unix 套接字。

我假设在您的配置中 MariaDB 没有监听 3306。

【讨论】:

  • 我可以使用 localhost:3306 在 php 中连接数据库,管理员说我必须使用 localhost,而不是 127.0.0.1,当我尝试使用 node.js 库连接时,他们正在尝试连接 127.0 .0.1 即使我将 'localhost' 写入主机名并将 3306 写入端口名。
  • 他还说没有可用的socketPath
  • 如果 php 连接到 localhost 那么它使用套接字。使用 php_info 显示默认套接字是什么,并在 nodejs 中使用。
  • 你好@danblack,我也试过 socketPath /var/lib/mysql/mysql.sock 和 /tmp/mysql.sock 但它不起作用。 phpinfo 说它在 /var/lib/mysql 文件夹中。
  • 听起来您应该更换无法在他们的系统上提供具体建议的管理员。
猜你喜欢
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 2019-07-19
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多