I want (as a first step towards an external mysql proxy) reroute traffic from the local connection of mysql (on a VM) to an external proxy (on the host) which then routes it back to the VM which will then give it to the mysql server. I am using this setup as the only thing I want to manipulate is the mysql configuration (the used port) to be mostly webapp independent.

Should look roughly like this:

 :4446 incoming connection on port of guest (1)
  +--------------:3305+ rerouted to host (2)
  +:8888--------------+ which then routes it back to the guest (3)
 :4444 which is then routed to the MYSQLServer port (4)

I am using for the different steps

(1)   nc -k -l -v 4446 | nc 3305
(2/3) nc -k -l -v 3305 | nc 8888
(4)   nc -k -l -v 8888 | nc localhost 4444

I configured mysql to listen on port 4444. I want to keep (4) even though you could configure mysql to listen on external connections as well to keep changes to the config file to a minimum and let mysql think that the connection established comes from localhost.

Now to the problem I am experience:

  • I cannot use an arbitrary order of the commands. I have to use the inverse order as else no connection will be established and it breaks as soon as I try to send data. This will give me a major headache as soon as I'll try to automize that setup as it seems to be impossible to time that within startup of the guest system.

  • it does not work with mysql. Telnet does, though, as long as you leave out the nc to the mysqlserver port. But as soon as this is used, as well, (4) will break when trying to connect with telnet. Connecting with the mysql client will not have any effect at all and plainly not work - no connection will be established and after entering the password for mysql nothing will happen anymore.

Am I completely insane by considering this setup? If not how can I make it (realiably) work?

A solution not involving nc and solving the overall question as described in the first paragraph will just as happily be accepted as fixing the nc setup. The problem described above shall only describe my current approach for a solution.

  • Would not redir be more appropriate for your needs than nc? linux.die.net/man/1/redir – Rui F Ribeiro Mar 2 '16 at 14:43
  • @RuiFRibeiro - yes perfect. I am using redir now and it works like a charm. Feel free to post this as a response so I may accept it. – Sim Mar 6 '16 at 10:50

redir is far more appropriate than nc for redirecting ports.

While nc is more lightweight and appropriate for one-shot tests, for more serious use redir is more appropriate; nc also does not lends itself to handle reconnections or errors without the support of auxiliary tools and more complicated setups like having xinetd.

redir is also a very lightweight tool that handles pretty well redirecting one TCP port to another with the inherent retries/error handling.

Tcp port redirections with redir

Suppose that the ip address of our system is and we would like to redirect all the traffic which coming from port 80 to a remote server with ip address of and port 8080. Simply, we want redir utility to redirect connections coming to on port 80 to port 8080. We have to run redir such as below to do so:

redir --laddr= --lport=80  --caddr= --cport=8080
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.