SQLSTATE[HY000] [1045] Access denied for 'user'@'localhost' (using password: YES)

15,519

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();
    }
?>
Share:
15,519
Miguel Sánchez García
Author by

Miguel Sánchez García

Updated on June 04, 2022

Comments

  • Miguel Sánchez García
    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
      arcee123 over 6 years
      is your mysql on the same server?
    • Option
      Option over 6 years
      Try and change localhost to 127.0.0.1
  • Miguel Sánchez García
    Miguel Sánchez García over 6 years
    Thanks 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
    Option over 6 years
    @Michael, edit your answer to prevent clogging up the comment replies on your answer.
  • mamesaye
    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.