Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any legitimate reason for using Unix sockets over TCP/IP with mysql?

Tags:

mysql

I'm trying to figure out why mysql uses Unix socket (/tmp/mysql.sock) by default, instead of normal TCP/IP sockets.

It doesn't seem like a security thing, as you can listen only on 127.0.0.1 which should be equally safe (socket file is world-writable, so you don't get Unix accounts based protection).

And surely all operating systems rely on high performance TCP/IP so much that it cannot be significantly slower than Unix sockets - Linux does all sort of zero-copy tricks even for network traffic, so it surely must be fast for loopback.

So is there any legitimate reason for using Unix sockets here, or is it just some weird historical accident?

like image 656
taw Avatar asked Dec 03 '09 08:12

taw


People also ask

In which situation is it preferred to use UNIX domain sockets over TCP IP sockets?

UNIX domain sockets know that they're executing on the same system, so they can avoid some checks and operations (like routing); which makes them faster and lighter than IP sockets. So if you plan to communicate with processes on the same host, this is a better option than IP sockets.

Are UNIX domain sockets reliable?

Valid socket types in the UNIX domain are: SOCK_STREAM, for a stream-oriented socket; SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.

Are UNIX sockets faster than TCP?

Unix domain sockets are often twice as fast as a TCP socket when both peers are on the same host. The Unix domain protocols are not an actual protocol suite, but a way of performing client/server communication on a single host using the same API that is used for clients and servers on different hosts.

Is it possible to connect to MySQL via Unix socket from another host?

"Unix sockets" are not TCP/IP sockets. They are always limited to the current system only – it is not possible to connect to another system's socket over the network. Because of that, the hostname localhost is special in MySQL and the client doesn't actually attempt to look up its IP address at all.


1 Answers

While you don't hit the entire IP stack when going over localhost, you still hit a big part of it. A unix socket is essentially just a 2-way pipe. It's faster and lighter.

Unix sockets also allow you to control access without managing firewall rules, as access can be given through filesystem permissions.

One other feature unix sockets provide is the ability to pass file descriptor from one process to another.

like image 132
nos Avatar answered Oct 26 '22 05:10

nos