通过SSH远程访问MySQL服务器

So you’ve got MySQL on your web server, but it’s only opened to local ports by default for security reasons. If you want to access your database from a client tool like the MySQL Query Browser, normally you’d have to open up access from your local IP address… but that’s not nearly as secure.

因此,您已经在Web服务器上安装了MySQL,但是出于安全原因,默认情况下仅对本地端口开放MySQL。 如果要通过MySQL查询浏览器之类的客户端工具访问数据库,通常必须从本地IP地址打开访问权限……但这并不安全。

So instead, we’ll just use port-forwarding through an SSH tunnel, so your MySQL client thinks it’s connecting to your localhost machine, but it’s really connecting to the other server through the tunnel.

因此,相反,我们将仅通过SSH隧道使用端口转发,因此您MySQL客户端认为它正在连接到本地主机,但实际上是通过该隧道连接到其他服务器。

If you are using the command line ssh, the command would look like this. (You can do the same thing graphically in Putty or SecureCRT options if you need to)

如果使用命令行ssh,则命令将如下所示。 (如果需要,可以在Putty或SecureCRT选项中以图形方式执行相同的操作)

ssh -L 3306:localhost:3306 [email protected]

ssh -L 3306:localhost:3306 [email protected]

The syntax is ssh -L <localport>hostname<remoteport> <username>@<servername>. We’re using localhost as the hostname because we are directly accessing the remote mysql server through ssh. You could also use this technique to port-forward through one ssh server to another server.

语法为ssh -L <本地端口>主机名<远程端口> <用户名> @ <服务器名>。 我们使用localhost作为主机名,因为我们直接通过ssh访问远程mysql服务器。 您还可以使用此技术将一台ssh服务器移植到另一台服务器。

If you already have mysql running on your local machine then you can use a different local port for the port-forwarding, and just set your client tools to access MySQL on a different port.

如果已经在本地计算机上运行mysql,则可以使用其他本地端口进行端口转发,只需将客户端工具设置为在其他端口*问MySQL。

通过SSH远程访问MySQL服务器

Once you’ve got the ssh tunnel going, you can open up MySQL Query Browser and enter in the details for your remote server, using localhost as the server host, and adjust the port to whatever you used.

ssh隧道启动后,您可以打开mysql查询浏览器,并使用localhost作为服务器主机,输入远程服务器的详细信息,并将端口调整为所使用的端口。

Once you get used to this method, you’ll wonder why you ever used phpmyadmin or the command line version.

一旦习惯了这种方法,您会想知道为什么要使用phpmyadmin或命令行版本。

翻译自: https://www.howtogeek.com/howto/ubuntu/access-your-mysql-server-remotely-over-ssh/