how to connect to database on another server

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.


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


Yes it can be done.

Find out the IP address of the server A where your scripts will be uploaded. Do not forget to change the localhost to the ip address of the Server B in mysql_connect() or mysqli_connect() method.

Now go the control panel of the Server B where your Database is.

In the control panel's Homepage go the databases section and click the Remote MYSQL option.

Then add the Ip address of the Server A and click on add host.

Now you can access to the database in Server B while your scripts are running in Server A. Mind you the fetched result will be slow cause it is getting data from database that is located on another server.

Your welcome


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]