Mysql cannot connect - Access denied (using password yes)

Mysql cannot connect - Access denied (using password yes)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding "Access Denied for User" in Shared Hosting Environments

Encountering a Warning: mysql_connect(): Access denied error is one of the most common hurdles when deploying applications on shared hosting platforms. The error message explicitly states whether a password was used (using password: YES), which saves you from debugging authentication protocol issues, but it rarely explains why the credentials are being rejected despite appearing correct in your database management panel.

In many cases, particularly with providers like Eleven2 or similar cPanel-based hosts, the discrepancy between what you see in the control panel and what the PHP application sees is due to how MySQL handles user host specifications and privilege propagation.

The Host Spec Mismatch

The core technical issue usually lies in the Host column of your MySQL user table, not necessarily the password itself. When you create a user via cPanel or phpMyAdmin, the system often creates an entry like 'username'@'localhost' rather than 'username'@'%'.

If your application configuration file attempts to connect over TCP/IP (using mysql_connect, PDO, or mysqli), but the MySQL server only has privileges granted for local socket connections (localhost), the connection will fail with an access denied error. This is a frequent point of confusion because cPanel sometimes presents a unified interface that hides these underlying host distinctions.

To resolve this programmatically without contacting support immediately, you can inspect your current user grants:

SELECT User, Host, password FROM mysql.user WHERE User = 'your_username';

If the Host column reads localhost, but your application is trying to connect via a remote IP (which happens even on shared hosting if the PHP process runs as an external user), you need to explicitly grant privileges for that host or the wildcard. As seen in the source material, attempting to use:

GRANT ALL PRIVILEGES ON databasename.* TO 'bookorama'@'%' IDENTIFIED BY 'yourpassword';
FLUSH PRIVILEGES;

often results in #1044 - Access denied if the MySQL server itself restricts who can execute GRANT statements or if the user being modified doesn't have sufficient privileges to alter their own host mapping. This is a security feature of MySQL to prevent privilege escalation, often leading users to believe their credentials are wrong when they aren't.

Deprecated Extensions and Modern Practices

The code snippet provided in the original scenario uses mysql_connect(). It is important to note that this extension was deprecated in PHP 5.5 and removed entirely in PHP 7.0 due to security vulnerabilities and a lack of prepared statement support. Relying on it for new projects is a significant technical debt decision.

If you are maintaining legacy code found on shared hosting, switching to mysqli or PDO is non-negotiable for security. However, even with modern drivers, the underlying authentication mechanism remains the same: the client must match an entry in the mysql.user table.

When debugging these issues, avoid blindly granting ALL PRIVILEGES. Follow the principle of least privilege. Instead of using a wildcard host (%) immediately, try granting access to your server's specific IP address first. If you don't know the IP, check the output of hostname -I on your local machine or ask the hosting provider for the external IP that initiates the connection.

GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO 'username'@'specific_ip' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

When Hosting Infrastructure Fails

Sometimes, as noted in the source material, the issue is not with your code or SQL commands but with the server-side configuration. In shared hosting environments, the MySQL service might be misconfigured to ignore certain host entries or block specific connection types (TCP vs. Unix socket).

If you have verified your credentials multiple times, ensured the Host field matches your connection method, and confirmed that FLUSH PRIVILEGES was successful, but the error persists, the problem likely lies with the hosting provider's MySQL instance. They may have restrictive firewall rules or a broken user mapping table that requires administrative intervention to fix.

In such scenarios, contacting support is the correct path forward. While waiting for them to investigate server-side logs or restart specific services, ensure your application code isn't hardcoding incorrect credentials or attempting to use deprecated functions that might mask the real connection failure.

Our network