What's the deal with socket paths?

michaelsanford

Translator, Web Developer
I was just reading my PHP manual and it says that I can refer to my MySQL database not only by IP address or host name:port but also by a socket path such as localhost:/tmp/mysql.sock which I noticed exists on my machine...

What exactly is a socket path? I'm trying to wrap my head around the way a file can act as a port. How can I determine the socket listing on my machine (and by extension, find the correct socket for MySQL). Are there advantages-performance or security-to using sockets over ports?

TIA!
 
One of the most common, and nicest, abstractions in Unix is "everything is a file". For example, check out /dev to see files for all your hard drives, the modem, the OS's random number service, etc. Basically, dealing with something that has a 'pseudo file' representation, whether it's a physical device or a service like MySQL, can then take on the familiar semantics of file I/O.

A socket can be bound to an IP port, so that it can be accessed via Internet, or to a file path, so that it can be accessed via filesystem calls. Once you have a file descriptor for the socket though, you just use read() and write() calls on it, no matter what it was bound to.

So, to answer your question - the only advantage to one method over another would be what you're most comfortable programming with.
 
Back
Top