[php] SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP

I am new to PHP and CakePHP. I am finding problems while wiring my database using CakePHP.

Below is my application configuration.

I am on Bitnami WAMP stack 5.4.40-0. I am using CakePHP 3.0.4 to create a web MVC application

Entry for datasources in my app.php file.

/**
 * Connection information used by the ORM to connect
 * to your application's datastores.
 * Drivers include Mysql Postgres Sqlite Sqlserver
 * See vendor\cakephp\cakephp\src\Database\Driver for complete list
 */
'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'nonstandard_port_number',
        'username' => 'test2',
        'password' => 'computer',
        'database' => 'jobs',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,

        /**
         * Set identifier quoting to true if you are using reserved words or
         * special characters in your table or column names. Enabling this
         * setting will result in queries built using the Query Builder having
         * identifiers quoted when creating SQL. It should be noted that this
         * decreases performance because each query needs to be traversed and
         * manipulated before being executed.
         */
        'quoteIdentifiers' => false,

        /**
         * During development, if using MySQL < 5.6, uncommenting the
         * following line could boost the speed at which schema metadata is
         * fetched from the database. It can also be set directly with the
         * mysql configuration directive 'innodb_stats_on_metadata = 0'
         * which is the recommended value in production environments
         */
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
    ],

I have already created a database table called jobs according to CakePHP conventions. User test2 has global privileges the same as the root administrator.

But when I am running the bake all command, I am getting the following error:

2015-07-01 06:24:56 Error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user 'test2'@'localhost' (using password: YES)
Stack Trace:
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\PDODriverTrait.php(48): PDO->__construct('mysql:host=127....', 'test2', 'computer', Array)
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\Mysql.php(89): Cake\Database\Driver\Mysql->_connect('mysql:host=127....', Array)
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Schema\BaseSchema.php(46): Cake\Database\Driver\Mysql->connect()

PROBLEM SOLVED (UPDATE)

I followed Ankit and Spencer's directions.

I had a couple of problems.

  1. Host of my user was not localhost; it was a wildcard %. Changed that, then MySQL started refusing connections.

  2. I disabled my firewall and found that the port was different from 3306. So I changed the entry in app.php. Now my application is baked :)

This question is related to php mysql cakephp cakephp-3.0

The answer is


I saw it's solved, but I still want to share a solution which worked for me.

.env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=[your database name]
DB_USERNAME=[your MySQL username]
DB_PASSWORD=[your MySQL password]

MySQL admin:

 SELECT user, host FROM mysql.user

Thank you, spencer7593

Console:

php artisan cache:clear
php artisan config:cache

Now it works for me.

Laracasts answers


Check Following Things

  • Make Sure You Have MySQL Server Running
  • Check connection with default credentials i.e. username : 'root' & password : '' [Blank Password]
  • Try login phpmyadmin with same credentials
  • Try to put 127.0.0.1 instead localhost or your lan IP would do too.
  • Make sure you are running MySql on 3306 and if you have configured make sure to state it while making a connection

If you use MAMP, you might have to set the socket: unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock


I want to add to the answers posted on above that none of the solutions proposed here worked for me. My WAMP, is working on port 3308 instead of 3306 which is what it is installed by default. I found out that when working in a local environment, if you are using mysqladmin in your computer (for testing environment), and if you are working with port other than 3306, you must define your variable DB_SERVER with the value localhost:NumberOfThePort, so it will look like the following: define("DB_SERVER", "localhost:3308"). You can obtain this value by right-clicking on the WAMP icon in your taskbar (on the hidden icons section) and select Tools. You will see the section: "Port used by MySQL: NumberOfThePort"

This will fix your connection to your database.

This was the error I got: Error: SQLSTATE[HY1045] Access denied for user 'username'@'localhost' on line X.

I hope this helps you out.

:)


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to cakephp

SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP CakePHP 3.0 installation: intl extension missing from system How to check not in array element 404 Not Found The requested URL was not found on this server What is /var/www/html? Request exceeded the limit of 10 internal redirects due to probable configuration error how we add or remove readonly attribute from textbox on clicking radion button in cakephp using jquery? PHP Fatal error: Class 'PDO' not found Undefined variable: $_SESSION CentOS: Enabling GD Support in PHP Installation

Examples related to cakephp-3.0

SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP CakePHP 3.0 installation: intl extension missing from system