【问题标题】:Trying to connect to a MySQL database using Node.js and the mysql javascript client尝试使用 Node.js 和 mysql javascript 客户端连接到 MySQL 数据库
【发布时间】:2018-10-14 05:19:42
【问题描述】:

所以我一直在尝试从我的电子应用程序连接到 MySQL 数据库:

    <script>
    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host     : '*********',
        user     : '******',
        password : '******',
        port     : '******',
        debug    : 'true'
    });
    console.log('trying to connect to DB')
    connection.connect(function(err) {
        if (err) {
            console.error('error connecting: ' + err.stack);
            return;
        }    
        console.log('connected as id ' + connection.threadId);
        });
    connection.end()
</script>

我的代码卡在 connection.connect 上,大约 1 分钟后它给了我:

    error connecting: Error: read ECONNRESET
      at exports._errnoException (util.js:1024:11)
      at TCP.onread (net.js:610:25)

我不知道该怎么做,有人有什么想法吗?

【问题讨论】:

  • mysql 实例是否与您的 Web 应用在同一台服务器上?
  • 应用程序在我的本地机器上,我正在尝试连接到 LAN 上的数据库
  • This 可能会对您有所帮助。

标签: javascript mysql node.js database electron


【解决方案1】:

您的局域网上的 mssql 实例可能未配置为允许连接到除本地计算机(即安装了 mssql 的计算机)以外的任何设备。

要允许来自您的 LAN 的连接,您需要编辑以下内容:

编辑 my.conf 并在 [mysqld] 部分找到 bind-address 参数,这可能会设置为 localhost 或 127.0.0.1,您可以将其更改为要连接到的机器的 ip数据库,或使用通配符并允许您的整个本地范围,例如 192.168.0.* 或 0.0.0.0

您需要授予对您尝试连接的数据库的访问权限

GRANT ALL ON <local database name>.* TO <user>@<iptryingtoconnectfrom> IDENTIFIED BY '<database user password>';

重启 mysql 服务就可以了。

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 2012-06-04
    • 2011-10-06
    • 2018-12-12
    • 1970-01-01
    • 2019-07-04
    • 2016-03-22
    • 1970-01-01
    相关资源
    最近更新 更多