【问题标题】:Why do I have to connect to Internet when I am using MySQL in NodeJS?为什么我在 NodeJS 中使用 MySQL 时必须连接到 Internet?
【发布时间】:2019-04-17 08:19:33
【问题描述】:

在 NodeJS 中使用 MySQL 时遇到一个奇怪的问题。当我连接到 Internet 时,我的 MySQL 数据库可以工作,但是,当我离线时,查询 MySQL 数据库会给我一个错误。我正在使用 Express 连接。

我的连接

app.use(
    connection( mysql, {
        host: dbConfig.host,
        user: dbConfig.user,
        password: dbConfig.password,
        database: dbConfig.database
    }, 'pool' )
);  

错误信息

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

还有:

connection.query(
           ^
TypeError: Cannot read property 'query' of undefined

【问题讨论】:

    标签: mysql node.js express


    【解决方案1】:

    您的/etc/hosts 文件中很可能缺少localhost 条目。

    【讨论】:

    • 好像没有,它只是文件包含IP地址和主机名之间的映射。
    • 所以您确实/etc/hosts 中有一个localhost 的条目,例如127.0.0.1 localhost::1 localhost 或类似的?如果您,那么您应该添加这样的条目。您可能还想检查您的/etc/nsswitch.conf,以确保fileshosts: 行上位于dns 之前。
    • 哦,谢谢,你让我心胸开阔。我尝试将主机的配置从 localhost 更改为 127.0.0.1,这是可行的!喜欢:var connection = mysql.createConnection({ host : '127.0.0.1', // no loger localhost user : 'root', password : '', database : 'nodejs_db' }); 和我的主机 127.0.0.1 localhost:90
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2017-06-01
    • 2019-04-17
    • 2016-10-17
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多