Search code examples
mysqlsshssh-tunnel

ssh tunnel in foreground works for mysql host localhost but ssh-tunnel in background works for mysql host 127.0.0.1


What I am trying to do here is forward all connections to localhost on machine-one on port 3306 to machine-two on localhost on port 3306. So if you are connecting to mysql on machine-one it acts like you are connecting on machine two.

I thought an ssh tunnel was supposed to forward the traffic on a specific port not act like it is logging me into the other machine. (like it is doing here) I have tried without the "admin" before "@machine-two-hostname.com" and that does the same thing. As the title says running this in the background doesn't let me connect on "localhost" it does give me a new message when I try to set up another ssh tunnel on the same ports that "bind address already in use" I'm suspicious that the commands I am running below aren't working either but that it is just logging me into the other machine and not connecting the ports.

admin@machine-one:~$ ssh -L 3306:localhost:3306 admin@machine-two-hostname.com
Linux machine-two 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/\*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Jun 20 11:16:07 2018 from 172.31.93.22
admin@machine-two:~$ mysql -uroot -proot-pass
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> \q
Bye
admin@machine-two:~$ exit
logout
Connection to machine-two-hostname.com closed.
admin@machine-one:~$ ssh -fN -L 3306:localhost:3306 admin@machine-two-hostname.com
admin@machine-one:~$ mysql -uroot -proot-pass
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

running in background:

admin@machine-one:~$ ssh -fN -L 3306:localhost:3306 machine-two-hostname.com
admin@machine-one:~$ mysql -uroot -proot-pass -hlocalhost
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

UPDATE when running in the background the mysql connection works when I use 127.0.0.1 instead of localhost, why?

admin@machine-one:~$ mysql -uroot -proot-pass -hlocalhost
ERROR 2002 (HY000): Cant connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
admin@machine-one:~$ mysql -uroot -proot-pass -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

Solution

  • Found the answer here: Can't connect to local MySQL server through socket error when using SSH tunel

    "On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:

    shell> mysql --host=127.0.0.1
    shell> mysql --protocol=TCP
    

    from mysql documentation: https://dev.mysql.com/doc/refman/8.0/en/connecting.html

    So this works after I set up the tunnel in the background:

    admin@machine-one:~$ mysql -uroot -proot-pass --protocol=TCP -hlocalhost
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 23
    Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1
    
    Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> 
    

    I would like to know however if my ssh tunnel in the foreground is actually working the way it should be, is it supposed to log you into the other machine?