简体   繁体   中英

netcat for MySQL connection forwarding

This question is in relation to

Dockerized web app connecting to MySQL DB on host

I am trying to open up a connection from a docker container to the host to support MySQL connections.

The way I understand it I should be able to execute the following in my container

nc.traditional -l -p 3306 -c "nc.traditional 172.17.42.1 3306" &

to open up a tunnel from the Docker container port 3306 to the host (IP 172.17.42.1) MySQL instance, running on port 3306.

Trouble is as soon as I try to connect from the container

mysql --host=127.0.0.1 --port=3306 -uroot -ppassword

I get an error and the tunnel exits

root@7ec710b77baf:/var/log# mysql --host=127.0.0.1 --port=3306 -uroot -pAcc355
(UNKNOWN) [172.17.42.1] 3306 (mysql) : Connection refused
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
[1]+  Exit 1                  nc.traditional -l -p 3306 -c "nc.traditional 172.17.42.1 3306"

Why would the tunnel exit? What am I doing wrong? It certainly seems to contact the MySQL instance as I get a different error message when I try a different port.

I haven't been able to find any info in logs or on std out to help.

Any ideas?

From my experience you're probably after socat rather than netcat.

eg

socat TCP-LISTEN:3306,fork TCP:db-host:3306

I've found issues with netcat handling the connection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM