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

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

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

Below is my app 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  ['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 same like root admin.

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 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 changed the entry in app.php. Now my application is baked :)

回答1:

That error message usually means that either the password you are using doesn't match what MySQL thinks the password should be for the user you're connecting as, or a matching MySQL user doesn't exist (hasn't been created).

In MySQL, a user is identified by both a username (test2) and a host (localhost).

The error message you are getting identify the user (test2) and host (localhost) values...

  'test2'@'localhost' 

Check to see if that user exists, using this query from a client you can connect from:

 SELECT user, host FROM mysql.user 

You're looking for a row that has test2 for user, and localhost for host.

 user     host         -------  -----------  test2     127.0.0.1    test2     ::1          test2     localhost   

If that row doesn't exist, then the host may be set to wildcard value of %, to match any other host that isn't a match.

If the row exists, then the password may not match. You can change the password (if you're connected as a user with sufficient privileges, e.g. root

 SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('mysecretcleartextpassword') 

Also verify that the user has privileges on objects in the database.

 GRANT SELECT ON jobs.* TO 'test2'@'localhost'  


回答2:

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


回答3:

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



回答4:

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.

:)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!