MySQL connection over SSH tunnel - how to specify other MySQL server?

て烟熏妆下的殇ゞ 提交于 2019-11-26 08:48:01

问题


I\'ve got ~50 databases all set up at different host names, with the requirement that I connect to them through an SSH tunnel.

For example:

  • SSH host at ssh.example.com

  • MySQL host at mysql1.example.com

I have managed to create the tunnel using autossh (web server running Debian), but I can\'t seem to figure out how to connect to a specific MySQL hostname \"beyond\" the SSH tunnel.

Typing lsof -i -n | egrep \'\\<ssh\\>\' confirms that the tunnel is working (sends port 3307 to ssh.example.com port 3306)

So when I try mysql -h 127.0.0.1 -P 3307 I get Connection refused. Not too weird since it\'s not an MySQL server.

My question to you guys:

How do I specify the mysql1.example.com host AFTER creating the SSH tunnel? I\'ve tried searching everywhere but can\'t seem to figure it out.


回答1:


Solved it! The thing was to connect to the correct server when creating the tunnel itself - should've seen that one coming.

ssh -f user@ssh.example.com -L 3307:mysql1.example.com:3306 -N

Then mysql -h 127.0.0.1 -P 3307 worked as intended. :)




回答2:


I got a nice blog about how to connect to MySQL using SSH tunnel. It is very well explained here.

The command to connect to SSH tunnel:

ssh -L [listening port]:[REMOTE_MYSQL_HOST]:[REMOTE_MYSQL_PORT] [SSH_USER]@[SSH_HOST]

The command to connect to MySQL:

localhost:[listening port]



回答3:


When you don't have direct access to mysql-server, you use jump-server.

From your machine, you connect(ssh) to jump-server and from there you connect to your mysql-server.

This can be avoided by using ssh- tunneling.

Suppose your

       jump server is `jump-ip`
       mysql server is `mysql-ip`
       your machine is `machine-ip`

Just open ssh client(Putty in windows or terminal in linux/ios).

Type:

    ssh -L [local-port]:[mysql-ip]:[mysql-port] [jump-server-user]@[jump-ip]

After this, you can use your localhost and local-port to access mysql-server on the remote machine directly.

Eg. Your Jdbc url to access mysql database, in that case, will be

jdbc:mysql://localhost:[local-port]/[database-name]



回答4:


I created a tunnelto the DB using this command

ssh  -L 10000:localhost:3306  user@<ip addess to connect DB> -N -f

-L is local host port it is user defined u can provide any port number

between 0 to 65535. 0 to 1023 are reserved.

whether you are using key based authentication to the server you should

mention the key like this.

ssh -i <path of the private key>  -L 10000:localhost:3306  user@<ip addess to connect DB> -N -f


来源:https://stackoverflow.com/questions/18373366/mysql-connection-over-ssh-tunnel-how-to-specify-other-mysql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!