Method of linking mysql server with ssh tunnel

  • 2021-07-06 11:58:21
  • OfStack

Preface

In some cases, we can only know the intranet address of the database and cannot connect through the intranet, but we can log in to other machines in the same intranet and these machines can access the database through the intranet, so we can access the database through ssh tunnel.

Usage

ssh tunnel is simple to use, and the specific format is as follows:


ssh -L [local port]:[remote host]:[remote port] [username]@[remote host]

Use -L Will access to the local port through ssh, tunnel, forward to the port of the remote host. In actual use, we can also add-f and-N parameters to make this ssh command work in the background without executing any commands.

Practice

Suppose that the intranet ip of our mysql server is 10.86. 22.22, which is opened on port 3306, and the extranet ip of another server in the same intranet as mysql is 121.43. 23.12.

First, execute locally:


$ ssh -f -N -L 12345:121.43.23.12:3306 root@121.43.23.12

Then execute it locally:


$ mysql -u root -P 12345 -h 127.0.0.1 -p

After entering the password, you can log in to mysql.

After execution, we can find the sshd connection just established through ps, and then drop its kill, and then we can close the ssh tunnel just established.

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: