0
votes

Trying to connect to a database that is not on the same host as my site for the first time and not having much luck. The DB is on a godaddy host and I have configured it to allow remote access:

$dbc = mysqli_connect($db_host,$db_user,$db_pass,$db_name);

if (!$dbc) {
    die('Connect Error: ' . mysqli_connect_error());
}

and I get the following on page:

Warning: mysqli_connect() [function.mysqli-connect]: [2002] Connection timed out (trying to connect via ‘mydbhostname:3306) in ‘path’ on line 3

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Connection timed out in ‘path’ on line 3

Connect Error: Connection timed out

Anything I need to look for on my end or something in the script that could be causing this?

EDIT: well it appears everything works when I use mysql, but not mysqli...and mysqli is enabled on the server.

3
Use localhost instead if it is on the same server? Is it using a different port perhaps? - 1321941
Thats the whole issue, it is not on the same server. - absentx
Are you sure "mydbhostname" is the uri for your database? - Sterling Duchess
that was just me putting in a dumby host name here. I have confirmed with the company that I have the proper hostname. - absentx
Blunt question but how long ago did you setup your sql database some hosts have a qeue about 2-5min delay for activation. Time out is result of dropped connection so either the db has not been activated yet or your host is blocking your port or there could be another application using said port. - Sterling Duchess

3 Answers

1
votes

I had the same problem on a Digitalocean Ubuntu 14.04 server, where the firewall configuration was the culprit. Mysql wasn't allowed on port 3306 by the ufw firewall rules.

Solution:

$ sudo ufw status
$ sudo ufw allow mysql
$ sudo ufw reload
-1
votes

I don't understand the difference between mysqli and mysql, but I've found on GoDaddy that using:

mysqli_connect doesn't work. mysql_connect does work.

Try using the following sample code to see if you can connect to your database which needs data in at least one table:

<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "XXXXXXX.db.XXXXXXXX.hostedresource.com";
$username = "XXXXXXX";
$dbname = "XXXXXXX";

//These variable values need to be changed by you before deploying
$password = "XXXXXXX";
$usertable = "XXXXXXX";
$yourfield = "XXXXXXX";

//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);

//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);

if ($result) {
    while($row = mysql_fetch_array($result)) {
        $name = $row["$yourfield"];
        echo "<h2>some data</h2>";
        echo "$name<br>";
    }
}
?>