Sergii Rechmp
Sergii Rechmp

Reputation: 309

Cant connect to mysql: MySQL server has gone away

I'm start to learn mysql not so long time ago, and now i have problem, that i cant solve by myself. I'm trying to connect to mysql server with php. My admin gaved me ip, port, login, pass and db name. Php file looks like this:

$hostname = '192.168.1.20:1433';
$username = "h";
$password = "h";
$dbName = "mydb";
mysql_connect($hostname,$username,$password) OR DIE(mysql_error());
mysql_select_db($dbName) or die(mysql_error());
$query = "SELECT * FROM `insy` WHERE 1;";
$result=mysql_query($query) or die(mysql_error());
echo "works!";
mysql_close();

When i run page, its do nothing few minutes, than errors appears: Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12

Warning: mysql_connect() [function.mysql-connect]: Error while reading greeting packet. PID=1908 in N:\home\192.168.1.19\www\phlib.php on line 12

Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12
MySQL server has gone away

12'th line is:

mysql_connect($hostname,$username,$password) OR DIE(mysql_error());

I tried to create ODBC Sql Server via Control Panel -> Administration - with same parameters - it works, connection is ok, and i ca see the Db's.

Tell me please, where is my fault? I hope, i was clear.

Thanks for help.

Upvotes: 1

Views: 5396

Answers (3)

Tak Tak
Tak Tak

Reputation: 1

You may be using a port other than the default 3306. Define the port number under within the hostname like so:

$hostname = '192.168.1.20:your_port_number';

Otherwise use:

$hostname = 'localhost';

Upvotes: 0

Julio Orellana
Julio Orellana

Reputation: 97

I've just solved that problem.. the thing was when I did create a new user in MySql, after writing the user, the server ask you for where will be the "server" and you put localhost:8080.. ERROR! don't put the port!! just localhost. And thats how I solved it..

Upvotes: 1

Rohit Choudhary
Rohit Choudhary

Reputation: 2259

you can try mysql_ping($conn) if your server connection lost. http://php.net/manual/en/function.mysql-ping.php

Upvotes: 1

Related Questions