I want to connect to my database on my Linux machine with PHP (PHP 7.2.24-0ubuntu0.18.04.6 (cli)) using mysqli(). But whenever I try to access it, I get this error:
Warning: mysqli_connect(): (HY000/1698): Access denied for user 'root'@'localhost' in /var/www/html/connect_DB.php on line 9
Connected Success!
I have MySQL installed (mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper), and I use the Apache2 server. I do not have XAMPP installed.
My code:
error_reporting(E_ALL); ini_set('display_errors', '1');
$mysql_host = 'localhost'; //server
$mysql_user = "root";
$mysql_pass = "root";
$conn = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, "tests");
echo "Connected Success!<br>";
The table ("tests") belongs to the root user... I don't know if this may have anything to do with it, but to open mysql, I do this:
sudo mysql -u root -p
I did check on Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) and it did not quite help...
My problem was that PHP didn't have the privileges to connect to user root, so I had to create a new user...
Fix: “Connect failed: Access denied for user 'root'@'localhost' (using password: YES)” from php function