Setup ssh tunnel for mysql to mysql server that is accessed through ssh tunnel itself - mysql

There is a MySQL server running inside a VM(1) that do not have any port forwarding to public IP and can only be accessed inside the VMWare network. I have an ssh connection to a sibling VM(2) though which I can connect to the VM(1) and can also setup an ssh tunnel to it. How can I connect to MySQL on my local machine? I tried setting up a tunnel on VM(2) like this
ssh -fNTM -S $(dirname "$0")/tmp/$ENV-mysql-socket -L "$mysql":"$mysql_server" "$login"#"$ip" "$ENV.server"
But when I try to connect to it on my machine an error occurs:
handshake: reading initial communication packet

Related

Connecting Jetbrains DataGrip to a remote database via SSH

I have an AWS Lightsail instance running the Bitnami configuration for Ubuntu 16.04.5 LTS and PHP7.
I can connect the the MySQL database using phpMyAdmin on the server via an SSH Tunnel. But when I try to configure DataGrip for SSH access to the database I receive the error message "Host '127.0.0.1' is not allowed to connect to this MySQL server".
When I remove "localhost" from the host field of the General tab then I receive the error "ExecutionException: SSH: Unable to find host/port in URL".
The following test and response appears to show a successful connection to the port:
cat < /dev/tcp/###.###.###.###/22
SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.6
I'm using keypair auth. Both the user name and the Private key file are the same used for the ssh tunnel when running phpMyAdmin.
Not sure what to try next. Any help appreciated.

MySQL: Unable to connect using TCP/IP over SSH

We have an EC2 and RDS instance on Amazon Web Services. I usually first connect to the server using Putty and then access the database through a tunnel from my localhost.
I am now trying to directly connect to the database using TCP/IP over SSH, but get the following error. I am able to connect other applications such as FileZilla directly without creating any tunnels, but not able to connect MySQL. Please advise. There is no entry in known_hosts file.
Error:
[Window Title]
MySQL Workbench
[Main Instruction]
Failed to Connect to MySQL at database.aws-server.rds.amazonaws.com:3306 through SSH tunnel at username#100.100.100.100:22 with user username
[Content]
Cannot open SSH Tunnel: Error connecting SSH tunnel: The host 100.100.100.100 fingerprints mismatch.
Expected key: hjd83iuwe98jdoiw8euoqeuo83eqe308
Server sent: 8ru84ru8ur984u8djc0938y4jddj398e
Please verify if it's correct.
To continue, delete entries for the host from the C:\Users\<USER>\AppData\Roaming\MySQL\Workbench\ssh\known_hosts file.
[OK]
I am connected to my rds database using the following script-
ssh -L 3406:database.aws-server.rds.amazonaws.com:3306 -i username#100.100.100.100
this command will create a tunnel on your local machine port 3406 of the mysql's default port 3306.
after that you can connect at 3406 port in your application
ssh-keygen -f "C:\Users<USER>\AppData\Roaming\MySQL\Workbench\ssh\known_hosts" -R "database.aws-server.rds.amazonaws.com"

MySQL Server Refusing Local Connections Over SSH Tunnel

I have a remote Windows server with MySQL and Cygwin (enabling remote SSH connections). When I connect from a local instance of Workbench (i.e. with Workbench handling the tunnel), via SSH as root, I have no problem connecting to the remote database.
I have a need to set up my own tunnel because I have another app that needs MySQL access. I chose to use Putty, and had only partial success.
I can connect (using Workbench as a test client) through my tunnel, localhost:3307, which I have forwarded to myremoteserver.no-ip.com:3306, ONLY if I have the remote root user there set to accept connections from all hosts (%), AND/OR the actual IP of the server.
If I restrict root to 127.0.0.1 (as it should be) I get the error Host x.x.x.x is not allowed to connect to this MySQL server where x.x.x.x is the actual address of the remote server!
Somehow, using Putty to make the tunnel isn't presenting me as a local connection. But instead as a remote connection, even though the remote IP is the IP of the remote server
My Question: How can I set up putty to mimic the way Workbench successfully makes tunnel connections?
Note: All my connections to the remote IP are via a no-ip address. This works when Workbench opens the tunnel. Also my server is run within the AWS EC2 ecosystem.
My current Putty connection is to the no-ip address and tunneling settings are Source:3307, Destination: myremoteserver.no-ip.com:3306, Local and Auto.
Edit: I have also tried 127.0.0.1:3306 as the destination address in the Putty tunnel settings and got the same result.
My current Putty connection is to the no-ip address and tunneling
settings are Source:3307, Destination: myremoteserver.no-ip.com:3306,
Local and Auto.
You want to restrict the remote instance of mysql to only accept connections on the localhost interface, so your tunnel should forward to the remote host's localhost interface instead of its network interface. Change the tunnel's destination address to 127.0.0.1 instead of the server's name.

Ping target through ssh tunnel

I want to connect from source server S to my target server T. However connection to T is restricted to only from intermediate server I. Since S can't connect to T, I have created a ssh tunnel from S to T via I using:
ssh -N -f -L port:T:22 I
Now I can connect to T using:
ssh -p port user#localhost
But my problem is that I have to ping to the target T. How can I ping using tunneling.
You can't ping through a tunnel. SSH tunnels can only pass TCP connections, ping uses ICMP.
You could run ping on the intermediate server via SSH:
ssh I ping T

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.