Daniel
Daniel

Reputation: 944

Can't connect to remote mysql with PHP

I have a database running on a server which my desktop program updates. My program is written in C# and I can connect just fine using the Mysql Connector.

private void Initialize()
    {
        server = "xxx.com";
        database = "test01";
        uid = "user";
        password = "pass";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new MySqlConnection(connectionString);
    }

I want to be able to retrieve some data from my database with a PHP script on my website, but I can't get even a simple connection to work with my database, with the same connection information. I am not very familiar with php, and I'm hoping that I'm just making a stupid mistake :)

$con = mysql_connect("xxx.com","user","pass");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

This just keeps dying... Could someone tell me that I am doing something stupid and tell me how to fix it?

Thanks in advance, LordJesus

EDIT: The error I keep getting is this: Could not connect: Lost connection to MySQL server at 'reading initial communication packet', system error: 110

EDIT #2: My 'user' already has been granted access on '%' which should cover all IP's. When I run phpinfo(); I get a lot of info including a section called mysql with info there.

SOLVED... OR UNRESOLVED? OK, I think I know why it fails. My website is hosted at one.com, and chatting with one of their supporters I learned that they do not allow connections to remote databases, only the one on my domain. Which is not what I want. So I didn't solve it, but I found out why I got that error. Now I need to find a way to work around that...

Upvotes: 2

Views: 8263

Answers (1)

Sean C.
Sean C.

Reputation: 430

It sounds to me like your environment is as follows: Your desktop with a C# program, your MySQL server located on another machine and finally and web server that is autonomous and not attached to either of those systems. If this is the case, you need to grant permissions to your database from the web server.

The following will grant select permission to your webserver.

GRANT SELECT ON test01.* TO 'user'@'ip.of.the.webserver' IDENTIFIED BY 'pass';

Upvotes: 2

Related Questions