# Connecting with Node.js

This guide explains how to establish a connection between a Node.js application and a MySQL database using the `<span class="s3">mysql2</span>` package. It walks through the necessary setup, configuration, and execution of a simple SQL query.

## **Variables**

Certain parameters must be provided to establish a successful connection to a MySQL database. Below is a breakdown of each required variable, its purpose, and where to find it. Here’s what each variable represents:

<table border="1" id="bkmrk-variable-description" style="border-collapse: collapse; border-color: rgb(0, 0, 0); width: 100%; height: 262.766px;"><thead><tr style="height: 29.7969px;"><th style="border-color: rgb(0, 0, 0); width: 10.0102%; height: 29.7969px;">**Variable**

</th><th style="border-color: rgb(0, 0, 0); width: 40.4069%; height: 29.7969px;">**Description**

</th><th style="border-color: rgb(0, 0, 0); width: 49.5828%; height: 29.7969px;">**Purpose**

</th></tr></thead><tbody><tr style="height: 46.5938px;"><td style="border-color: rgb(0, 0, 0); width: 10.0102%; height: 46.5938px;">`USER`

</td><td style="border-color: rgb(0, 0, 0); width: 40.4069%; height: 46.5938px;">MySQL username, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%; height: 46.5938px;">Identifies the database user who has permission to access the MySQL database.

</td></tr><tr style="height: 46.5938px;"><td style="border-color: rgb(0, 0, 0); width: 10.0102%; height: 46.5938px;">`PASSWORD`

</td><td style="border-color: rgb(0, 0, 0); width: 40.4069%; height: 46.5938px;">MySQL password, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%; height: 46.5938px;">The authentication key is required for the specified USER to access the database.

</td></tr><tr style="height: 46.5938px;"><td style="border-color: rgb(0, 0, 0); width: 10.0102%; height: 46.5938px;">`HOST`

</td><td style="border-color: rgb(0, 0, 0); width: 40.4069%; height: 46.5938px;">Hostname for MySQL connection, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%; height: 46.5938px;">The address of the server hosting the MySQL database.

</td></tr><tr style="height: 46.5938px;"><td style="border-color: rgb(0, 0, 0); width: 10.0102%; height: 46.5938px;">`PORT`

</td><td style="border-color: rgb(0, 0, 0); width: 40.4069%; height: 46.5938px;">Port for MySQL connection, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%; height: 46.5938px;">The network port used to connect to MySQL. The default port is 3306.

</td></tr><tr style="height: 46.5938px;"><td style="border-color: rgb(0, 0, 0); width: 10.0102%; height: 46.5938px;">`DATABASE`

</td><td style="border-color: rgb(0, 0, 0); width: 40.4069%; height: 46.5938px;">Database Name for MySQL connection, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%; height: 46.5938px;">The name of the database being accessed. A MySQL instance can contain multiple databases.

</td></tr></tbody></table>

These values can usually be found in the Elestio service overview details as shown in the image below, make sure to take a copy of these details and add it to the code moving ahead.

[![rivimage.png](https://docs.elest.io/uploads/images/gallery/2025-04/scaled-1680-/mP2rivimage.png)](https://docs.elest.io/uploads/images/gallery/2025-04/mP2rivimage.png)

## **Prerequisites**

- **Install Node.js and NPM**
    - Check if Node.js is installed by running: `node -v`

- - If not installed, download it from [nodejs.org](https://nodejs.org) and install. Additionally, verify npm installation: `npm -v`

- **Install the mysql2 Package**
    - The <span class="s2">mysql2</span> package enables Node.js applications to interact with MySQL. Install it using: `npm install mysql2 --save`

## **Code**

Once all prerequisites are set up, create a new file named `<span class="s2">mysql.js</span>` and add the following code:

```javascript
const mysql = require("mysql2");

// Database connection configuration
const config = {
  host: "HOST",
  user: "USER",
  password: "PASSWORD",
  database: "DATABASE",
  port: PORT,
};

// Create a MySQL connection
const connection = mysql.createConnection(config);

// Connect to the database
connection.connect((err) => {
  if (err) {
    console.error("Connection failed:", err);
    return;
  }
  console.log("Connected to MySQL");

  // Run a test query to check the MySQL version
  connection.query("SELECT VERSION() AS version", (err, results) => {
    if (err) {
      console.error("Query execution failed:", err);
      connection.end();
      return;
    }

    console.log("MySQL Version:", results[0]);

    // Close the database connection
    connection.end((err) => {
      if (err) console.error("Error closing connection:", err);
    });
  });
});
```

To execute the script, open the terminal or command prompt and navigate to the directory where <span class="s1">mysql.js</span> is located. Once in the correct directory, run the script with the command:

```
node mysql.js
```

If the connection is successful, the terminal will display output similar to:

```
Connected to MySQL
MySQL Version: { version: '8.0.41' }
```