Node.js Tutorial
- Node.js Tutorial
- Install Node.js on Windows
- Install Node.js on Linux/Ubuntu/CentOS
- Node.js First Example
- Node.js Console
- Node.js REPL
- Node.js Package Manager
- Node.js Command Line Options
- Node.js Global Objects
- Node.js OS
- Node.js Timer
- Node.js Errors
- Node.js DNS
- Node.js Net
- Node.js Crypto
- Node.js TLS/SSL
- Node.js Debugger
- Node.js Process
- Node.js Child Process
- Node.js Buffers
- Node.js Streams
- Node.js File System (FS)
- Node.js Path
- Node.js StringDecoder
- Node.js Query String
- Node.js ZLIB
- Node.js Assertion Testing
- Node.js V8
- Node.js Callbacks
- Node.js Events
- Node.js Punycode
- Node.js TTY
- Node.js Web Module
- NestJS
Node.js MySQL
Node.js MongoDB
Nodejs Difference
Node.js MCQ
Node.js Express
Nodejs Interview Questions
Node.Js Create Connection with MySQL
We can use Node.js in database applications. Here we use MySQL as a database with Node.js.
Install MySQL on your computer.
You can download it from here https://www.mysql.com/downloads/.
Once the MySQL is installed and running, you can access it by using Node.js.
Install MySQL Driver
You have to install MySQL driver to access a MySQL database with Node.js. Download MySQl module from npm.
To download and install the "mysql" module, open the Command Terminal and execute the following:
- npm install mysql
Create Connection
Create a folder named "DBexample". In that folder create a js file named "connection.js" having the following code:
- var mysql = require('mysql');
- var con = mysql.createConnection({
- host: "localhost",
- user: "root",
- password: "12345"
- });
- con.connect(function(err) {
- if (err) throw err;
- console.log("Connected!");
- });
Now open the command terminal and use the following command:
Node connection.js
Now Node.js is connected with MySQL.