简体   繁体   中英

connection failed: SQLSTATE[HY000][1045] Access denied for 'username'@'localhost'(user password:YES) I am trying to connect php and my sql with PDO

        $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();
        }

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM