计算机离线时将 nodeJS 连接到 MySQL

connect nodeJS to MySQL when computer is offline

当电脑处于离线状态时,那么我的Electron应用无法连接到本地MySQL。
当电脑连接到互联网时,一切正常。
仅在 Windows.
上测试 在 nodeJS(命令提示符)和 Electron 中也会发生同样的情况。

代码:

s = {database: "test", user: "test", password: "test", host: "localhost"}
var mysql = require('./mysql');
var mysqlc = mysql.createConnection(settings);
mysqlc.connect(function(err) { console.log(err); });

错误代码为:

{ [Error: getaddrinfo ENOENT localhost:3306]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'getaddrinfo',
hostname: 'localhost',
host: 'localhost',
port: 3306,
fatal: true }

为什么?
我该怎么办?

Windows 在未连接到网络时无法将 localhost 解析为物理 IP 地址。显然,在 Windows 上,当您提供地址 localhost 时,它会将其传递给完整的 DNS 解析器,该解析器需要连接到互联网才能正常工作。

在此处找到了一个关于可能原因的好答案:Windows 7: “localhost name resolution is handled within DNS itself”. Why?

尝试使用 IP 地址本身:

s = {database: "test", user: "test", password: "test", host: "127.0.0.1"}
var mysql = require('./mysql');
var mysqlc = mysql.createConnection(settings);
mysqlc.connect(function(err) { console.log(err); });