How to use ssh tunnel to connect to mysql server

How to use ssh tunnel to connect to mysql server

Preface

In some cases, we 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 in the intranet, then we can access the database through the ssh tunnel method.

usage

The usage of 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 on the remote host through the ssh tunnel. In actual use, we can also add -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 is enabled on port 3306. The external 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 [email protected]

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 the execution is complete, we can use ps to find the sshd connection that was just established, and then kill it to close the ssh tunnel that was just established.

Summarize

The above is the full content of this article. I hope that the content of this article can be of some help to your study or work. If you have any questions, you can leave a message to communicate.

You may also be interested in:
  • Problems and solutions of error 08001 when linking to MySQL in IDEA and no table display after successful connection
  • After installing Navicat in MySQL, 2059 appears, Authentication plugin and local link virtual machine docker, remote link server
  • Python pymysql link database query results converted to Dataframe instance
  • Detailed explanation of MySQL multi-table query examples [link query, subquery, etc.]
  • Example operation MySQL short link
  • Summary of the installation process of MySql 8.0.11 and the problems encountered when linking with Navicat
  • Detailed explanation of the use of MySQL group links
  • MySql uses skip-name-resolve to solve the problem of slow external network connection client
  • How to view MySQL links and kill abnormal links

<<:  Summary of Vue 3 custom directive development

>>:  Example of how to build a Mysql cluster with docker

Recommend

4 functions implemented by the transform attribute in CSS3

In CSS3, the transform function can be used to im...

How to backup and restore the mysql database if it is too large

Command: mysqlhotcopy This command will lock the ...

Solve the installation problem of mysql8.0.19 winx64 version

MySQL is an open source, small relational databas...

Several ways to encrypt and decrypt MySQL (summary)

Table of contents Written in front Two-way encryp...

30 minutes to give you a comprehensive understanding of React Hooks

Table of contents Overview 1. useState 1.1 Three ...

10 Website Usability Tips Everyone Should Know

Let’s not waste any more time and get straight to...

How to filter out duplicate data when inserting large amounts of data into MySQL

Table of contents 1. Discover the problem 2. Dele...

Vue Router vue-router detailed explanation guide

Chinese documentation: https://router.vuejs.org/z...

How to restore a database and a table from a MySQL full database backup

In the official MySQL dump tool, how can I restor...

Detailed steps to install Docker 1.8 on CentOS 7

Docker supports running on the following CentOS v...

How to implement interception of URI in nginx location

illustrate: Root and alias in location The root d...

Solution to data duplication when using limit+order by in MySql paging

Table of contents summary Problem Description Ana...

Implementing a simple carousel based on JavaScript

This article shares the specific code of JavaScri...