When I connect to a mysql database in PHP, and the db is hosted on the same machine as the site itself, I can either use the machine’s IP, or 127.0.0.1
Is there a difference in performance between the two? Is there a guideline for when I should use 127.0.0.1?
There is probably an incredibly small (unnoticeable) performance benefit to connecting via 127.0.0.1, since it will not require as many firewall/routing rules to be checked when you connect.
If you have the option, connect via the unix domain socket (usually something like
/tmp/mysql.sock). There is a definite performance increase to connecting via this method, as well as improved security.Edit
According to the comment by MarcB apparently connecting via
localhostwill automatically attempt to connect via the domain socket. As stated this is the preferred and most performant method,