• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

“Access denied for user ‘username’@’hostname’ (using password: YES)” – Error while connecting MySQL with PHP

by admin

The Problem

If the MySQL password contains a dollar sign character ‘$’, PHP is unable to connect and issues an error like:

Access denied for user 'username'@'hostname' (using password: YES)

Connections via other clients work correctly.

The Solution

PHP interprets dollar signs inside of double quotes as starting a variable name, which will be expanded. This expansion will change the password sent to MySQL.

There are 3 ways to avoid this problem:

  1. Escape the character with a backslash: “pa\$\$word” (actual password being pa$$word)
  2. Use single quotes: ‘pa$$word’
  3. Avoid using the ‘$’ character in the password.

To help detect this kind of problem, a good practice is to run PHP with error reporting enabled and set to include E_STRICT (or E_ALL for PHP versions 5.4.0 and later). This setting will notify you of an undefined variable, which may reveal the problem.

PHP Error Levels

PHP provides several built-in constants for describing different error levels. Table below includes some of the more important ones.

Name Description
E_ERROR Fatal run-time error. Execution is halted.
E_WARNING Non-fatal run-time error.
E_NOTICE Run-time notice about possible error.
E_USER_ERROR Fatal user-generated error.
E_USER_WARNING Non-fatal user-generated warning.
E_USER_NOTICE User-generated notice.
E_COMPILE_ERROR Fatal compile-time error.
E_PARSE Compile-time parsing error.
E_STRICT Suggested change to ensure forward compatibility.
E_ALL All errors, except E_STRICT prior to PHP 5.4.

Filed Under: mysql, PHP

Some more articles you might also be interested in …

  1. How to List and Set SELinux Context for MySQL Server
  2. How to take Logical Backups on a MySQL Replication Slave using shell script
  3. PHP function strcmp() – Binary safe string comparison
  4. How to Install MySQL Cluster on a single server for testing purposes
  5. How To Shut Down a Node in MySQL Cluster
  6. How to find the size of a MySQL database
  7. Configuring mysqld to log slow queries
  8. PHP function abs – Absolute value
  9. PHP atan function – Arc tangent
  10. Excluding a table or database from MySQL replication

You May Also Like

Primary Sidebar

Recent Posts

  • csslint: A linter for CSS code
  • csh: The shell (command interpreter) with C-like syntax (Command Examples)
  • csc: The Microsoft C# Compiler (Command Examples)
  • crystal: Tool for managing Crystal source code

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright