This article mainly introduces you to the method of using ssh tunnel to connect to the mysql server. The introduction in the article is very detailed. Friends in need can refer to it. Let’s take a look together.
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 the same intranet Other machines, and these machines can access the database on the intranet, then we can access the database through ssh tunnel.
Usage
The method of using ssh tunnel is very simple, the specific format is as follows:
ssh -L [local port]:[remote host]:[remote port] [username]@[remote host]
Use the -L
method to forward access to the local port to the port of the remote host through the ssh tunnel. In actual use, we can also add the -f and -N parameters to make the ssh command work in the background without executing any commands.
Practice
Assume that the intranet IP of our mysql server is 10.86.22.22, and it is opened on port 3306, and the other one is in the same intranet as mysql The external IP of the server 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 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 kill it to close the ssh tunnel just established.
Summary
The above is the content shared by using ssh tunnel to connect to the mysql server. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!