When I connect to my MySQL database locally I use:

$host = 'localhost';
$db = 'myDB';
$user = 'root';
$pass = '';

When my site resides on remote server I use the following parameters to make a MySQL connection:

$host = 'localhost';
$db = 'myDB';
$user = 'myUuserName';
$pass = 'myPAss';

What is strange that I use for both $host = 'localhost'; and it works also on remote server? Why? How is it possible? Shouldn't it be my domain name instead of 'localhost'?

2 Answers 2


localhost is relative to where the file is being executed from. To your remote host, its connecting to the mysql database on the remote side. This will only work if you have a mysql server on both sides and both are configured with the same database/data though.

  • Should it work if I write www.myDomainName.com instead of 'localhost' for remote server?
    – Narek
    Feb 6, 2011 at 19:08
  • @Narek: that will work if your mysql server accepts connections from other hosts than 'localhost'.
    – udo
    Feb 6, 2011 at 19:54

If you have a tunnel (ssh perhaps) this can work. Your local end of the tunnel will listen on localhost ( will copy the connection across to the remote server for you. It is more likely you have MySQL running locally and are connecting to the local MySQL server.

To connect to the remote server use the name of the server or its IP address as the host. This will only work if MySQL is listening on the interface. On many systems MySQL is listening only on the loopback (localhost) interface. You will need to change the bind-address in my.cnf to '' from '' and restart the database.

Permissions in the mysql user table can have a host specified. If your user is defined as 'user@localhost' you won't be able to connect from the remote server. It needs to be specified as 'user@%' or 'user@remotehost' to enable remote access.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .