> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connecting to MySQL using Node.js

> Discover how to set up a Node.js MySQL database connection with this concise guide featuring instructions, code snippets, and related resources.

This article demonstrates how to connect to a MySQL database using Node.js.

<Warning>
  **Important**

  * Node.js must already be installed on your account. For information about how to install Node.js, please see[this article](/docs/installing-and-configuring-nodejs-on-managed-hosting).

  * A MySQL database and user must already exist before you can follow the procedures in this article. For information about how to manage MySQL databases using cPanel, please see [this article](/docs/managing-mysql-databases).
</Warning>

## Connecting to MySQL using the node-mysql package

The *node-mysql* package enables you to easily connect to a MySQL database using Node.js. Before you can do this, however, you must install the *node-mysql* package on your account. To do this, follow these steps:

1. Log in to your account [using SSH](/docs/using-ssh-secure-shell).

2. Type the following commands:

```bash theme={null}
cd ~
npm install mysql
```

### Code sample

After you install the *node-mysql* package, you are ready to work with actual databases. The following sample Node.js code demonstrates how to do this.

In your own code, replace ***dbname*** with the database name, ***username*** with the MySQL database username, and ***password*** with the database user's password. Additionally, you should modify the SELECT query to match a table in your own database:

```javascript theme={null}
var mysql      = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    database: 'dbname',
    user: 'username',
    password: 'password',
});

connection.connect(function(err) {
    if (err) {
        console.error('Error connecting: ' + err.stack);
        return;
    }

    console.log('Connected as id ' + connection.threadId);
});

connection.query('SELECT * FROM employee', function (error, results, fields) {
    if (error)
        throw error;

    results.forEach(result => {
        console.log(result);
    });
});

connection.end();
```

This example creates a MySQL connection object that connects to the MySQL database. After the database connection is established, you can use the **query** method to run raw SQL statements (in this case, a *SELECT* query on a table named *employee* ).

## More information

For more information about the *node-mysql* package, please visit [https://github.com/mysqljs/mysql](https://github.com/mysqljs/mysql).

## Related articles

* [Connecting to MySQL from the command line](/docs/connect-to-mysql-from-the-command-line)

* [Connecting to MySQL using PHP](/docs/connect-to-mysql-using-php)

* [Connecting to MySQL using Python](/docs/connecting-to-mysql-using-python)

* [Connecting to MySQL using Perl](/docs/connect-to-mysql-using-perl)

* [Connecting to MySQL using Microsoft.NET](/docs/connecting-to-mysql-using-microsoft-net)

* [Managing MySQL databases, users, and tables from the command line](/docs/managing-mysql-databases-and-users-from-the-command-line)
