Skip to main content

Connect to mySQL with mySQL package

mysql package doesn't support async function, but you can have a function with a promise to manage it.

const connection = mysql.createConnection({
host: "mysql.test.com",
port: 3306,
user: "user",
password: "password",
database: "myDatabase",
});

const executeQuery = (sql) => {
return new Promise((resolve, reject) => {
connection.query(sql, (error, results) => {
if (error) {
return reject(error);
}
return resolve(results);
});
});
};

await executeQuery("INSERT xxx INTO yyy");

connection.end();
info

For security reason, you may want to have a static IP for reaching your database (in order to filter access). More information are available here