Connecting and sending SQL to a MySQL server
Structured Query Language has been a standard since 1986 and it's the prevailing language for relational databases. MySQL is the most popular SQL relational database server around, often appearing in the prevalent LAMP (Linux Apache MySQL PHP) stack.
If a relational database was conceptually relevant to our goals in a new project, or we were migrating a MySQL-backed project from another framework to Node, the third-party mysql
module would be particularly useful.
In this task, we will discover how to connect to a MySQL server with Node and execute SQL queries across the wire.
Getting ready
Let's grab mysql
, which is a pure JavaScript (as opposed to C++ bound) MySQL client module.
npm install mysql
We'll need a MySQL server to connect to. By default, the mysql
client module connects to localhost
, so we'll have MySQL running locally.
On Linux and Mac OSX we can see if MySQL is already installed with the following command:
whereis mysql
And we can see...