views:

187

answers:

5

Hi,

Could I have my php scripts on server A and connect to the MySQL database on server B?

If yes, how it would be done? Thanks in advance

+1  A: 

Yes.

The same way you access the localhost on the same server, you change the database host to the external one. This is more a configuration issue, you need to grant your database user remote access to your MySQL, you also need to make sure your firewall allows connections on the MySQL port.

Example on Debian: http://www.debianhelp.co.uk/remotemysql.htm

F.Aquino
A: 

That is all what you need .

(Even you can have your scripts on server A, your web server on server B and your database on server C ...)

PeterMmm
A: 

Have a look here:

http://us2.php.net/manual/en/function.mysql-connect.php

You can either pass in the server hostname as an argument, or configure in php.ini.

AJ
+1  A: 

Just don't the hostname of the other box for the connection. Details depend on the extension you're using:

$mysql = mysql_connect($host, $user, $pass);
$mysqli = new mysqli($host, $user, $password, $schema);
$pdo = new PDO("mysql:host=$host", $user, $pass);

Make sure that the user is allowed to access by the MySQL server (CREATE USER) and check that there's no firewall in the way.

johannes
+1  A: 

its simple all thise above techniques are quite complicated

suppose you have database on server B and website on server A(say it has IP 192.234.12.1)

on cpanel whitelist the IP of server B

and create a new user having sufficient privileges in database (say this user is test)

then create this user as [email protected]

Ankit Sachan