I've just started getting into Node.js. I come from a PHP background, so I'm fairly used to using MySQL for all my database needs.
How can I use MySQL with Node.js?
I've just started getting into Node.js. I come from a PHP background, so I'm fairly used to using MySQL for all my database needs.
How can I use MySQL with Node.js?
You can also try out a newer effort known as Node.js DB that aims to provide a common framework for several database engines. It is built with C++ so performance is guaranteed.
Specifically you could use its db-mysql driver for Node.js MySQL support.
Here is production code which may help you.
Package.json
Here is Server file.
Reference : https://codeforgeek.com/2015/01/nodejs-mysql-tutorial/
Since this is an old thread just adding an update:
To install the MySQL node.js driver:
If you run just
npm install mysql
, you need to be in the same directory that your run your server. I would advise to do it as in one of the following examples:For global installation:
For local installation:
1- Add it to your
package.json
in the dependencies:2- run
npm install
Note that for connections to happen you will also need to be running the mysql server (which is node independent)
To install MySQL server:
There are a bunch of tutorials out there that explain this, and it is a bit dependent on operative system. Just go to google and search for
how to install mysql server [Ubuntu|MacOSX|Windows]
. But in a sentence: you have to go to http://www.mysql.com/downloads/ and install it.connect the mysql database by installing a library. here, picked the stable and easy to use node-mysql module.
For NodeJS mysql connecting and querying example
KnexJs can be used as an SQL query builder in both Node.JS and the browser. I find it easy to use. Let try it - Knex.js
You can use it like this
or