简体   繁体   English

尝试使用Node.js和mysql javascript客户端连接到MySQL数据库

[英]Trying to connect to a MySQL database using Node.js and the mysql javascript client

So i have been trying to connect to a MySQL database from my electron app: 所以我一直试图从我的电子应用程序连接到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>

My code gets stuck on connection.connect and after about 1 min it give me: 我的代码卡在connection.connect上,大约1分钟后,它给了我:

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

I have no idea what to do, anyone have any ideas? 我不知道该怎么办,任何人都有什么主意?

The chances are that your mssql instance on your lan is not configured to allow connections to anything but your local machine, ie the machine that mssql is installed. 可能是您的局域网上的mssql实例未配置为允许连接到除本地计算机(即安装了mssql的计算机)以外的任何对象。

To allow connections from your LAN you need to edit the following: 要允许来自局域网的连接,您需要编辑以下内容:

edit my.conf and find the bind-address parameter in the [mysqld] section, this will probably be set to localhost or 127.0.0.1, you can either change this to the ip of the machine you want to connect to the database, or use a wildcard and allow your whole local range, eg 192.168.0.* or 0.0.0.0 编辑my.conf并在[mysqld]部分中找到bind-address参数,该参数可能会设置为localhost或127.0.0.1,您可以将其更改为要连接到数据库的计算机的ip,或者使用通配符并允许您使用整个本地范围,例如192.168.0。*或0.0.0.0

You will need to grant access to the database you are trying to connect to 您将需要授予对您尝试连接的数据库的访问权限

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

Restart the mysql service and you should be good to go. 重新启动mysql服务,您应该一切顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM