Node.js
May 3, 2026
How to Connect Node.js to a Remote MySQL Database
Connecting your Node.js application to a remote MySQL database like FreeDB is straightforward. In this tutorial, we will use the popular mysql2 package.
1. Install mysql2
First, install the package using npm or yarn:
npm install mysql2
2. Create the Connection
Use the credentials provided in your FreeDB dashboard:
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'mysql.freedb.tech',
user: 'freedb_your_username',
password: 'your_password',
database: 'freedb_your_database_name'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected to FreeDB!');
});
3. Best Practices
Always use environment variables (.env files) to store your database credentials. Never hardcode them in your source code or commit them to GitHub.