Lost connection to MySQL server while running query in Docker - mysql

I'm running a simple query on the MySQL server (5.7.20) inside Docker container.
Query is run from a BASH script inside the same container. Variables are exported and defined. Credentials are checked and valid.
mysql --user="root" --password="${MYSQL_ROOT_PASSWORD}" --show-warnings --verbose -e "DROP DATABASE IF EXISTS ${MYSQL_TEST_DB};"
There is no firewall problem since the MySQL server is binned to 127.0.0.1. Running and connecting also from localhost.
/etc/hosts does have a 127.0.0.1 localhost defined.
Service is running and I'm able to login to MySQL over CLI with the same credentials.
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
Full error message shows
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading
initial communication packet', system error: 104
Not finding anything useful on Google or SO and running out of ideas.
Most of the answers are connected to firewall or timeout answers.

Related

Can't connect to Google Cloud SQL using socket, but can do so using the IP address

I have an existing PHP service running on a Compute Engine VM Instance and a Google Cloud SQL service.
After I login to the VM and run this command
mysql -h <IP> <db> -umyuser -p
I can connect to the mysql instance with no problem.
However, if I try to use socket
mysql -h localhost --socket=/home/forge/.gcp/cloudsql/proxy/socket/project:region:production
I get this error
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 95
This is an existing setup and I am not very familiar with GCP. But, this was working until yesterday after which it started to fail with this error
Did I miss any configuration change?

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

Connecting to MySQL via SSH ERROR 2013 (HY000)

The Setup
I am currently using the Premium Wordpress Hosting provided by MediaTemple. I have a very large data-set to import and I was hoping to get direct access to the database via an SSH tunnel.
--------------- ------------------- ------------
| My Machine | ---- SSH TUNNEL -----| Hosting Server | -- -- ? -- -- | Database |
--------------- ------------------- ------------
What Works
If I ssh into the Hosting Server and from the shell on the Hosting Provider, connect to mysql like this, I am able to get into MySQL.
mysql -uuser -ppassword -h123.456.789.1 -P3308
What Does Not Work
However, if I try to connect to MySQL using the -L flag with SSH to create a tunnel, I am unable to connect to the server.
ssh me#hostingserver.net -L 7002:123.456.789.1:3308
From a shell on My Machine:
mysql -uuser -ppassword -h127.0.0.1 -P7002
I get the following error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
From reading other answers (StackOverflow , StackOverflow), I have reasoned that the issue stems from the IP address with which MySQL client tries to bind. I think that the ip address attach to the request to connect, when executed on my machine, is not on the white-list of the Database Server.
Is there anyway to get direct access to the MySQL Database from My Machine. From a system administration perspective, I obiviously have enough access to connect to the MySQL database from the shell but I cannot run the client on My Machine. I have a very large dataset that I would like to transfer from My Machine to Database. I would also like to be able access the database and exicute SQL whenever I need to. This and the large dataset kind of eliminates the possibility of just using a the source command from the MySQL Client on Hosting Server. What is the best workaround to give me something close to the ability to run SQL on the Database from My Machine?
I encountered roughly the same issue. That is, I simply could not connect to the MySQL server, even though I had successfully tunneled to the remote host.
TLDR: it was an iptables issue involving the loopback interface
In my situation, mysqld was running on the same VPS as sshd. However, the MySQL instance was bound only to 127.0.0.1 and listening on the default port. As you did, I confirmed that I could connect to the mysqld instance on the remote machine using the credentials used locally.
Here is the tunnel:
ssh -v -N -L 33306:127.0.0.1:3306 sshuser#sshanddbvps.org
Here is the connection string to the mysqld instance using the mysql client:
mysql -umysqluser -h127.0.0.1 -P 33306 -p
Even though ssh indicated that the connection was successful...
debug1: Connection to port 33306 forwarding to 127.0.0.1 port 3306 requested.
...the mysql client connection would error out after accepting the correct password with the message you mentioned:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet'...
To check that data was flowing across the loopback interface, I logged into the remote server and ran three commands in three separate shells:
while true; do echo -e "HTTP/1.1 200 OK\n\n $(date)" | nc -l 127.0.0.1 1234; done
tcpdump -i lo src 127.0.0.1 -or dst 127.0.0.1
nc 127.0.0.1 1234
After running the third, output from the second command appeared:
13:59:14.474552 IP localhost.36146 > localhost.1234: Flags [S], seq 1149798272, win 43690, options [mss 65495,sackOK,TS val 48523264 ecr 0,nop,wscale 7], length 0
But nothing indicating that packets were flowing in the reverse direction.
Inserting a rule in the INPUT chain of the firewall that allowed traffic from the default loopback address solved the issue:
iptables -I INPUT 4 -i lo -s 127.0.0.1 -j ACCEPT

Connecting to MySQL db

When I attempt to connect from Windows 7 using 32bit and 64bit mysql-connector-odbc-5.3.2 and the connectionstring:
Provider=MSDASQL;Driver={MySQL ODBC 5.3 Driver};Server=192.168.1.13;Port=3306;Database=mydb;Uid=root;Pwd=****;
I get
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Likewise with version 5.2.6. After downgrading the provider to 5.1 I get:
[MySQL][ODBC 5.1 Driver]Can't connect to MySQL server on '192.168.1.13' (10060)
which is more encouraging as it has at least recognised the provider.
I have tried adding a firewall rule to allow outbound connections to port 3306. I've tried disabling my local firewall. I have checked that the server is listening on port 3306 and verified the IP address. (tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN) I have further verified that I can access the server and that MySQL is running.
When I attempted to connect using the IP address from an ssh terminal to the server itself:
mysql -u root -h 192.168.1.13 -D whiskeywheel -p
I am prompted for the password after which I get:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.13' (111)
I have added the host/user to the user table in the mysql database and restarted MySQL. I've granted 'ALL' on the database to the user at anyhost ('%') and I've explicitly added access to port 3306 to the ubuntu firewall:
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
I will of course restrict access prior to deployment but I need to be able to interact with the database in a meaningful form while I'm developing it. I'm happy to re install any of the various components and start again as this is early enough in the project but ideally I'd like to know what I've misconfigured or where I went wrong.
mysql server by default only bind to the loopback address on the sever (127.0.0.1). you have to edit the my.cnf file of the server and do a restart of the mysql server.
also note that specifying localhost is treated special by mysql server. it does not call 127.0.0.1 as one would expects.
more info here on how to bind to your external ipaddress:
http://www.cyberciti.biz/faq/unix-linux-mysqld-server-bind-to-more-than-one-ip-address/

MySQL remote access via SSH Tunnel error

I've created an SSH tunnel on my local machine to reach an access to the mysql server on my remote machine. Everything is fine, tunnel is working if I get this error, but why this error occurs?
Here is the error message:
2013 - Lost connection to MySQL server at 'reading initial communication packet', system error: 0
It happends only, when I want to connect via Navicat (SSH Tunnel) from my local to the remote machine, on the remote machine mysql works without such errors.
Have you guys any solutions for that?
This error occurs when the configuration of the bind-adress option is wrong. Your MySQL server at least should listen on the localhost - 127.0.0.1
Login to your local Linux machine then run this command:
ssh -L 3307:localhost:3306 -N user#remote.server.com
Use the mysql database from remote server:
mysql -u root -p --port 3307 -h 127.0.0.1
Make sure the local mysql does not use the same port in Linux environment.