Redirect mysql server using /etc/hosts

Link to this post 19 Feb 10


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. 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 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,


Link to this post 30 May 10

please try instead of localhost:dry:

Link to this post 02 Jun 10

Thanks for the idea, but unfortunately this does not work. It still get an error from my apache that it cannot connect to mysql.

Link to this post 02 Jun 10

Do you have any odd rules in your firewall that may be restricting loopback access on the necessary ports?

Link to this post 11 Jun 10

Did you put the IP address before the hostname? The following is the correct way to redirect back the the local machine in /etc/hosts:

Link to this post 11 Jun 10


I have to apologize. I figured out a few days ago how to redirect the mysql server and forgot to post it here.

It works exactly as jabirali wrote:


But it seems that this does not work, when I establish a VPN connection to my university network. But I have to double check this first and it is not a really problem for me, because I nearly never connect to the university and use than the mysql server.

Thanks a lot for your help and sorry again for forgetting to post my solution.

Best regards,


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Linux Training / Board