Reputation:
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
.
I have installed the mysql
node module
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();
It shows the following error :
Error: connect ETIMEDOUT at Connection._handleConnectTimeout (/Users/fitz035/Desktop/sony/presave/node_modules/mysql/lib/Connection.js:425:13)
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
Upvotes: 10
Views: 71444
Reputation: 1
Check your current MySQL
server port and change to:
DB_PORT=3304
Upvotes: 0
Reputation: 75
If it on VPS configured with Firewall, you need to whitelist your IP via SSH. Otherwise it still throws exactly above error even after adding via cPanel's RemoteMYSQl
# csf -a [RemoteIP]
# csf -r
You can do it quickly via WHM too. Just posted as it may help someone.
Upvotes: 1
Reputation: 12399
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.
try this :
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();
});
Upvotes: 17