Chapter 23

Connecting to MySQL Using node-mysql

WHAT’S IN THIS CHAPTER?

  • Connecting to MySQL databases from Node
  • Leveraging Node’s asynchronous, non-blocking style when exchanging data with the database
  • Avoiding security and performance pitfalls

As in many other programming environments, you will sooner or later want to persist certain types of data your application generates. This is especially true with multi-user server-client applications, in which information one user provides needs to be presented to one or many other users at a later point in time.

Although saving data in flat files or memory works in Node just as well as in other environments, it doesn’t scale when structured access is needed for large amounts of data.

Relational databases like MySQL provide a reliable and fast solution to save and retrieve information in a structured manner by storing data on the hard drive and making it accessible through the standardized SQL language.

If your Node applications need to be able to communicate with MySQL databases and exchange data with them, the following sections on using the node-mysql library, reading from a MySQL database efficiently, and passing in data in a secure way will enable you to accomplish these tasks.

USING A LIBRARY TO CONNECT TO AND COMMUNICATE WITH A MYSQL DATABASE

As installing and setting up a MySQL database is not within the scope of this book, it’s assumed that you have a working MySQL instance running on your localhost or on another reachable host. ...

Get Professional Node.js: Building Javascript Based Scalable Software now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.