Using MySQL With Node.js
HTML-код
- Опубликовано: 14 окт 2024
- In this quick guide I will show you how to use MySQL along with Node.js. This is something I am constantly asked by LAMP stack devs wanting to learn Node.js. We will be using the MySQL JavaScript client along with Express and Node.
CODE: Code for this video
www.traversymed...
BECOME A PATRON: Support me directly for even $1 per month
/ traversymedia
ONE TIME DONATIONS:
www.paypal.me/t...
FOLLOW TRAVERSY MEDIA:
/ traversymedia
/ traversymedia
/ traversymedia
EDUONIX COURSES: Please use affiliate links from website below
www.traversymed...
Guys I apologize for the out of sync mini-cam toward the middle/end. I'm working on fixing that in my recording software. Hope you enjoy the video!!
Hi Brad, thanks for this video. I have a request in countinuation this video. I would be more than happy to pay for that course. A detailed course on Sequilize ORM for Node teaching basics of Sequilize and also Database Design using Sequilize. There are not much great resources on it and it would be great to have a course from you. Please consider this once :-)
just came here to press a like, u deserve it man :) good job
I will most likely do a youtube video but I am also considering a node.js course with a detailed project using sequelize
Thank you bud :)
Thanks
I've gone through dozens of videos on this topic, I was finally able to get this going through this; you're very clear and concise. I mean, thanks to all the others for adding to my knowledge base, but it goes a long way to create a video where someone who can speak clearly and knows what they are doing, without wasting time.
Thank you.
This is exactly what I think every time I want to learn something new. Brad is one of the few that is genuine, not overly and grossly enthusiastic, explains clearly, and is even funny at times which helps ease the learning process. He has a natural ability to make learning fun in this category.
True
3 hours spend looking for an example of adding parameters at request, finally an example that works, really helpful
I need a help. So I made a login page which works fine when input is correct. But in case if the input goes wrong like either the password or username, it throws error and the server crashes. I don't want that. Instead I want that error to be handled someway but I ain't able to find any way. Please help me with this. I'd really appreciate if you can please.
00:00 Intro
01:40 Install XAMPP
03:09 Create app & Install dependencies
07:03 Create connection
08:12 Connect to MySql
09:30 Create database
12:36 Create table posts
16:56 Insert data to posts
19:58 Select posts
21:10 Select single post
23:05 Update post
24:55 Delete post
Thank you Brad. Why is it that you seem to be the only guy on the internet who can explain things so that they're easy to understand, easy to implement and don't waste your time? You helped me so much (with your Udemy courses and RUclips videos) I cannot thank you enough. You rock :)
i regret not learning node backend development a few years ago, and sticked with laravel and codeigniter as the companies i was working for had projects in those. Thank you for this Brad. Your NodeJS Tutorials are very easy to get and understand. Thank You.
no words for traversy media i cant imagine that much information in this duration very clearly explained hats off man from india
Was just building an express mysql2 api just for fun to practice and forgot how to use "?" for escape characters in queries to prevent sql injection. Fast forwarded to 17:30 and got my answer. Thanks TraversyMedia. I am now a professional web dev working on a contract with the DoD and your RUclips channel really helped me to become the web developer that I am today.
Every time I see this Traversy Media intro I know something good is coming...
Great video. I was coming from mssql and had to find this specific video on how to link node.js with MySQL. Spot on. Thanks for the video, really helpful and well executed. Simple and easy, no messing around.
Very good tutorial. Some updates(2022) would be to use mysql2 instead of mysql, and to add a port (usually 8889) if you are using MAMP. Thank you Brad
Thanks Brad for consistently delivering awesome tutorials that are precise, packed with tons of meat and no fluff. Great stuff bruv
I need a help. So I made a login page which works fine when input is correct. But in case if the input goes wrong like either the password or username, it throws error and the server crashes. I don't want that. Instead I want that error to be handled someway but I ain't able to find any way. Please help me with this. I'd really appreciate if you can please.
this is some of the channel on which i don't skip ads...for now this is the best i can do to help this channel. and i am commenting while that long ass ad is playing. :)
That's plenty. Thank you. I know ads really suck. I do the same thing though for all the creators I watch and enjoy. Especially if you are really liking the content and you just want to hurry up and skip. People like you are the reason I do this, it shows your appreciation and boosts my drive to create more free content :)
I tried your video using mamp.
At first I couldn't access the db, but in the case of the mamp I changed the password to 'root' and it worked. I learned a lot.Thanks from Japan.
Those who are getting "ER_NOT_SUPPORTED_AUTH_MODE" error in the DB connection, try the newer NPM package "mysql2". It supports advanced authentication that MySQL 8.0 requires.
Here's the NPM page link: www.npmjs.com/package/mysql2
Thanks!
thanks a bunch🙏
don't know why people are using mongodb or postgre sql in tutorial of nodejs with expression.
this tutorial simply show how to connect with database (mysql). it is very very helpful.
thanks a lot.
I was working on a project and was about to give up and this guy just made me not to.
Thanks bro
Really straightforward and clear from the beginning! Thank you for doing this video
I need a help. So I made a login page which works fine when input is correct. But in case if the input goes wrong like either the password or username, it throws error and the server crashes. I don't want that. Instead I want that error to be handled someway but I ain't able to find any way. Please help me with this. I'd really appreciate if you can please.
Looked everywhere for something simplified...you made it much easier..thumbs up!
You are the best web development teacher on RUclips. Thank You Sir
I'm learning a lot of English and a lot of web development with this video. This was very useful, thanks a lot.
Such a chill, to the point tutorial... absolutely brilliant, lots of love from confused front-end devs
This is really good. You know it oops at number one on the search... You really need to make an update on this... Its been six years now... You know many things must have changed overtime
I have had problems with the update because of the ID of my table data, thanks 4 explaining it clearly 👌
Oh what an introduction. To be frank, I was almost giving up but this made my learning seamless and easy. I can now CRUD using node. Thanks
In ES6, when you have one function parameter, you can delete the parentheses which makes the code cleaner in my opinion :)
db.connect( err => {
if (err) { throw err }
});
will still work.
Though one could argue that the parentheses shows clearly that it's a group of arguments, also it you want to use rest operator then you need to add parentheses.
Sometimes you have to think how does it look to someone who's never seen your code and how easy is it to reason about your program.
But I also prefer the cleaner look, but just wanted to show a reason why keeping it might be better, at-least if you are working within a team.
It does kinda throw me a bit when there is no parenthesis around the argument, no pun intended!
Congratulations, man. I am from Brazil and I love your tutorials.
Okay, so after a long day and a half, here's what I did to get this working. Hoping it helps anyone who struggled at it like i did.
First, it's important to have your MySQL configuration set to legacy for password values as the connection from node kept throwing an error due to protocol mismatch. Once i reconfigured MySQL to the legacy, it finally connected.
He doesn't cover installing Bash, so do an npm install bash after you install Terminal, then click the plus sign on the terminal to get the bash terminal.
Once you do these things, following this tutorial is easy and does what it is supposed to do. Thank you
finally have found the best explanation. And get the code too so i dont have to rewrite everything. Thanks
Dude, I’m a beginner but I love these videos. Big shoutout to the person for not blasting me out with intro music too’
Thanks for helping me with my web programming homework, hugs from Brazil!
This is the most helpful and easy-to-understand video. Thanks, brad.
Watched a few videos and this is definitely the best and easiest to understand and follow
Wow, very informative, thanks! I watched this video to see if it would show me what I need to begin building a project, and it gives me what I need *and more*, so I'll definitely be rewatching.
One of the few channels i have subscribed
I am using MAMP (not pro) and I had a hard time tracing the cause the error it was giving me when db.connect( ) runs. The solution I found was to enter the following argument into createConnection( ) :
port: '/Applications/MAMP/tmp/mysql/mysql.sock',
Hope this helps somebody!
Also, Traversy, once beautifully concise video as always!
thats a socket not a port
Great video. all of the code works fine. It's Still relevant in 2023!
Love the practical nature of your videos.
Hey Brad,
love your work keep up the good stuff!!
mabby in one of your future projects you can make a video on how to create and publish a react component library, would really like to learn more on that.
Greets Julian
Thank you man, you made me believe I can still do it..!
Is the SET ? syntax a part of MySQL itself? Or is that something that is part of the Node MySQL libary?
Every project I can think of you have a video for thank so much for your hard work
CREATE DATABASE IF NOT EXISTS will be more suitable example if the task is to create db from code
24:30 - line 87 - that's a nice candidate for SQL injection :)
He is concatenating SQL query with string he receives from user request. So if user in that requests sends malicious SQL query it will get executed. This vulnerability basically allows you (as a hacker) to do anything with target database.
Fortunately you may use "Escaping query values": github.com/mysqljs/mysql#escaping-query-values
So in this case the query may look like this: let sql = 'UPDATE posts SET title = ' + connection.escape(newTitle) + ' WHERE id = ' + connection.escape(req.params.id) +';
thank you
Use parameterized query. blogs.msdn.microsoft.com/sqlphp/2008/09/30/how-and-why-to-use-parameterized-queries/
maybe better look like this:
let sql = 'update posts set title='+ db.escape(newTitle) +' where id='+ db.escape(req.params.id) + '';
This video is still very useful!
Nosql is so fun. Sql needs so much structuring beforehand
You explain these things well. Thank you very much.
Lots of thanks for providing such a good content :)
Thank you so much for your video. It was really helpful for me. I was having hard time in a web project. then your video saved my life.
This is really very helpful I learned it so fast bcoz you taught it so well❤️❤️
Thank youuuu ! couldn't figure how to show the output in my browser from MySQL/Node. Thanks a lot!!!
Awesome!! Easy to understand, clear, and the best intro to understanding Node with mysql. thanks alot!!
Thank you so much! This was a hugely helpful tutorial. Keep up the great work!
You are really awesome bro ❤
Always you are my guru
Laravel and node I have learned from you 😍
Thanks alot. This was exactly what I was looking for!
Thanks, Travisscott Media!
good content brad really enjoy your crash courses and your series thank alot brad
I really liked this. I always refers to this MAN's videos ( Traversy Media) for Node, MySQL , Mongo..etc....
To be honest you helped me understand express framework!!! Well done!!!
Travis why are you using app.get to post instead of app.post? and to update you use get instead of put? and last thing, should not be better to use the "?" in the update to avoid sql injection?
Basically, the reason for the get is because my goal of the video was to display how the MySQL driver works. I guess I kind of guessed that if people are using node they know not to do this but I probably should have mentioned it if I didn't.
Oh, yes you are right, i was only saying that just cause maybe you missed, or maybe you did it for a specific reason.
your right this does display how the mysql driver doesnt really care about the type of request it is getting and mysql is not complaining when its getting the actual code in sql. but i would mention it when there is and actual user interface and the request is coming from a form.
I LOVE your videos man! I'm getting comfortable with web technologies and your channel is really helping. Thanks for the videos! A request if you ever find time... Joomla as a Framework (not CMS)! Thanks again!
Really great video! I've twisted the code a little bit to send the results of the query to the screen like 'res.send(results)' for a better development experience! Thanks a lot
Hey Brad, Can you please share your work desk photos? I would love to have a look please. Also do you prefer desktop or laptop?
Great!, i just got into node js and this is a good start for me . Thank you
Can you do a user management tutorial with user and secure admin features in node.js/Angular 4/mongoDB? If you have done this course could you point me in the right direction? You're courses are awesome!
Chris Campbell yes please do this
Thank you Brad for this tutorial
excatly what needed for my DBMS project ✍
Brad, thanks so much for this video. It really helped me understand things easier.
Hi Brad. Great video as always. Have you switched to Visual Studio Code from Atom? How do these two compare in your opinion? :)
I use both often. What I usually do is use Atom for frontend html/css/sass and I use vscode for Angular/React/Node/Other backend. I love the integrated terminal. I do think vscode is faster as well. I plan on making a comparison video soon
Thanks ! That would be greatly appreciated!
This was extremely helpful. Thanks for the tutorial.
you are awesome sir your all videos always very helpful thank you
Thank You So Much Man as a Newbie this Video Changed my Life, Lots of Love Bro
it was a nice tutorial. Just a question that if i want to use pre install mysql on my system, will node js be able to connect to that.
Do you have any examples about shopping cart on nodeJs and mysql?
This is such an odd way to do it, but I get that you're trying to teach the concept.
Awesome stuff.
Thank you Brad :)
Very clean and quick guide, thank you!
Thanks so much for this tutorial, I was wondering how to work on connected tables, I am building an exam app where there will be table for type of exam and questions related to that specific exam type, how do I make this kind of connection.. thanks so much for the video 🙌
Good tuitorial , do you have any examples on Oracle with Node js
Thanks for this. I have a new task that requires me to extract data from Observium's Billing table and push that to a NodeJS API endpoint.
Thanks Brad! Very helpful. One thing I really don't understand is the adding post part from min 17:40. Why is this hard coded and not dynamically added through postman or the url input field? Anyone an answer? Thanks a lot!
Nice tutorial! Easy to understand & implement! Thank you!
I need a help. So I made a login page which works fine when input is correct. But in case if the input goes wrong like either the password or username, it throws error and the server crashes. I don't want that. Instead I want that error to be handled someway but I ain't able to find any way. Please help me with this. I'd really appreciate if you can please.
Thanks a lot Brad, exactly what i was looking for. You are Super !
I need a help. So I made a login page which works fine when input is correct. But in case if the input goes wrong like either the password or username, it throws error and the server crashes. I don't want that. Instead I want that error to be handled someway but I ain't able to find any way. Please help me with this. I'd really appreciate if you can please.
Well explained, nice tutorial. I just have one question, aren't we supposed to use app.delete when we are making delete query instead of app.get? Same goes for app.put or app.patch on update query instead of app.get.
Yes. I think he overlooked this because it is more an SQL tutorial rather than a REST api tutorial.
Excellent video, Brad.
I have a question. What does the query return?
I see RowDataPacket. But what is that? A list of objects? List of lists?
Thank you.
A list of objects.
Nice tutorial your awesome but I have a question what is difference between using sequelize and mysql
thanks a lot man such a precise tutorial
hey, I taking ur course on udemy full stack web development. good to see you here.
For ones who had problem connecting with db, around 11:30, password could be just empty string instead of 123456
Hi and thank you for your awesome video tutorials, you are one of my most favourite tutors :)
+1
I need a help. So I made a login page which works fine when input is correct. But in case if the input goes wrong like either the password or username, it throws error and the server crashes. I don't want that. Instead I want that error to be handled someway but I ain't able to find any way. Please help me with this. I'd really appreciate if you can please.
Amazing video man! Thank you very much. Could you tell me how to send an object by params instead of only the id? Thanks again
Thank you Brad this is what i m waiting for
very nicely & quickly explained
Thanks alot I'm from Iraq I'm follow your courses is the best continues
Thanks man, that's awesome I'm able to help people all around the globe :)
This was extremely helpful, thank you! Keep up the great work.
Brad ! Whats the best framework to work with APIs, Django or JS frameworks with node ????
I love your tutorials
Brad, you're great.
Regarding NODE, why not doing something for Microsoft SQL.
Have used both MSSQL and SERIATE libraries and would be nice to have your opinion. (I personally prefer the second one because I can use query with parameters)
Many thanks
Will this work on Ionic? I'm new to these node.js frameworks, but I'm trying to make an app with a questionnaire, and need to store data in a database. (preferably in mySQL).
Sir you used very simple technique for inserting and updating data, is there any similar technique in php or .net too?
Гоуем си, брат! (You are big one, brother (брат == brother, Brad is similar to Брат and sounds the same in my language - Bulgarian :) ) You are the best, really! Bless you! Have a great and lovely new year!