A message for Linux.com registered users: We are in the process of making changes to the Linux forums. Starting Monday, 8/13/18 at 6:00 PM PT, you will be unable to access the forums. They will re-launch as soon as possible on Wednesday, 8/15/18 with new features and improved functionality. Thank you for your patience and stay tuned for the new improved forums.

February 19, 2010

Redirect mysql server using /etc/hosts


I have a PHP script that runs on my Webserver and connects to my MySQL server. So far nothing special.
But for code development I have a local copy of my script and of my database on my laptop.
But now the address of the MySQL server in my PHP script points to the wrong direction. It still points to
my Web-MySQL-Server (e.g. mysql.example.org) but my development MySQL server runs on localhost.

Of course I could simply change the PHP script and change the name of the server, but than I have to remember
to do this every time I up or download the script.

Therefor, I tried to redirect the MySQL server DNS name in the file /etc/hosts
mysql.example.org localhost

That would be no problem for me because I never connect the server directly from my laptop (only through scripts
that run on the remote Webserver).

Unfortunately this does not work and I do not understand why.
If I change the server name in the script everything works fine.
If I change the /etc/hosts file the script says it could not connect to the server.

I hope you can help me to understand what I did wrong.

Thanks a lot and best regards,


Click Here!