BUILD not KILL your business.

There are many reasons why businesses close but the common thread throughout these failed businesses is that the business owner and/or operator were overworked, overwhelmed and over-stressed which…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Connecting to MySQL in NodeJS

How to connect your MySQL database to your NodeJS application? It’s very simple. Don’t believe me? Here’s how you can do it.

First, create a folder in your Node application direction named config. Now, this can be of any name of your preference.

Inside this folder, create a file named database.js. This file will contain our database configuration and connection.

Now, there are 2 ways in which you can create a database connection in NodeJS. The simple connection and a pool connection. I prefer the latter because of a few reasons which I am going to explain later.

Creating a simple connection to MySQL database using NodeJS

Open the file database.js

Let’s discuss a bit about what we did in the above code.
First, we need to require a module named mysql. To install it, simply use

Now, we need to create our database configuration.
For this, we are going to use createConnection function of mysql module.
We are going to pass an object as function parameter. The object will contain the host, i.e. basically your database IP or domain name or localhost if we are trying to connect to our local database. Now, the user and password will be the username and password of your database which is used to log into your MySQL database. The database will be the name of the database we want to access. and Port by default is 3306 for MySQL.

To connect to our database, we are going to use the connect method which on success will create a connection to our database.

This was a simple database connection. Now, this type of connection is fine when we are not dealing with multiple connections at the same time. For heavier workloads, we use connection pooling.

We can use pooling directly to handle multiple connections and reuse the connection. This helps us handle multiple requests to our server at the same time without causing any delay.

For connection pooling, we are going to use the following code snippet.

Now for pool connection, we use more or less the same configuration as used in a simple connection above. We only use a connectionLimit, i.e. number of connections that are allowed at a time.

Also, instead of createConnection function, we use createPool function.

This is how we connect our MySQL database to our NodeJS application.
I hope this helps you to understand which connection to use in your application and help you connect to your MySQL database.

Add a comment

Related posts:

Dating After Your Twin Flame

To be loved intimately is like standing directly underneath a waterfall. Rocks placed underneath a waterfall are slowly worn over time and we would too if we stood there for many years. We can feel…

More from Sisterly HQ

To make sense of Web 3.0, it is important to clarify a common misconception. While most people believe web 3.0 is one single idea, it is not. Instead, it is a big concept with a bunch of little…

Nobita Ragu Jelang Pernikahan dengan Shizuka Stand by Me Doraemon 2

Nobita ragu jelang pernikahan dengan shizuka di Stand by Me Doraemon 2. Sudahkah kamu menonton filmnya? Keraguan yang dirasakan oleh Nobita itu nyata. Efek dari kelelahan yang berdampak pada emosi…