Thursday, August 15, 2019

How to connect Nodejs to Mysql

Today i w'll introduce for you How to connect Nodejs to Mysql, at the moment have verry many database ( Oracle, SQL, MoongDB, SQLServe ...), me will introduce all, but at this post me instructions for your connect to mysql in Nodejs how, to do this then you please do step by step below.


Step 1. Create app in angular

ng new Angular-mysql
Step 2. install libary Mysql in Angular two way
if you at use yarn then install libary mysql use yarn
yarn add mysql
also you can install npm
npm install mysql --save
So you have installed the Mysql database in angular successfully, at Node.js can use this module to manage the MySQL database.
Step 3. Create a connection in server.js
Please open project created, after create one file inside the root of the project called server.js
const mysql = require('mysql');
const con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});
con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});
note: please input password if you have
Make run code
node server
If your local MySQL database credentials are right then you can connect your MySQL database with the Node.js application and in your terminal, you can see that we have connected our node app to the MySQL database.
How to connect Nodejs to Mysql

Step 4. Query a Database in Angular with nodejs
We can use the SQL statement to read from the database MySQL or write new data to the database. This is also called a database query. We have created connection objects are created in the example above and the object has a method to query the database.
Until now, we have connected our application button with mysql database, but now we will create a database in MySQL using the query. So let us see how we can create a new database.
open file server.js coppy code below after pase
con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  let sql = 'CREATE DATABASE angular-mysql';
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("The Database is created!!");
  });
});
Name database angular-mysql you can change name database this your, please save file if databse not create then us will create a database, but
if it has created you a error
Step 5. Create Table in angular with Mysql
To create tables in MySQL, we can use the CREATE TABLE statement of CRE. You should ensure that you specify the name of the database when you create the connection. So we need to add a parameter, while we are connecting with their Node application database MySQL.
After this you change fuction connect in file server.js above
const con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "angular-mysql"
});
Meaning you just need ti add database in function connect, the next write function create table in file server.js below
con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  let sql = `CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))`;
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("The customers table is created!!");
  });
});
This is it for the How To Connect Nodejs Application To MySQL Database. Thanks verry much.
result image below:
connect Nodejs to Mysql

3 comments:

  1. Thanks for sharing this useful content. How to create stored procedure in mysql with example and 5 Career Pathways with an ITIL service transition certification Mysql Database

    ReplyDelete
  2. Thanks for sharing this useful Content. BMI is an acronym that stands for Brain-Machine Interface also Known as Brain-Computer Interface(BCI).

    ReplyDelete
  3. Thanks for sharing this useful Content. BMI is an acronym that stands for Brain-Machine Interface also Known as Brain-Computer Interface(BCI).


    put a chip in my head

    ReplyDelete