查询MySQL数据库时出现ETIMEDOUT错误 [英] ETIMEDOUT error when querying mysql database

查看:921
本文介绍了查询MySQL数据库时出现ETIMEDOUT错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用续集pro我创建了一个名为test的数据库.它有一个名为users的表.在该表中有一个用户-> id=1, name=Phantom.

Using sequel pro I have created a database called test. It has one table called users. In that table there is one user -> id=1, name=Phantom.

我已经安装了mysql节点模块

当我运行下面的代码时,我得到The solution is: undefined.

When I run the code below I get The solution is: undefined.

谁能建议我如何连接到数据库并向用户显示?

Can anyone advise how I can connect to database and show the users?

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost:8889',
  user     : 'root',
  password : 'root',
  database : 'test'
});

connection.connect();

connection.query('SELECT * from users', function(err, rows, fields) {
  if (err) throw err;
  console.log('The solution is: ', rows);
});

connection.end();

它显示以下错误:

错误:连接ETIMEDOUT 在Connection._handleConnectTimeout(/Users/fitz035/Desktop/sony/presave/node_modules/mysql/lib/Connection.js:425:13)

Error: connect ETIMEDOUT at Connection._handleConnectTimeout (/Users/fitz035/Desktop/sony/presave/node_modules/mysql/lib/Connection.js:425:13)

我正在通过MAMP运行数据库.这些是数据库设置:

I am running the db through MAMP. These are the db settings :

Host:   localhost 
Port: 8889 
User:   root 
Password:   root 
Socket: /Applications/MAMP/tmp/mysql/mysql.sock

推荐答案

节点是异步的,因此connection.end()很可能在查询回调之前发生.另外,指定非标准时运行Mysql的端口.

Node is asynchronous, so connection.end() is likely to happen before your query calls back. Also, specify the port Mysql is running on when non-standard.

尝试:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'test',
  port: 8889
});

connection.connect();

connection.query('SELECT * from users', function(err, rows, fields) {
    if(err) console.log(err);
    console.log('The solution is: ', rows);
    connection.end();
});

这篇关于查询MySQL数据库时出现ETIMEDOUT错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