When to use a MySQL socket and when to use a host:port?
Many applications allow me to connect to Mysql using a username, password host and port. Some allow me to configure a socket instead of the host:port
.
Is there any clear benefit of one over the other? I can imagine that a socket only works when MySQL is on the same machine. Is that so? And if so, are there benefits over using that socket instead of connecting to localhost:3306
?
I am not too familiar with the ins- and outs of networking and sockets, so maybe I am completely missing some crucial information and my question is just plain stupid; if so, could you explain what I am missing?
Well, it's simple.
Socket is a file based communication, and you can't access the socket from another machine.
On the other hand, ports are open to the world (depends on configuration) and you can access the mysql from other machine using host+port combination.
Also, as much I understand sockets, they are just combination of host+port, just in the file format. So, I don't see any clear benefit in using any of them (as much my knowledge goes).
Though I personally prefer using host+port, as my code becomes more flexible, as I can move it to the other machine, without changing much.
Copy pasting from some old post:
Unix sockets are a little bit faster as you don't have the tcp-overhead. If you realize this performance loss is a question of server load. If you don't have very high server load you won't recognize it.
If you use Jails (FreeBSD) or some other virtualisation technology to separate the e.g. MySQL-Server from the Webserver, you often use the tcp/ip setup instead of sockets. The firewall rules need to restrict the access though.
You need to find out if your system is under heavy load so that a socket is a must or you can focus on a nice system design (separating services), then a tcp/ip solution would be better.
So make a long answer short:
Yes, there is a performance difference, sockets are faster. If you are not suffering high server load, just choose what fits better to your system's design.
... after 10 years,
GCP MySQL has a sql proxy by which you can use a socket from a VM to connect to MySQL server on different VM.
Connecting using the Cloud SQL Auth proxy