Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I use this to connect to my local mysql server:

<?
$host = "localhost";
$user = "root";
$pass = "";
$db1 = "mydb";
$db = mysql_connect($host,$user,$pass);
mysql_select_db($db1,$db);
?>

But, I want to connect to a remote mysql server instead. I mean that site will be on the same server, but I'll connect to another mysql server. How can I do this?

I'm on Ubuntu.

share|improve this question
add comment

2 Answers

This is as simple as adjusting $host, $user and $pass appropriately. Of course, firewalls between you and the remote server will have to allow the connection, and you'll need a valid username/password (which should not be "").

share|improve this answer
    
PHP Warning: mysql_connect(): Lost connection to MySQL server at 'reading initial communication packet', system error: 113 in /home/dima/domjudge/domserver/www/my/test.php on line 7 PHP Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/dima/domjudge/domserver/www/my/test.php on line 8 i already tried , but how can i get permissions to connect ? –  Dima Savva Mar 19 '12 at 22:29
    
Who runs the remote server? Who controls the firewall where the webserver is? These people will need to be involved. –  Kyle Smith Mar 19 '12 at 22:31
    
thanks a lot , i'll talk to my hosters –  Dima Savva Mar 19 '12 at 22:39
add comment

On the remote MySQL server issue a GRANT statement like:

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON mydb.* TO remoteuser@'192.168.1.10' IDENTIFIED BY 'password';

Then change your $host to the remote MySQL server's IP address and $user to remoteuser and $pass to password (but hopefully not password). Also make sure that the remote MySQL server doesn't have bind-address=127.0.0.1 in the my.cnf and that there aren't any firewall rules blocking access.

In the above GRANT statement 192.168.1.10 is the IP address of the host that is running your PHP application.

Hope this helps.

share|improve this answer
    
Does it mean that i need to talk with hoster ? –  Dima Savva Mar 19 '12 at 22:36
    
I'd suggest using bind-address=<ip of app server> rather than omitting the line. –  xofer Mar 19 '12 at 22:37
    
@DimaSavva -- yes, we've been assuming you have control of the remote server. Someone needs to confiugre MySQL on that server to allow your app server to connect. –  xofer Mar 19 '12 at 22:38
    
ok , thanks a lot –  Dima Savva Mar 19 '12 at 22:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.