Home > Database > Mysql Tutorial > body text

How to use ssh tunnel to connect to mysql server

黄舟
Release: 2017-03-06 13:45:15
Original
1327 people have browsed it

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]
Copy after login

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
Copy after login

Then execute locally:

$ mysql -u root -P 12345 -h 127.0.0.1 -p
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!