node-mysql client.connect() hangs

420 views
Skip to first unread message

Cainus

unread,
Sep 23, 2010, 5:57:23 AM9/23/10
to nodejs
Hey all... I'm wondering if someone can throw me a clue... I'm trying
to figure out how to connect to mysql and I'm having no luck. Here's
the short script that just hangs:

var Client = require('mysql').Client,
client = new Client();

client.user = 'root';
client.password = 'geronimo';
client.host = 'localhost';
client.port = 3306;

client.connect();


This is all valid connection info... If I give invalid info, I get an
appropriate error and the script terminates normally.

Thanks!

Bert Belder

unread,
Sep 23, 2010, 1:52:21 PM9/23/10
to nodejs
I think your script doesn't hang, but Node just won't exit as long as
the client is connected. So I suggest you try to do a query. When the
database connection is no longer needed you can disconnect the client
with client.end().

Cainus

unread,
Sep 23, 2010, 10:34:10 PM9/23/10
to nodejs
A client.end() call at the end doesn't seem to have any effect
actually. I've tried it both ways and the script still hangs. I just
left it off in my paste because it's not necessary to reproduce the
issue.

Any other ideas?

Thanks!

Alan Gutierrez

unread,
Sep 24, 2010, 11:25:17 PM9/24/10
to nod...@googlegroups.com
The node-mysql library works great. I am using it in a project that I converted from a Java. The line count of the application went down by an order of magnitude. From 2,400~ to 240~. A huge part of it was replacing Hibernate, JPA and all the XML and model classes that go with it, with a handful of JavaScript functions.

Your problem is that you're calling end after calling connect, but connect is asynchronous, so the connection has not been made when you call end, so it is a noop. Wait for the connection, then end it.


var Client = require('mysql').Client
  , client = new Client();

client.user = 'scott';
client.password = 'tiger';
client.database = 'accounting';

client.connect(function () {
    client.end();
});

Alan Gutierrez - http://twitter.com/bigeasy - al...@prettyrobots.com

Cainus

unread,
Sep 28, 2010, 5:35:28 AM9/28/10
to nodejs
Thanks Alan! That's got it. :)



On Sep 24, 11:25 pm, Alan Gutierrez <a...@prettyrobots.com> wrote:
> The node-mysql library works great. I am using it in a project that I  
> converted from a Java. The line count of the application went down by  
> an order of magnitude. From 2,400~ to 240~. A huge part of it was  
> replacing Hibernate, JPA and all the XML and model classes that go  
> with it, with a handful of JavaScript functions.
>
> Your problem is that you're calling end after calling connect, but  
> connect is asynchronous, so the connection has not been made when you  
> call end, so it is a noop. Wait for the connection, then end it.
>
> var Client = require('mysql').Client
>    , client = new Client();
>
> client.user = 'scott';
> client.password = 'tiger';
> client.database = 'accounting';
>
> client.connect(function () {
>      client.end();
>
> });
>
> Alan Gutierrez -http://twitter.com/bigeasy- a...@prettyrobots.com
Reply all
Reply to author
Forward
0 new messages