NoSQL databases are all the rage these days and probably the preferred back-end for Node.js applications. But you shouldn't architect your next project based on what's hip and trendy, rather the type of database to be used should depend on the project's requirements. If your project involves dynamic table creation, real time inserts etc. then NoSQL is the way to go, but on the other hand, if your project deals with complex queries and transactions, then a SQL database makes much more sense.
In this tutorial, we'll have a look at getting started with the mysql module — a Node.js driver for MySQL, written in JavaScript. I'll explain how to use the module to connect to a MySQL database, perform the usual CRUD operations, before examining stored procedures and escaping user input.
This popular tutorial was updated on 11.07.2017. Changes include updating to ES6 syntax, addressing the fact that the node-mysql module module was renamed, adding more beginner friendly instructions and adding a section on ORMs.
Quick Start: How to Use MySQL in Node
Maybe you've arrived here looking for a quick leg up. If you're just after a way to get up and running with MySQL in Node in as little time as possible, we got you covered!
Here's how to use MySQL in Node in 5 easy steps:
- Create a new project:
mkdir mysql-test && cd mysql-test
- Create a
package.json
file:npm init –y
- Install the mysql module:
npm install mysql –save
- Create an
app.js
file and copy in the snippet below. - Run the file:
node app.js
. Observe a “Connected!” message.
//app.js
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'database name'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
Installing the mysql Module
Now let's take a closer look at each of those steps. First of all we're using the command line to create a new directory and navigate to it. Then we're creating a package.json
file using the command npm init –y
. The -y
flag means that npm will use only defaults and not prompt you for any options.
This step also assumes that you have Node and npm installed on your system. If this is not the case, then check out this SitePoint article to find out how to do that: Install Multiple Versions of Node.js using nvm.
After that, we're installing the mysql module from npm and saving it as a project dependency. Project dependencies (as opposed to dev-dependencies) are those packages required for the application to run. You can read more about the differences between the two here.
mkdir mysql-test
cd mysql-test
npm install mysql -y
If you need further help using npm, then be sure to check out this guide, or ask in our forums.
Getting Started
Before we get on to connecting to a database, it's important that you have MySQL installed and configured on your machine. If this is not the case, please consult the installation instructions on their home page.
The next thing we need to do is to create a database and a database table to work with. You can do this using a
graphical interface, such as phpMyAdmin, or using the command line. For this article I'll be using a database called sitepoint
and a table called employees
. Here's a dump of the database, so that you can get up and running quickly, if you wish to follow along:
CREATE TABLE employees (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50),
location varchar(50),
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO employees (id, name, location) VALUES
(1, 'Jasmine', 'Australia'),
(2, 'Jay', 'India'),
(3, 'Jim', 'Germany'),
(4, 'Lesley', 'Scotland');
Connecting to the Database
Now, let's create a file called app.js
in our mysql-test
directory and see how to connect to MySQL from Node.js.
Continue reading %Using MySQL with Node.js & the mysql JavaScript Client%
by Jay Raj via SitePoint
No comments:
Post a Comment