Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: mysqli_connect(): MySQL server has gone away

I wrote a simple PHP code to connect to the MySQL server as below

<?php

$username = "root";
$password = "Kepwd";
$hostname = "localhost:81";

//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

but this generates the following errors. I found some topics regarding this issue in google and Stack Overflow. but those don't help me.

( ! ) Warning: mysqli_connect(): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8

( ! ) Warning: mysqli_connect(): Error while reading greeting packet. PID=10612 in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8

( ! ) Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8
Unable to connect to MySQL
like image 463
Lak Avatar asked Feb 12 '23 06:02

Lak


1 Answers

The error is here:

$hostname = "localhost:81";

You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use

$hostname = "localhost";
like image 177
user4035 Avatar answered Feb 13 '23 20:02

user4035