Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 711
alesf Avatar asked Jan 30 '13 14:01

alesf


People also ask

How do I access MySQL server on localhost?

Connecting via a standard connectionEnter 127.0. 0.1 for the host. The default username for a new MySQL installation is root, with a blank password. You can leave the port field blank unless your server uses a different port than 3306.


1 Answers

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
like image 93
Cubiczx Avatar answered Sep 30 '22 08:09

Cubiczx