Home > Software engineering >  connection failed: SQLSTATE[HY000][1045] Access denied for 'username'@'localhost'
connection failed: SQLSTATE[HY000][1045] Access denied for 'username'@'localhost'

Time:12-05

        $servername= "localhost"; 
        $username = "username"; 
        $password = "password"; 
        try { 
            $conn = new PDO("mysql:host=$servername;dbname=testing",$username, $password);      
            //set the PDO error mode exception    
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
            echo "connected successfully";
        } catch(PDOException $e) {
            echo " connection failed: " . $e->getMessage();
        }

CodePudding user response:

Please check if:

  • user with username username really exists. With default Mysql instalation the standard username is root, not username.
  • the user with username has the password password. In default MYsql installation the user root has no password set.

CodePudding user response:

Check via commandline whether you can connect to your database: https://dev.mysql.com/doc/refman/8.0/en/connecting.html

mysql --host=localhost --user=myname --password=password mydb

I believe you are not knowing your credentials to access the database... How did you install the MySQL database?

  • Related