SQLSTATE[HY000] [1045] Access denied for 'user'@'localhost' (using password: YES)
Solution 1
1)For the first error, it is saying that your mysql credentials are invalid. Correct your database configuration in your php code. Arrange the username, password, etc... You can get this info from your hosting provider. Try to change '%' to 'localhost'.
2)For the second Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server and
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Solution 2
For the first issue:If your server name is localhost. Please verify all o f the info (servername, username, password for database) from your hosting provider. Try this code:
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
try {
$conn = new PDO("mysql:host=$servername;dbname=yourdatabase",
$username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
Miguel Sánchez García
Updated on June 04, 2022Comments
-
Miguel Sánchez García almost 2 years
It is the first time I upload a web page to a server and I am quite inexperienced in the subject. I downloaded xampp to work in a production environment and through phpmyadmin I configured a user name, password and host = '%' But when I upload my files with Filezila, I throw this:
SQLSTATE [HY000] [1045] Access denied for 'user' @ 'localhost' (using password: YES)
I tried to connect to my server through the Mysql Shell and Workbench to create a user "user" that had all the permissions to access my database, but in both cases I threw the following:
Host xxxx.xx.xx Is not allowed to connect to this MariaDB server.
What I do not understand is why MariaDB Server? I do not have it downloaded or something.
I do not really know how I can grant the permissions to the new user inside the host so that it can access my database. My connection code is as follows:
try { $dsn = 'mysql:host=%;dbname=db_name;charset=utf8'; $db = new PDO($dsn, 'user', 'contraseña'); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } catch (Exception $e) { echo $e->getMessage(); exit; }
-
arcee123 over 6 yearsis your mysql on the same server?
-
Option over 6 yearsTry and change
localhost
to127.0.0.1
-
-
Miguel Sánchez García over 6 yearsThanks for your answer! I have changed the username, and password (those provided by my hosting provider) in phpmyadmin, in the console (the second problem is solved, thanks!) and in my php code, and I have left both the host as localhost, but the error persists.
-
Option over 6 years@Michael, edit your answer to prevent clogging up the comment replies on your answer.
-
mamesaye almost 5 years
ALTER USER 'userName'@'localhost' IDENTIFIED BY 'New-Password-Here';
helped since I did not set a password. then grant privileges and flush.