Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL with Node.js

Tags:

node.js

mysql

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?

like image 562
crawf Avatar asked Apr 28 '11 12:04

crawf


People also ask

Can we use MySQL with node js?

Once you have MySQL up and running on your computer, you can access it by using Node. js. To access a MySQL database with Node. js, you need a MySQL driver.

Can we use SQL database with node js?

Yes, it's true. You can build Node. js applications with SQL Server! In this tutorial, you will learn the basics of creating a Node.

Which database is best with node js?

js supports all kinds of databases no matter if it is a relational database or NoSQL database. However, NoSQL databases like MongoDb are the best fit with Node. js.


1 Answers

Check out the node.js module list

  • node-mysql — A node.js module implementing the MySQL protocol
  • node-mysql2 — Yet another pure JS async driver. Pipelining, prepared statements.
  • node-mysql-libmysqlclient — MySQL asynchronous bindings based on libmysqlclient

node-mysql looks simple enough:

var mysql      = require('mysql'); var connection = mysql.createConnection({   host     : 'example.org',   user     : 'bob',   password : 'secret', });  connection.connect(function(err) {   // connected! (unless `err` is set) }); 

Queries:

var post  = {id: 1, title: 'Hello MySQL'}; var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {   // Neat! }); console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL' 
like image 169
mak Avatar answered Sep 21 '22 12:09

mak