DEV Community

loading...

Can I use MySQL with Node.js?

Pan Seba
Sebastian specializes in web dev and is a certified Linux admin. He loves JS and Node.js. He works as a backend lead and is responsible for architecture design and development of reliable solutions.
・4 min read

For far too many times have I heard that question and far too many times have I heard someone saying that it’s not a popular combination or questioning this choice. It’s time to finally release my frustration and dispel any doubts on this topic.

Here’s what we are going to cover in this article:

  • Why MySQL may look unnatural for Node?
  • What database should you choose when working with Node?
  • Can I use MySQL with Node?

The aim of this article is to explain the common misconception about using MySQL with Node.js and to assure you that there is completely nothing wrong with using this database together with Node.js. Let’s get started.

Why MySQL may look unnatural for Node?

I think the whole uncertainty of choosing MySQL for Node.js projects comes from the conviction that when you are using Node and want to store your data in a database then you should pick MongoDB. Why is that? Well, I think we need to get back in time to 2013 when the acronym MEAN was coined.

What is MEAN and what does it stand for? It is a well-known JavaScript software stack for building full-stack web applications. It stands for:

MongoDB
Express.js
Angular
Nodejs

All of these components are technologies supporting programs written in JavaScript. MongoDB is a non-relational database for storing document-oriented data. Express.js is a web framework designed for Node.js. And Angular is a frontend framework created for building dynamic web applications. All of them support JS and therefore when choosing this stack you can build the whole system using a single programming language. It’s the essence of the „JavaScript everywhere” paradigm.

Because knowing just a single programming language enabled developers to build the whole web apps the MEAN stack became very popular. As new JavaScript frameworks was created, new versions of this stack evolved. Today we have the three most popular JS frameworks and because of that we have three variations of the MEAN stack:

  • MEAN – the original stack with Angular as the main frontend technology
  • MERN – a variation using React as the frontend technology
  • MEVN – a variation using Vue as the frontend technology

Today, the MERN stack became even more popular than the original variation because React's popularity is skyrocketing.

There are dozens of free tutorials, courses and videos showing how to get started with these stacks. By googling any of these we can learn that using them you can build any web application with only one language – JavaScript.

All of this caused that today these stacks seem to be a natural combination for Node. This belief becomes stronger as new developers learn that it’s the only right way of building JS apps and share it further. MongoDB became a default choice for Node apps – especially for junior developers or people who are completely new to programming. Of course, this is wrong.

So what is the best option for Node? What should be your default choice when choosing a database for Node? Let’s find out.

What database should you choose when working with Node?

Let’s start with the fact that there is no such thing as a default database for any technology. That’s not how it works. You never should pick a database based on the technology that you use in your application. The reason is that similarly to the choice of backend and frontend technology it all depends on requirements. The project requirements should be your main deciding factor for database technology. On top of that, when your architecture is well-designed the kind of your database becomes a technical detail that does not have anything in common with programming languages used to create a backend.

Every database available on the market has its own advantages and will be the best choice for projects that it has been created for. Taking only the two databases that we are discussing here – MySQL and MongoDB – each of them should (and should not) be used in certain scenarios. If in your project you must deal with a large amount of data (like really large) or the data is unstructured or it has a potential for rapid growth then probably the better choice is to use MongoDB. On the other hand, if you have structured data or your priority is data security or you just need a relational database then MySQL is a lot better choice.

So let me repeat that again: there is no default database for any backend technology. It all depends on project requirements and what do you expect from the database. You should never be guided by the technology that you chose to create your backend.

Having said that, let’s finally answer the main question of this article.

Can I use MySQL with Node?

Yes! There is completely nothing wrong with that. I hope you realize this by now. The common misconception comes from the huge popularity of the ME[ARV]N stacks and the fact that MongoDB feels very compatible with JavaScript because of its JSON-like document-oriented structure and the query language that looks like JavaScript. However this is only the illusion – yes, in many cases MongoDB is the best option for Node.js but not always. In equally many scenarios the best option will be MySQL or any other database available in the market.

At the end of the day, we do have ORMs like TypeORM or Sequelize (or a dozen of others) that support MySQL drivers and they are very popular – it also means something.

To sum up, the most important thing to realize is that there is no such thing as a default database for Node.js (or any technology) and that you never should pick MongoDB just because you use Node. In fact, Node.js does not care about data storage really – just like Java or Python, Node.js is a backend technology used to implement server-side business logic. You should always choose the database that meets your particular project needs regardless of the chosen programming language.

With that in mind, go ahead and create a new Node.js project without worrying if MySQL will be a good choice.

Discussion (2)

Collapse
codefinity profile image
Manav Misra

Nice follow up would be NoSQL vs. SQL use cases.
NoSQL is simpler to setup and it's super fast for reads and most common things.
Also, recent improvements to MongoDB alleviate at least some concerns regarding ACID compliance.
So, what are use cases for MySQL?

Collapse
zohebkhan profile image
Zoheb Alli Khan

Awesome