Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect MySQL database through two SSH hosts

Tags:

mysql

ssh

How do I connect to MySQL database through two SSH hosts ?

Here is my situation: I have two SSH hosts as show below. MySQL is running in BOX2 host.

MyBox(MySQL GUI client) -----SSH(port 22)--->BOX1(IP: 190.xx.xx.xx)---SSH(port 22)--->BOX2(IP: 190.xx.xx.xx)[MySQL server]

My question is how do I connect if I have multiple SSH hosts to tunnel as shown above ?

like image 834
Sathish D Avatar asked Apr 05 '12 06:04

Sathish D


People also ask

How do I connect to a remote MySQL server using SSH?

Open a new terminal window. Direct your local MySQL client to 127.0. 0.1:3306 with the MySQL server username and password. Your connection to the remote MySQL server will be encrypted through SSH, allowing you to access your databases without running MySQL on a public IP.

Can MySQL handle multiple connections?

By default 151 is the maximum permitted number of simultaneous client connections in MySQL 5.5. If you reach the limit of max_connections you will get the “Too many connections” error when you to try to connect to your MySQL server. This means all available connections are in use by other clients.


2 Answers

I think this will help. Try this command from MyBox (having the MySQL Client)

ssh user@box1 -L some-local-port:box2:22

Now from the Client try to connect to connect this way

MySQL host : localhost
MySQL usernmae : mysql-username-of-server-installed-at-Box2
MySQL password : mysql-password-of-server-installed-at-Box2
MySQL port : 3306 (suppose mysql server at Box-2 is listening at port 3306)

In tunnelling info

SSH host : localhost
SSH pass : ssh-password-of-Box2
SSH port : some-local-port
like image 73
Rituparna Kashyap Avatar answered Oct 05 '22 03:10

Rituparna Kashyap


Try the following:

ssh -L 33306:localhost:33306 user@box1_hostname ssh -L 33306:localhost:3306 user@box2_hostname

Then try connecting to mysql using 127.0.0.1:33306. Note that port 33306 will be available to anyone on box1 which may or may not be an issue for you.

Another way to accomplish the same is to use ProxyCommand as explained here.

like image 31
BluesRockAddict Avatar answered Oct 05 '22 03:10

BluesRockAddict