Not able to make connection to mysql 8 in php - php

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

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'

Related

Using PHP to connect to AWS Lightsail MYSQL Database instance

I hope this question falls within the scope of this site.
I have created a Lightsail instance in AWS and added a MYSQL database to it. I have been able to connect to the database with Workbench, but not with a PHP file hosted in my instance.
I have checked the servername, username, password and dbname 20 times. Every time I try to connect I get the error message: "Connection failed 6: Unknown database 'Databasename-1'". Does anybody know of a guide on troubleshooting connecting to a MYSQL database on Lightsail?
<?php
$servername = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.rds.amazonaws.com";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "Databasename-1";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed 6: " . $conn->connect_error);
}
So, i think it turns out I did not know how to get the database name from my lightsail instance and possibly not understand how to use Workbench.
I was using a database name that did not exist.
I just started over and created everything from scratch from the command prompt and everything works as expected.

MySQL server has gone away error while using PHP to connect

When I try to run the below program I am get error as MySQL server has gone away.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Google says that it is due to
2
Try to debug the problem. This can be caused by any number of things.
Commonly there are:
The MySQL server crashed
A comms problem between the client and server
Abusing the client library in a way not intended, perhaps sending commands out of order or sending junk through the socket.
I came to solution as to debug the problem. How to debug to find the correct reason as to why this error is happening?
First check version of php installed in your computer
The MySQLi extension is designed to work with MySQL version 4.1.13 or newer.
if you have required version
am suggesting you to unistall your server for example if is xampp then install it again
it will work

How to fix MySQL State [2002] Database "Connection Refused "

I am setting a new live server for my Laravel application its work perfectly on localhost but not on live server. Now its showing SQL State [2002] connection refused. I also try with mysqli_connect and PDO but the error remains the same. Is possible a problem with Hosting Provider?
<?php
$servername = "examrunner.com";
$database = "XXXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXXXXX";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Use server name as localhost. Because cPanel hosts your databases locally, use localhost as the database's hostname.
$servername = "localhost";
Please check below mentioned points to resolve this issue :
Check database and user exists
Make sure that Database user and Database connected to each other and you have given sufficient privileges to user.
If site hosted on current serve user hostname = 'localhost'
Try to print detailed error.

Remote MySQL connection using php not working

I can connect to the remote MySQL database from the MySQL Workbench using these credentials but when I try to connect using the php script I keep getting a connection timed out error. The MySQL server has its bind address already changed, it is running on a Linode server so I am not sure if that changes anything.
I'm using the php connect test script for MySQLi from W3 Schools.
<?php
$servername = "remote-db-IP";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
The exact error I get is
Warning: mysqli::mysqli(): (HY000/2002): Connection timed out in
/home/glavxfrw/public_html/db-connect-test2.php on line 7 Connection
failed: Connection timed out

Connect to remote MySql server with XAMPP

I have an script that i want it to run for more that 5 minutes. But my current hosting service does not allow me to modify the max_time_limit in the php.ini (That is set to 2 minutes)...
So i thought that i could run the script with XAMPP and send the data to my database but i cant connect to the remote database.
This is the code for the connection:
$servername = "217.70.186.108"; //I've also tried with the name of the webpage (metagame.gg)
$username = "the_username"; //the username is not root since I've read that root can only connect from localhost. This user has all privileges. This user was created with the permision to be connected from any server (%)
$password = "the_password";
$dbname = "the_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
It displays this error:
Connection failed: An error occurred during the attempt to connect because the connected party did not properly responded after a period of time, or failed in the established connection because connected host has failed to respond.
Any help would be highly appreciated

Categories