10

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
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • 1
    Check the `err` parameter in the callback to see, what went wrong. From your code I guess, the connection was closed, before the actual query went through. – Sirko Nov 29 '16 at 14:43
  • @Sirko updated the question –  Nov 29 '16 at 14:46
  • 1
    Have you verified, that your database is actually running (on that port)? – Sirko Nov 29 '16 at 14:46
  • I am running the db through `MAMP`. I believe these are the db settings - `Host: localhost Port: 8889 User: root Password: root Socket: /Applications/MAMP/tmp/mysql/mysql.sock` –  Nov 29 '16 at 14:48
  • 1
    If your db is running on port 8889, you should pass that as an additional parameter to the connection. The default mysql port is 3306, where your app wont find any server right now. As an alternative you can pass the socket directly as shown here: http://stackoverflow.com/a/26465986/1169798 – Sirko Nov 29 '16 at 14:50
  • @Sirko I have updated it to `host : 'localhost:8889',`. the error is no longer showing but it is now timing out –  Nov 29 '16 at 14:51
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129360/discussion-between-sirko-and-phantom). – Sirko Nov 29 '16 at 14:51

3 Answers3

17

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();
});
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • apologies, updated the question. Think the new info may make some more sense –  Nov 29 '16 at 14:46
  • indeed, that changes everything :) try adding the port number to `createConnection` – xShirase Nov 29 '16 at 14:51
  • cool, the point about `connection.end` is still valid though. Accept the answer if you're happy with it, or i can clarify if needed – xShirase Nov 29 '16 at 14:59
  • 2
    Just a side note if you ever use a pool of connection, the correct way of closing a connection in a pool is by using connection.release() (connection.end() is deprecated !) – Luxior Jan 14 '22 at 15:36
1

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.

Raju
  • 75
  • 10
0

Check your current MySQL server port and change to:

DB_PORT=3304
Toni
  • 1,555
  • 4
  • 15
  • 23