javascript - 尝试使用 Node.js 和 mysql javascript 客户端连接到 MySQL 数据库
问题描述
所以我一直在尝试从我的电子应用程序连接到 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)
我不知道该怎么办,有人有什么想法吗?
解决方案
很可能您的局域网上的 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 服务,您应该一切顺利。
推荐阅读
- java - 你如何从一个类中运行一组测试类
- php - 当我在 php 邮件程序中添加此查询 $email=$_POST['RegistrationForm[usernam] 时,我无法获取任何数据
- python - 应处理的文本数量 = 1 = 4(处理文档的功能)
- sql - 如何组合数据类型
- wordpress - 从单选切换到复选框时的 for 循环问题
- typescript - Typescript 类型的元组成员依赖于其他
- python-3.x - 使用向上和向下箭头键在条目小部件的输入框之间移动
- python - “UnboundLocalError:分配前引用的局部变量'isrunning'” Idk 为什么
- angular - 使用“ToastController”显示通知列表
- algorithm - 找到除以 n 的子数组的有效方法