
许多时候当要使用mysql时,会遇到如下情况:
1. 信息比较重要,希望通信被加密。
2. 一些端口,比如3306端口,被路由器禁用。
对第一个问题的一个比较直接的解决办法就是更改mysql的代码,或者是使用一些证书,不过这种办法显然不是很简单。
相关学习视频教程推荐:mysql视频教程
这里要介绍另外一种方法,就是利用SSH通道来连接远程的Mysql,方法相当简单。
一、建立SSH通道
有道小P
有道小P,新一代AI全科学习助手,在学习中遇到任何问题都可以问我。
64 查看详情
只需要在本地键入如下命令:
ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.comThe command tells ssh to log in to 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.
二、连接Mysql
现在,你就可以通过本地连接远程的数据库了,就像访问本地的数据库一样。
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.
或者用Mysql Query Brower来访问Client的3307端口。
类似的,用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:127.0.0.1:3306 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.
以上就是详解如何通过SSH通道来访问MySQL的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/365862.html
微信扫一扫
支付宝扫一扫