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

会有一股神秘感。 提交于 2021-02-19 02:12:53

问题


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?


回答1:


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.




回答2:


There is less overhead in using Unix Sockets instead of TCP/IP, as this is basically a byte stream without the extra network bookkeeping. See wikipedia for a little bit more info.

Unix domain connections appear as byte streams, much like network connections, but all data remains within the local computer. UNIX domain sockets use the file system as address name space, i.e. they are referenced by processes as inodes in the file system. This allows two distinct processes to open the same socket in order to communicate. However, the actual communication (the data exchange) does not use the file system, but buffers in kernel memory.



来源:https://stackoverflow.com/questions/1838358/is-there-any-legitimate-reason-for-using-unix-sockets-over-tcp-ip-with-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!