ssh reverse mysql tunnel with a aliased host - mysql

I am trying to ssh into my server and then access the mysql on localhost for quick prototype development when testing on live.
Here is my connection in the server:
mysql -h db_master_www -u game -D db_www -p
I then setup my tunnel (sp is my ssh config name)
~ssh -N -L 3336:db_master_www:3306 sp
And on my local machine
hutber#hutber:~$ mysql -h db_master_www -u game -D db_www -p
ERROR 2005 (HY000): Unknown MySQL server host 'db_master_www' (-2)
So logically I'll need to use my host as the ip and not the alias?

When you use an ssh tunnel to map the local port 3336 to the remote port 3306, you would connect to 3336 locally:
mysql -h 127.0.0.1 -P 3336 -u game -D db_www -p
Make sure to use 127.0.0.1, not "localhost" because to the MySQL client, these are not the same.

Related

Mysql-Client Jumphost from Local Client to Webserver to DB Server without DB Server SSH User

My scenario looks as follows:
I have a web server and a DB server. On the webserver I have an SSH user with which I can connect to the database via mysql-client. The mysql port is restricted to the IP address of the webserver and I don't have an SSH user on the DB server.
I thought that some kind of SSH tunnel should be possible here, but I couldn't wrap my head around it yet.
I would have imagined something along these lines:
Tunnel: 127.0.0.1:9999 -> Webserver.IP:9999 -> DBserver.IP:3306
Mysql-Client CMD: mysql -u db_user -h 127.0.0.1 -p -P 9999
If anyone can share an idea on if and how this can be implemented I would be very grateful.
I have found a solution that works for me..
Establish connection and keep it open:
ssh -L 9999:[DB-SERVER.IP]:3306 [SSH-USER]#[WEB-SERVER.IP]
Connect to the local source port:
mysql -h 127.0.0.1 -P 9999 -u [DB-USER] -p

Localhost connect MySQL Docker confusion

I created a test_mysql using the following command:
docker run -d -p 3306:3306 --name=test_mysql --env="MYSQL_ROOT_PASSWORD=123" mysql
I got the IP address using docker inspect test_mysql. The IP is 172.17.0.2.
The strange thing is that when I tried to connect to mysql server on my local using
mysql -uroot -p123 -h 172.17.0.2 -P 3306
An error raised:
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (51)
However, if I use the localhost IP address instead it did work:
mysql -uroot -p123 -h 127.0.0.1 -P 3306
My question is why I can't connect to the container use docker inspect result while localhost IP works?
1) while localhost IP works?
Let's see again your command to start container:
docker run -d -p 3306:3306 --name=test_mysql --env="MYSQL_ROOT_PASSWORD=123" mysql
The -p 3306:3306 will "bind" the port 3306 of host to the port 3306 of the container. As a result, we can see that if there is any connections come to port 3306, they will be forwarded to the port of the container.
So, your connections on local IP will work:
mysql -uroot -p123 -h 127.0.0.1 -P 3306
See more detail on Docker page
2) why I can't connect to the container use docker inspect result
It seems your container is connected to the default bridge network(docker0 may have IP:172.17.0.1 in your case ) which is often created by default when you install Docker.
You can find more detail in Docker network page.Therefore, inside your container, you may "see" the bridge (you can try to use ping command", but from your local host, it may not know how to find/resolve the 172.17.0.2 and you got that error.

how to connect remote docker container mysql server

is there any way to connect remote docker container mysql server?
I am installing magento web application, now I have a situation like I need to use/point the existing remote docker container database. I have make port forwarding in order to access database from remote machine but it doe not work.
docker run -it -d -p 3002:80 -h tm.gworks.mobi -v /var/www/public --privileged --name database magedev
For testing purpose in remote machine I have tried like mysql -u root -h 192.168.1.21:3002 -p in mysql console but it does not connect, it throws error ERROR 2005 (HY000): Unknown MySQL server host '192.168.1.21:3002' (-2)
Docker run command should be,
docker run -it -d -p 3002:3306 -h tm.gworks.mobi -v /var/www/public --privileged --name database magedev
default mysql port is 3306 but I listen port 80 which is my nginx port so it can't be to allow.
mysql -u root -h 192.168.1.21 -P 3002 -p
now everything works fine

SSH port Tunnel for MySQL works with mysql command line but not WordPress

I created an SSH port tunnel:
ssh -f -N -L 3306:localhost:3306 myuser#thedomain.com
On that same machine where I created the port tunnel, I can connect to mysql by doing:
mysql -h 127.0.0.1 -u user -p
On that same machine, if I try to make Wordpress connect to MySQL, none of the following work:
/** MySQL hostname - NONE of these work */
define('DB_HOST', 'localhost');
//define('DB_HOST', 'localhost:3306');
//define('DB_HOST', '127.0.0.1');
//define('DB_HOST', '127.0.0.1:3306');
Why can't WordPress connect?
I tried restarting httpd but that didn't fix the issue.

Connect to MySQL via ssh tunnel to localhost

I want to connect to remote MySQL via ssh tunnel with user that has 'localhost' access.
I use this to make a tunnel:
ssh -f -N -L 33306:localhost:3306 user#remote-host
and this to connect to host:
mysql -h 127.0.0.1 -P 33306 -uuser -ppassword
The error i get is:
ERROR 1045 (28000): Access denied for user 'user'#'remote-host' (using password: YES)
The problem is that user 'user'#'remote-host' (or 'user'#'%') does not exist, only 'user'#'localhost' does.
Is there a way to force remote host, without server-side modifications into thinking that i come from localhost? That's the only reason I would do the connection via ssh tunnel anyway.
Note:
If I want to connect with this command:
mysql -h localhost -P 33306 -uuser -ppassword
I get this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Additional data:
On remote server in /etc/hosts the values are like this:
127.0.0.1 localhost
remote-ip remote-host
The simple way to create MySQL Tunnel to REMOTE HOST:
$ ssh -fNL TEMP_PORT:localhost:MYSQL_SERVER_PORT USER#SERVER_NAME
Test:
$ mysql -u root -p -h 127.0.0.1 -P TEMP_PORT
Please note that localhost and 127.0.0.1 are treated differently in mysql on unix.
Quoting:
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
http://dev.mysql.com/doc/refman/5.7/en/connecting.html:
Furthermore, mysql client would silently try to use a socket file even if you explicitly specify -P on your command line:
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
Effectively, using this command
mysql -h localhost -P 33306 -uuser -ppassword
you're simply trying to connect to your local mysqld which is missing
Considering this, your question boils down to connecting to a remote server available over a domain socket.
If installing additional software meets your requirements 'without server-side modifications' you could use socat as described here:
https://www.debian-administration.org/users/dkg/weblog/68.
Tailored for mysql, it could work as follows:
install socat on both ends
socat "UNIX-LISTEN:your_local_path/mysql.sock,reuseaddr,fork" EXEC:'ssh user#remote-host socat STDIO UNIX-CONNECT\:/your_server_path/mysql.sock"
mysql -S your_local_path/mysql.sock -u user