mysql socket socat,如何将本地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.

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

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.

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.

Thanks.

解决方案

Reverse the order of your arguments to socat, and it works.

socat -v tcp-l:6666,reuseaddr,fork unix:/var/lib/mysql/mysql.sock

This instructs socat to

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

doesn't work, because this instructs socat to

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值