netcat for MySQL connection forwarding - mysql

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

Related

How do I connect to MySQL unix socket via SSH tunnel using HeidiSQL?

I'm properly establishing SSH tunnel and I can connect to MySQL users accesible via tcp connection but I don't know how to connect to users accesible only by unix socket. I know the correct socket location: /var/lib/mysql/mysql.sock and I can connect to it from the command line.
How do I connect via socket using HeidiSQL? Is this impossible when SSH tunnel is being used? When I enter '/var/lib/mysql/mysql.sock' into "Host/IP" then I get
Lost connection to MySQL server at 'handshake: reading initial communication packet', system error 22
UPDATE
My HeisiSQL connection settings:
Servers\ATLANTIS\SessionCreated<|||>1<|||>2020-08-14 13:03:22
Servers\ATLANTIS\Host<|||>1<|||>/var/lib/mysql/mysql.sock
Servers\ATLANTIS\WindowsAuth<|||>3<|||>0
Servers\ATLANTIS\CleartextPluginEnabled<|||>3<|||>0
Servers\ATLANTIS\User<|||>1<|||>atlantis
Servers\ATLANTIS\Password<|||>1<|||>7
Servers\ATLANTIS\LoginPrompt<|||>3<|||>0
Servers\ATLANTIS\Port<|||>1<|||>3306
Servers\ATLANTIS\NetType<|||>3<|||>2
Servers\ATLANTIS\Compressed<|||>3<|||>0
Servers\ATLANTIS\LocalTimeZone<|||>3<|||>0
Servers\ATLANTIS\QueryTimeout<|||>3<|||>30
Servers\ATLANTIS\KeepAlive<|||>3<|||>20
Servers\ATLANTIS\FullTableStatus<|||>3<|||>1
Servers\ATLANTIS\Databases<|||>1<|||>
Servers\ATLANTIS\Library<|||>1<|||>libmariadb.dll
Servers\ATLANTIS\Comment<|||>1<|||>
Servers\ATLANTIS\StartupScriptFilename<|||>1<|||>
Servers\ATLANTIS\TreeBackground<|||>3<|||>536870911
Servers\ATLANTIS\SSHtunnelHost<|||>1<|||>atlantis.localdomain
Servers\ATLANTIS\SSHtunnelHostPort<|||>3<|||>22
Servers\ATLANTIS\SSHtunnelUser<|||>1<|||>atlantis
Servers\ATLANTIS\SSHtunnelPassword<|||>1<|||>6
Servers\ATLANTIS\SSHtunnelTimeout<|||>3<|||>4
Servers\ATLANTIS\SSHtunnelPrivateKey<|||>1<|||>C:\secure-folder\private-key.ppk
Servers\ATLANTIS\SSHtunnelPort<|||>3<|||>3307
Servers\ATLANTIS\SSL_Active<|||>3<|||>0
Servers\ATLANTIS\SSL_Key<|||>1<|||>
Servers\ATLANTIS\SSL_Cert<|||>1<|||>
Servers\ATLANTIS\SSL_CA<|||>1<|||>
Servers\ATLANTIS\SSL_Cipher<|||>1<|||>
Servers\ATLANTIS\IgnoreDatabasePattern<|||>1<|||>
Servers\ATLANTIS\RefusedCount<|||>3<|||>5
Remotely, the command should be executed like: mysql -u mySqlUser -p mySqlPw -h localhost.
ssh remote-user#remote "mysql -u mySqlUser -p mySqlPw -h localhost"
As from there, you should be able to do your stuff

cannot start vault container with mysql storage

