查询MySQL数据库时出现ETIMEDOUT错误 [英] ETIMEDOUT error when querying mysql database
问题描述
使用续集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
.
当我运行下面的代码时,我得到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屋!