Click here to Skip to main content
15,167,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Wile connecting to MySql I am getting error "MySql server has gone away" on line "$db = mysqli_connect("localhost:9080","root","XXXX");"

Here is my code.

	ini_set('mysql.connect_timeout', 300);
	ini_set('default_socket_timeout', 300);
   define('DB_SERVER', 'localhost:9080');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'XXXX');
   define('DB_DATABASE', 'api');
   //$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD);
   $db = mysqli_connect("localhost:9080","root","XXXX");
   // Check connection
	if (!$db) 
		die("Connection failed: " . mysqli_connect_error());
	$select_db = mysqli_select_db($db, DB_DATABASE);
		die("Database selection failed: " . mysqli_error($db));
	echo "Connected successfully";

What I have tried:

Have added

ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);

Also updated max_allowed_Packet to 16 M.
Then Restart all Services.

Error Screensshot: Imgur: The most awesome images on the Internet[^]
Updated 26-Sep-20 7:14am
Richard MacCutchan 10-Jul-17 9:18am
Please post the full text of the error messages in your question, that image is unreadable.
RickZeeland 10-Jul-17 10:48am
Can you connect to the db using PhpMyAdmin ?
Richard Deeming 10-Jul-17 12:01pm
Looks like this was an incorrect port number, and has been solved on your StackOverflow copy of this question:
Mysql server has gone away. Error while reading greeting packet[^]
KarstenK 11-Jul-17 12:13pm
leave it as answer to close the Q&A ;-)
Richard Deeming 11-Jul-17 12:21pm
Done, although I don't usually like to do that, since I didn't come up with the answer. :)

(Posting to remove the question from the "unanswered" list, as suggested in the comments.)

Looks like this was an incorrect port number, and was solved on StackOverflow by RiggsFolly[^] :
php - Mysql server has gone away. Error while reading greeting packet - Stack Overflow[^]

RiggsFolly wrote:
"and port is 3307"
Then use define('DB_SERVER', 'localhost:3307');
Although the normal port is 3306 UNLESS you are actually using MariaDB when the default port is 3307
//Run SQL query/queries

// 1. Check "max_allowed_packet"
show variables like 'max_allowed_packet'

// 2. update "max_allowed_packet"
set global max_allowed_packet=33554432

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900