Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using SSH channel to access MySQL

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report