0
0
Node.jsframework~20 mins

MySQL connection with mysql2 in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MySQL Connection Mastery with mysql2
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this MySQL connection code?
Consider this Node.js code using mysql2 to connect to a MySQL database and query a table named 'users'. What will be logged to the console?
Node.js
import mysql from 'mysql2/promise';

async function testConnection() {
  const connection = await mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'wrongpassword',
    database: 'testdb'
  });
  const [rows] = await connection.execute('SELECT * FROM users');
  console.log(rows);
}

testConnection().catch(err => console.log(err.message));
ASyntaxError: Unexpected token 'await'
BAn array of user objects from the 'users' table is logged.
CError: Table 'testdb.users' doesn't exist
DError: Access denied for user 'root'@'localhost' (using password: YES)
Attempts:
2 left
💡 Hint
Check the password used in the connection configuration.
component_behavior
intermediate
1:30remaining
What happens if you forget to close the MySQL connection?
In a Node.js app using mysql2 with async/await, what is the likely effect of not calling connection.end() after queries?
AThe database automatically closes the connection after 1 second.
BThe app will immediately crash with an error.
CThe app may keep running but eventually exhausts database connections causing errors.
DThe queries will not run without calling connection.end().
Attempts:
2 left
💡 Hint
Think about resource management and connection limits.
📝 Syntax
advanced
1:30remaining
Which option correctly creates a MySQL connection pool with mysql2?
Select the code snippet that correctly creates a connection pool using mysql2 in Node.js.
Aconst pool = mysql2.createPool({ host: 'localhost', user: 'root', password: 'pass', database: 'db' });
Bconst pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'pass', database: 'db' });
Cconst pool = mysql.createPoolConnection({ host: 'localhost', user: 'root', password: 'pass', database: 'db' });
Dconst pool = mysql2.createConnectionPool({ host: 'localhost', user: 'root', password: 'pass', database: 'db' });
Attempts:
2 left
💡 Hint
Check the exact method name for creating a pool in mysql2.
🔧 Debug
advanced
2:00remaining
Why does this mysql2 query throw a TypeError?
Look at this code snippet. Why does it throw a TypeError: connection.execute is not a function?
Node.js
import mysql from 'mysql2';

async function run() {
  const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'pass',
    database: 'db'
  });
  const [rows] = await connection.execute('SELECT 1');
  console.log(rows);
}

run();
ABecause createConnection returns a callback-based connection, not a promise-based one, so execute is undefined.
BBecause the SQL query syntax is invalid.
CBecause the database 'db' does not exist.
DBecause await cannot be used outside an async function.
Attempts:
2 left
💡 Hint
Check the import and method used to create the connection.
🧠 Conceptual
expert
2:30remaining
How does mysql2 handle prepared statements with parameter binding?
Which statement best describes how mysql2 handles prepared statements and parameter binding in queries?
Amysql2 automatically escapes parameters passed as an array to execute(), preventing SQL injection.
Bmysql2 requires manual escaping of parameters before passing them to queries.
Cmysql2 only supports parameter binding for SELECT queries, not INSERT or UPDATE.
Dmysql2 does not support prepared statements or parameter binding.
Attempts:
2 left
💡 Hint
Think about security and how parameters are handled in execute().