Connecting Node.js to a MySQL Database | Node.js Connect MySQL with Node app

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will connect our load.js application with my SQL so to connect that so the first thing is that we should already install node.js okay I have already installed that so to check that just come to the command prompt and write node version okay it will give you okay it is installed 16 version so after that you should have installed the MySQL and your system okay my SQL database because we are connecting our new touches with my secret so after installing these two now we will come here and our write our code so for that I will open a folder which I updated this okay node with my SQL yeah the folder has opened now I will create a folder here inside this the name of that would be backend [Music] inside this backend we will write our backend code okay so for that we need the first thing to initialize with it with npm so I will write something like this I will go to the directory app backend so after that I will write npm init hyphen y after that it will create a package.json file inside backend folder okay it has created this folder so now we need to run our packet node.js with my secret we need these things okay the first thing is to install my SQL npm install my Ctrl after that the express Express is the a framework for node.js okay to write a code in express.js and also we can install node month node mod is used for to refresh our server okay this backend it will refresh whenever we make change to our server it will refresh that by default it will run it by default to magical and also the course the cards are used really we would like to access our node.js API in our front end so then we will use this course okay so let's install this so these packages will install now so let's wait for it yes the package has installed here so now we will create a folder here the name of the folder would be server.js okay here in this file we will write our code after writing this so let's come here and write our code so for that first of all we will import the express accounts Express is equal to require after that we will import our my sequence required execute to use node main so for that we will come to package.json and here under the script we will write something like this start it will start automatically our server.js file and here now we will just create an update R is equal to called the express now we will use our app to for example up that listen so first we will assign a port number to this the port number would be 8081 our server will listen at this port number and the second thing is that let's run a console.log listening for example okay so in Also let's create a connection to create the connection we will write because DB is equal to mySQL dot create connection so here we will pass our properties will be first would be the host the host is the Local Host with us second one is the user the user is the root filters and the third one is the password you didn't set the password for our my city and the fourth is that database database the database name is the correct okay so let's check our database I am using sample server MySQL so for that I will learn localhost PHP my admin to go to my secret database okay I'm in the mySQL database here let's come to the cart it is a database name okay and in the cloud we have a student table we will access the data of student yeah the correct database so let's first of all run this okay so to run this I will just start right npm start it is it has started listing Google this video has printed for us okay now let's write in API okay for example we would like to access the data from my SQL database okay so for that let's write up dot it we will get the value the URL would be that would be for example student and here we will be a compact function in that we will get a request in it now we will write our query so to write the query we will like cons is equivalent is equal to delete State from table okay it will select our rows data from the student table in credit database after that we will operate the execute the query [Music] which we have created okay this one a b dot key we will pass our query first of all and after that we will get an error our data these two properties parameters so if we got error so we will print that paper there was an error so return result dot a response.json for example just print as the error otherwise the return as response.json the data okay so now let's access this student let's come here and we will learn our server voltage is listening and port number 8081 so I think we assigned that okay yeah 8081 code it's like localhost and here we will like 80 81 so let's move to the student we got the data okay we have the data ID name and email let's check our student table see we have that data here once we got the data here it was a simple okay we did connection with our mySQL database using node.js so I hope this video was helpful for you if you like this video please subscribe our YouTube channel and thanks for watching
Info
Channel: Code With Yousaf
Views: 7,006
Rating: undefined out of 5
Keywords: how to connect to mysql database from node.js, node.js, database, how to connect to mysql database, nodejs, how to connect mysql from nodejs, nodejs + mysql databse connection tutorial, database connection, how to create mysql database with node, connecting to mysql from nodejs application, node.js database connectivity, mysql database connection in node.js, database connection in nodejs, node.js database connectivity setup, database connectivity using nodejs
Id: JWia8kUPrRs
Channel Id: undefined
Length: 8min 12sec (492 seconds)
Published: Sat Mar 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.