How to connect to MySQL using Node.js

If you want to use MySQL with your Node.js app here's how to do it:

Install the MySQL

First of all go to to check the latest version. At the day of this post:

Connect to the MySQL

Now add the following lines somewhere at the top of your application:

Run a query

Use the following function to run a query. Notice the callback function you can use to retrieve rows, fields and errors.

I've used a for to go through all the returned results.

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

Leave a Reply

Add <code> Some Code </code> by using this tags.