Skip to main content
MediumInfo disclosureOWASP A05:2021CWE-209

How to fix: MySQL Access Denied Error Exposed

The application returned a raw MySQL 'Access denied for user' error message in the response body. These messages include the database username, host, and whether a password was provided, directly exposing database configuration details.

Why it matters

MySQL access denied errors expose the database username (e.g., 'root'@'localhost'), the hostname or IP of the database server, and whether a password was used ('using password: YES/NO'). This information helps attackers map the database configuration, try credential stuffing against the database port, and determine whether the database uses default or weak credentials.

When a PHP, Python, or other server-side application fails to catch a database connection error, the raw MySQL error message from mysqli_connect() or PDO is passed through to the HTTP response body. The error format 'Access denied for user "user"@"host" (using password: YES)' contains multiple sensitive fields that should never reach the browser.

How to fix it

  1. 1Wrap all database connection calls in try/catch blocks and return a generic 500 error to the client.
  2. 2Log the full database error server-side (not to the response) for debugging.
  3. 3Never let unhandled exceptions propagate to the HTTP response in production.
  4. 4Set PHP's display_errors = Off and log_errors = On in php.ini for production environments.

Code examples

PHP (safe error handling with PDO)

PHP
try {
  $pdo = new PDO(
    'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME,
    DB_USER,
    DB_PASS,
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
  );
} catch (PDOException $e) {
  // Log the real error server-side
  error_log('DB connection failed: ' . $e->getMessage());
  // Return a generic error to the client: never expose $e->getMessage()
  http_response_code(500);
  echo json_encode(['error' => 'Internal server error']);
  exit;
}

Node.js (safe MySQL error handling)

JavaScript
import mysql from 'mysql2/promise';

let pool;
try {
  pool = mysql.createPool({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME,
  });
} catch (err) {
  // Log, never expose
  console.error('DB pool creation failed:', err.message);
  process.exit(1);
}

// In route handlers:
try {
  const [rows] = await pool.query('SELECT ...');
} catch (err) {
  console.error('Query failed:', err.message);
  res.status(500).json({ error: 'Internal server error' });
}

PHP (php.ini production settings)

INI
; Production settings: never display errors to users
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/log/php/error.log

References

Detected automatically by VulnRadar. The check id is mysql-access-denied-error, stable across every scan so you can filter or diff on it.

See every info disclosure check

Questions

How severe is MySQL Access Denied Error Exposed?
VulnRadar rates this medium severity in the info disclosure category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id mysql-access-denied-error, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does MySQL Access Denied Error Exposed map to?
It maps to CWE-209. In the OWASP Top 10 it falls under A05:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for mysql access denied error exposed

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: MySQL Access Denied Error Exposed | VulnRadar