I'm trying to start Vault docker container with mysql storage using this command:
docker run --cap-add=IPC_LOCK -e 'VAULT_LOCAL_CONFIG={"storage": {"mysql": {"username":"root", "password":"hello", "database":"vault", "address":"127.0.0.1:3306"}}, "listener": {"tcp":{"address":"127.0.0.1:8200", "tls_disable":"1"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h"}' -e VAULT_SKIP_VERIFY=true vault server
This is the error I'm getting:
Error initializing storage of type mysql: failed to check mysql schema
exist: dial tcp 127.0.0.1:3306: connect: connection refused
I can connect to mysql using the username and password I am supplying to the previous command.
I also made sure that the mysql is running on the 3306 port
[root#jwahba]# netstat -tlpn | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 39552/mysqld
I checked out the vault official document (here) but it's not obvious what is wrong in my configuration. Any suggestions please ?
You are trying to connect to a db on localhost from a Docker container, but they are on different network stacks. Use --net="host" in your docker run command; 127.0.0.1 in your docker container will now point to your docker host.
Source: From inside of a Docker container, how do I connect to the localhost of the machine?

SSH Tunnel MySQL Connection with socket-connection via PhpStorm

By default, Database Manager from PhpStorm works well. But currently on a special Provider (1u1.de) I have some trouble to got this work.
I can connect to the Provider via SSH. If I want to connect to MySQL database, I have to use:
mysql --host=localhost --user=dbo123123123 -S /tmp/mysql5.sock --password='123123123';
That's works well via CLI on Server, but I didn't find a way to connect via PhpStorm to this Database.
For me it seems that the "socket-connection" may be the Problem. Does anybody have a clue how to got this to work?
Part of the Solution (?!):
Maybe a first part of an solution, I found that you be able to forwarding an Socket to your local pc as own socket this way:
ssh -nNT -L $(pwd)/yourLocal.sock:/var/run/mysqlREMOTEMYSQL.sock user#somehost
Source of Information
This show me, that the Socket is established:
netstat -ln | grep mysql
unix 2 [ ACC ] STREAM LISTENING 3713865 /myFolder/mysql5.sock
But I'm still unable to connect to this Socket with:
mysql -h localhost --protocol=SOCKET -u'username' -p'mypassword' -S /myFolder/mysql5.sock
Got this Error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 95 "Operation not supported"
ssh -L /tmp/mysql.sock:/var/run/mysqld/mysqld.sock sshuser#remotehost
and then
mysql -h localhost --protocol=SOCKET -u'username' -p'mypassword' -S /tmp/mysql.sock
seems to work fine for me
Use SSH to setup a port forward, this will allow you to connect securely to your database without exposing it to the world.
On ssh, use the -L argument to establish the tunnel.
ssh -L <local_port>:<remote_host>:<remote_port> user#host
This will open <local_port> on your local machine, and then redirect all packets out the other side of the tunnel, destened for the <remote_host>:<remote_port>
In your case, you might want to try something like this:
ssh -L 3306:127.0.0.1:3306 user#mybox.1u1.de
After establishing the tunnel, you will be able to connect to the database through a local port.
From your local machine, not the 1u1 host,
mysql -u <user> -p --host 127.0.0.1 --port 3306
If this works properly, you should be able to configure PhpStorm to use the same address, 127.0.0.1:3306
The SSH tunnel will need to remain open the entire time you need to be connected to the database.

docker mysql on different port

I want to change the default exposed port for mysql docker container, but if i try to use this command:
docker run --detach --name=test-mysql -p 52000:52000 --env="MYSQL_ROOT_PASSWORD=mypassword" mysql
It does not work. mysql -uroot -pmypassword -h 127.0.0.1 -P 52000
Warning: Using a password on the command line interface can be insecure.
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
If I use the standard port 3306:3306 then it works fine, but i want change the port. Is it possibile?
I had already tried -p 52000:3600 , but i have always gotten:
mysql -uroot -pmypassword -h 127.0.0.1 -P 52000
Warning: Using a password on the command line interface can be insecure.
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
You need to map the container-port 3306 on the prefered TCP port (of your server):
-p <host_port>:<container_port> (map container_port xx on host_port yy)
So for your mysql
docker run --detach --name=test-mysql -p 52000:3306 --env="MYSQL_ROOT_PASSWORD=mypassword" mysql
there is also a second option:
don't map a port to another port but let mysql itself run directly on another port using the MYSQL_TCP_PORT-variable.
example:
docker run --detach --name=test-mysql --env="MYSQL_TCP_PORT=52000" mysql

Remote Access MySQL connection error

I am trying to remotely connect to a MySQL server. I have followed advice from (1) and setup a user on the ip address I will be remotely accessing from.
user$ mysql -u TestUser -p -h 129.169.66.149
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '129.169.66.149' (60)
I have checked and the port (default, 3306) is correct and the ip address is correct. MySQL is also running.
From a remote computer, I can successfully ping the server
ping 129.169.66.149
64 bytes from 129.169.66.149: icmp_seq=48 ttl=63 time=1.010 ms
But when I use Telnet:
TELNET 129.169.66.149
Trying 129.169.66.149...
telnet: connect to address 129.169.66.149: Operation timed out
telnet: Unable to connect to remote host
Can anyone advise? Is this a firewall issue?
(1) - https://superuser.com/questions/826896/access-wordpress-mysql-database-remotely
First try to check TCP connection issue, using netcat and telnet : on the mysql server, stop mysqld (to release port 3306) and run netcat on listen mode :
nc -l -p 3306
Now, netcat is listening port 3306 (like mysqld does when it's running) and will show what happen on that port (incoming connections, what's in the wire...).
On your remote computer (mysql client), try to connect to the mysql server host :
telnet 129.169.66.149 3306
or :
nc 129.169.66.149 3306
If this is not working, this is not a mysql server configuration issue but a network issue, and you must check your router firewall rules.
Otherwise, your problem comes from mysql server settings. Check your mysql configuration file for bind-address, and remove them to make mysqld accept clients from any IP address.
The 3306 port on the 129.169.66.149 server is closed you'll have to open this port
Use this reference as a guide http://www.cyberciti.biz/faq/linux-unix-open-ports/