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.

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'?

share|improve this question

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.

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

If you have a tunnel (ssh perhaps) this can work. Your local end of the tunnel will listen on localhost (127.0.0.1) 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 '0.0.0.0' from '127.0.0.1' 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.

share|improve this answer

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.