0
votes

I want to establish a connection with MySql server.

My PHP code is :

<?php
$dbhost = 'testhost.com';
$dbuser = 'testuser';
$dbpass = 'testpass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die        
('Error connecting to mysql');
$dbname = 'testdb';
mysql_select_db($dbname);
?>

But it show the following error:

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 0 in F:\xampp\htdocs\db_connect123.php on line 6 Error connecting to mysql

In this situation i am totally confused because it work fine in localhost if i change the hostname. Please, Give me a solution how can i resolve this problem and get connect with MySql server.

2
Are thos your real username/passwords? seems abit silly dont you think.. - Lawrence Cherone
"reading initial communication packet" refer to an invalid hostname.. please check the Hostname correctly. - Fahid Mohammad
your server might not be allowing remote connections. Check to make sure remote connections are enabled on your server / hosting account. Are you on shared hosting? - Imran Omar Bukhsh
Permissions. You need to set them on the remote mysql box so whatever account the web server is running as, has access. - Tony Hopkinson
If as @LawrenceCherone alludes to you have just told the entire world your user name and password, change them. - Tony Hopkinson

2 Answers

0
votes

Make sure my.ini is not:

bind-address = 127.0.0.1
port = 3306

Instead it should be:

bind-address = 0.0.0.0
port = 2083

Make sure that your user 'carefre2_polash' has a Host field of "%" (means any-host) under MySQL.

And if this is MySQL running on Windows, configure the native Windows firewall to accept incoming TCP on the above port number.

-1
votes

Use this Code Please, its a standart SQL connection code:

$link = mysql_connect('xhost', 'xuser_name', 'xpassword');
if (!$link) {
    die('Connect Error : ' . mysql_error()); 
}
mysql_set_charset('utf8', $link); // Encoding
$db_selected = mysql_selectdb('x_database', $link);
if (!$db_selected) {
    die ('DBselect Error : ' . mysql_error());
}

Good Luck and Have a nice day.