MySQL Over SSH – Connecting to MySQL Over SSH Tunnel

MySQLsshtunnel

I can ssh onto the remote server and login without a problem.

I'm trying to connect to a remote mysql db over a ssh tunnel and running into issues. I've created the tunnel successfully (verified by telnet). When I try to login with the command:

mysql --host=127.0.0.1 -P 3302 -u fakeuser -p

I get

Access denied for user 'fakeuser'@'192.168.100.93'

The issue is the @192.168.100.93. I believe I need that to be fakeuser@localhost. Since I'm connected through the SSH tunnel why isn't it being set to localhost? How do I force it to localhost? where is it getting the 192.168.100.93 from?

Note that I can't change any settings on the MySQL server.

Thanks for the help!

Best Answer

When you do ssh tunnel it make a encrypted communication between your system to remote server and bind the remote opened port to your defined port.

ssh -L 33333:localhost:3306 fakeuser@server.remote.com

here 3306 as you said is mysql port no.

use IPADDRESS instead of localhost i.e 127.0.0.1

Connection to 10.10.0.31 closed.
linux@tuxworld:~$ ssh -fNg -L 33333:localhost:3306 root@10.10.0.31
root@10.10.0.31's password: 
linux@tuxworld:~$

See below eg which I did short while ago, mysql user is root and ssh user is also root. I opened a new terminal

linux@tuxworld:~$ mysql -u root -h 127.0.0.1 -P 33333
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.69 Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> exit
Bye
linux@tuxworld:~$