0

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

Narek
  • 235
  • 1
  • 4
  • 15

2 Answers2

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.

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

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.

BillThor
  • 27,354
  • 3
  • 35
  • 69