Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to make connection to mysql 8 in php

Tags:

php

mysql

I am trying to connect to mysql server using php but it gives the following error

Connection failed: The server requested authentication method unknown to the client

mysql server version is 8.0.12 and php version is 7.2.9. My code connect to mysql server

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "mypassword";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

This question has been previously asked before here but the solution did not work for me so I am asking this again. Thanks

edit: I re-installed php now it gives the following for the same code

This page isn’t working
127.0.0.1 is currently unable to handle this request.
HTTP ERROR 500
like image 312
mark robin Avatar asked Nov 08 '22 02:11

mark robin


1 Answers

I had a similar problem and found that you can change mysql authentication by running the following code via Workbench

ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY 'new-password-here'
like image 96
Bonifacey Avatar answered Nov 14 '22 23:11

Bonifacey