system:
Linux web 2.6.27.21-0.1-pae #1 SMP 2009-03-31 14:50:44 +0200 i686 i686 i386 GNU/Linux
PHP Version 5.3.8
Apache Version Apache/2.2.21 (Linux/SUSE)
Kind of an old question, but I just stumbled on it after upgrading my Mac to Sierra. Until then I hadn't had the problem.
The trick is definitely to add your hostname to /etc/hosts, but you have to have the right hostname. That's easy on linux, it's in /etc/sysconfig/network. On Mac find it in System Preferences > Sharing. Under where it says Computer name will be something like computer-name.local. In /etc/hosts put
127.0.0.1 computer-name.local
Replacing computer-name with your computer name of course :). It needs this regardless of whether the database is a remote one or a local one.
I had plenty of other aliases for 127.0.0.1 but not that one. As soon as I added that one my apps started working again.