MySQL and PHP returning HTTP error 500

August 11, 2018 1.3k views
MySQL Ubuntu

I'm trying to connect to my MySQL database with php, and I'm getting a http 500 error, and the following in the error log.

[Sat Aug 11 07:33:08.899904 2018] [php7:error] [pid 18763] [client 75.97.225.218:57986] PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' in /var/www/html/createaccount.php:14\nStack trace:\n#0 /var/www/html/createaccount.php(14): PDO->__construct('mysql:host=127....', 'root', 'password123', Array)\n#1 {main}\n  thrown in /var/www/html/createaccount.php on line 14

This is the code for createaccount.php

<?php
$host = '127.0.0.1';
$db   = 'databaseDB';
$user = 'root';
$pass = 'password123';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
?>


And I can log in with mysql -u root -p and then entering the password password123. So I know it's the password, or at least I think so, why else would it let me log in.

3 comments
Be the first one to answer this question.