如何将本地unix套接字映射到inet套接字? [英] How can I map a local unix socket to an inet socket?
问题描述
我很好奇是否可以将UNIX套接字映射到INET套接字.情况很简单,我想连接到MySQL服务器.不幸的是,它禁用了INET套接字,因此我只能使用UNIX套接字进行连接.我正在使用/编写的工具必须连接到INET套接字上,因此我试图查看是否可以将一个映射到另一个.
I'm curious if it is possible to map a UNIX socket on to an INET socket. The situation is simply that I'd like to connect to a MySQL server. Unfortunately it has INET sockets disabled and therefore I can only connect with UNIX sockets. The tools I'm using/writing have to connect on an INET socket, so I'm trying to see if I can map one on to the other.
进行了大量搜索,但我确实找到了 socat ,据说我在寻找什么.我想知道是否有人对如何实现这一目标有任何建议.我一直使用的命令行(部分成功)是:
It took a fair amount of searching but I did find socat, which purportedly does what I'm looking for. I was wondering if anyone has any suggestions on how to accomplish this. The command-line I've been using (with partial success) is:
socat -v UNIX-CONNECT:/var/lib/mysql/mysql.sock TCP-LISTEN:6666,reuseaddr
现在,我可以建立连接并与服务器对话.不幸的是,由于我需要使用fork选项,因此建立多个连接的任何尝试都会失败,但是此选项似乎使连接无法正常工作.
Now I can make connections and talk to the server. Unfortunately any attempts at making multiple connections fail as I need to use the fork option but this option seems to render the connections nonfunctional.
我知道我可以使用Perl(我的首选语言)解决该问题,但是我宁愿避免自己编写整个实现.我熟悉IO :: Socket库,我只是希望任何人都有做这种事情的经验.打开建议/想法.
I know I can tackle the issue with Perl (my preferred language), but I'd rather avoid writing the entire implementation myself. I familiar with the IO::Socket libraries, I am simply hoping anyone has experience doing this sort of thing. Open to suggestions/ideas.
谢谢.
推荐答案
将参数的顺序反转为socat
,它可以工作.
Reverse the order of your arguments to socat
, and it works.
socat -v tcp-l:6666,reuseaddr,fork unix:/var/lib/mysql/mysql.sock
这指示socat
要
- 在TCP端口6666上侦听(使用
SO_REUSEADDR
) - 等待接受连接
- 建立连接后,进行分叉.在孩子中,继续以下步骤.在父级中,转到2.
- 打开到
/var/lib/mysql/mysql.sock
套接字的UNIX域连接. - 在两个端点之间传输数据,然后退出.
- Listen on TCP port 6666 (with
SO_REUSEADDR
) - Wait to accept a connection
- When a connection is made, fork. In the child, continue the steps below. In the parent, go to 2.
- Open a UNIX domain connection to the
/var/lib/mysql/mysql.sock
socket. - Transfer data between the two endpoints, then exit.
反之亦然
Writing it the other way around
socat -v unix:/var/lib/mysql/mysql.sock tcp-l:6666,reuseaddr,fork
不起作用,因为这指示socat
要
doesn't work, because this instructs socat
to
- 打开到
/var/lib/mysql/mysql.sock
套接字的UNIX域连接. - 在TCP端口6666上侦听(使用
SO_REUSEADDR
) - 等待接受连接
- 建立连接后,产生一个工作子进程以在两个地址之间传输数据.
- 父级继续接受第二个地址上的连接,但不再有第一个地址可用:它已分配给第一个孩子.因此,从现在开始,没有什么可以做的.
- Open a UNIX domain connection to the
/var/lib/mysql/mysql.sock
socket. - Listen on TCP port 6666 (with
SO_REUSEADDR
) - Wait to accept a connection
- When a connection is made, spawn a worker child to transfer data between the two addresses.
- The parent continues to accept connections on the second address, but no longer has the first address available: it was given to the first child. So nothing useful can be done from this point on.
这篇关于如何将本地unix套接字映射到inet套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!