Get the App
SLTechnology News&Howtos  ›  Database  › 

The method of using SSH channel to access MySQL

Shulou Source: shulou.com Published: 2022-06-01 18:56:37 10月12日 Update

Many times when you want to use Mysql, you will encounter the following situations:

1. The information is more important and we want the communication to be encrypted.

two。 Some ports, such as port 3306, are disabled by the router.

A more straightforward solution to the first problem is to change the mysql code, or to use some certificates, but this is obviously not very simple.

Related learning video tutorials recommended: mysql video tutorials

Another method to be introduced here is to use the SSH channel to connect to the remote Mysql, which is quite simple.

First, establish a SSH channel

Just type the following command locally:

Ssh-fNg-L 3307 myuser@remotehost.comThe command tells ssh to log into remotehost.com as myuser, go into the background (- f) and not execute any remote command (- N), and set up port-forwarding (- L localport:localhost:remoteport). In this case, we forward port 3307 on localhost to port 3306 on remotehost.com.

Second, connect to Mysql

Now you can connect to a remote database locally, just like accessing a local database.

Mysql-h 127.0.0.1-P 3307-u dbuser-p dbThe command tells the local MySQL client to connect to localhost port 3307 (which is forwarded via ssh to remotehost.com:3306). The exchange of data between client and server is now sent over the encrypted ssh connection.

Or use Mysql Query Brower to access port 3307 of Client.

Similarly, access it with PHP:

Making It A DaemonA quick and dirty way to make sure the connection runs on startup and respawns on failure is to add it to / etc/inittab and have the init process (the, uh, kernel) keep it going.Add the following to / etc/inittab on each client:sm:345:respawn:/usr/bin/ssh-Ng-L 3307 myuser@remotehost.comAnd that should be all you need to do. Send init the HUP signal (kill-HUP 1) to make it reload the configuration. To turn it off, comment out the line and HUP init again.

The above is the details of how to access MySQL through the SSH channel, please pay attention to other related articles!

Tags: Channels that is ports methods methods tutorials data databases videos important code information content can be passed commands situations articles times more certificates Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Linux MariaDB Shulou Information