SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP
That error message usually means that either the password we are using doesn't match what MySQL thinks the password should be for the user we'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 identifies the user ("test2") and the host ("localhost") values...
'test2'@'localhost'
We can check to see if the user exists, using this query from a client we can connect from:
SELECT user, host FROM mysql.user
We're looking for a row that has "test2" for user, and "localhost" for host.
user host
------- -----------
test2 127.0.0.1 cleanup
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. We can change the password (if we're connected as a user with sufficient privileges, e.g. root
SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('mysecretcleartextpassword')
We can also verify that the user has privileges on objects in the database.
GRANT SELECT ON jobs.* TO 'test2'@'localhost'
EDIT
If we make changes to mysql privilege tables with DML operations (INSERT,UPDATE,DELETE), those changes will not take effect until MySQL re-reads the tables. We can make changes effective by forcing a re-read with a FLUSH PRIVILEGES
statement, executed by a privileged user.
Unexpected Exception: SQLSTATE[HY000] [1045] Access denied for user ****@'localhost' (using password: YES)
Sometimes Access denied Exception Error because your mysql credentials are invalid.
Secondly, from my experience i observed that this also happens because you did not set password to your database connectivity. eg
private $host = "localhost";
private $db_name = "db_dbtest"; // Database name
private $username = "db_user"; // your database username
private $password = "db_password"; // Your password
public $conn;
}
Try and set password to your database connectivity. I had such experience and after changing my Collation to utf8_general_ci on the Operations tab, this could not solve my problem. I thought of adding password to my database connection and immediately it connected.
You can tesrun this and see if it helps.
cakephp error. Connection to database ...not ...: Access denied for user 'my_app'@'localhost' (using password: YES)
Cake will not create the database or database user for you. You need to create them yourself and then match these database credentials into the db config file. Your datasource in app/Config/app.php
file should look something similar to:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'my_db_user',
'password' => 'my_db_user_password',
'database' => 'cake_database_name',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
]
],
In this example you would have to create a database named: cake_database_name, and then add a database user named my_db_user with the password of my_db_user_password.
CakePHP database: SQLSTATE 28000 - Error 1045 Access denied for user 'kcal'@'localhost'
An access denied error indicates exactly that - Cake is able to talk to MySQL, and it's saying access is denied with the user@host indicated. Proabbly because the user doesn't have access to the checkpoint_live database.
Since you just moved to a new server, you probably need to grant privileges on the MySQL table for that particular user@host. Creating the same user with the same password isn't enough. Try this:
GRANT ALL PRIVILEGES ON checkpoint_live.* TO 'kcal'@'localhost';
FLUSH PRIVILEGES;
Related Topics
When to Pass-By-Reference in PHP
How to Fetch All in Assoc Array from a Prepared Statement
PHP - Session Destroy After Closing Browser
Traits in PHP - Any Real World Examples/Best Practices
Save Current Page as HTML to Server
Advantages/Disadvantages of Pconnect Option in Codeigniter
How to Check If an Integer Is Within a Range
How to Remember Input Data in the Forms Even After Refresh Page
Upload Multiple Files in Codeigniter
Replacing MySQL_* Functions with Pdo and Prepared Statements
PHP Form Send Email to Multiple Recipients
How to Sort an Array of Utf-8 Strings in PHP
Read a File Backwards Line by Line Using Fseek
Php-Fpm Doesn't Write to Error Log
How to Efficiently Find the Closest Locations Nearby a Given Location
How to Remove Blank Lines from Text in PHP