在2010年用Amazon AWS时,就用过ssh命令来建立一个SSH Tunnel,然后本地配置为代理就可以无障碍访问互联网了。 现在连接一个IDC的MySQL服务器时,由于网络的隔离性,需要ssh到一台跳板机上才能直接访问MySQL,而我想在本地使用MySQLWorkbench这样的客户端直
在2010年用Amazon AWS时,就用过ssh命令来建立一个SSH Tunnel,然后本地配置为代理就可以无障碍访问互联网了。
现在连接一个IDC的MySQL服务器时,由于网络的隔离性,需要ssh到一台跳板机上才能直接访问MySQL,而我想在本地使用MySQLWorkbench这样的客户端直接连接到MySQL中。其解决方案是,先用ssh命令建立一个SSH Tunnel,然后MySQLWorkbench只需要连接本地的一个端口,即可与对应的远程MySQL建立连接。
假设跳板机为:192.168.1.1 (它可以与MySQL连通)
MySQL IP为:10.1.0.58 端口为:3306
建立SSH Tunnel的命令如下:
<span style="color: #666666; font-style: italic;"># 建立SSH Tunnel</span> jay<span style="color: #000000; font-weight: bold;">@</span>Jay-Air:~ <span style="color: #007800;">$ssh</span> <span style="color: #660033;">-f</span> jay<span style="color: #000000; font-weight: bold;">@</span>192.168.1.1 <span style="color: #660033;">-L</span> <span style="color: #000000;">2001</span>:10.1.0.58:<span style="color: #000000;">3306</span> <span style="color: #660033;">-N</span> Warning: Permanently added <span style="color: #ff0000;">'192.168.1.1'</span> <span style="color: #7a0874; font-weight: bold;">(</span>RSA<span style="color: #7a0874; font-weight: bold;">)</span> to the list of known hosts. jay<span style="color: #000000; font-weight: bold;">@</span>192.168.1.1<span style="color: #ff0000;">'s password: # 查一下在后台运行的ssh进程 jay@Jay-Air:~ $ps -ef | grep '</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-f</span><span style="color: #ff0000;">' | grep -v grep 501 9729 1 0 6:10PM ?? 0:00.19 ssh -f jay@192.168.1.1 -L 2001:10.1.0.58:3306 -N</span>
ssh命令中,-f 表示让ssh在执行命令(这里没有配置执行任何命令)之前进入后台运行状态; -L 指定本地的一个端口(如这里的2001),它将会被转发到远程IP的一个端口(如:10.1.0.58:3306);-N 表示不执行远程命令,对于只做端口转发的场景非常有用。
在MySQLWorkbench中,将原来连接MySQL的IP和端口,配置为设置的本地SSH Tunnel即可(这里设置的是:localhost:2001)
补充说一下,在MySQLWorkbench中一直使用是正常的,而在用mysql命令连接时遇到了问题,如下:
jay<span style="color: #000000; font-weight: bold;">@</span>jay-linux:~$ mysql <span style="color: #660033;">-P</span> <span style="color: #000000;">2001</span> <span style="color: #660033;">-u</span> qa <span style="color: #660033;">-p</span> Enter password: ERROR <span style="color: #000000;">1045</span> <span style="color: #7a0874; font-weight: bold;">(</span><span style="color: #000000;">28000</span><span style="color: #7a0874; font-weight: bold;">)</span>: Access denied <span style="color: #000000; font-weight: bold;">for</span> user <span style="color: #ff0000;">'qa'</span><span style="color: #000000; font-weight: bold;">@</span><span style="color: #ff0000;">'localhost'</span> <span style="color: #7a0874; font-weight: bold;">(</span>using password: YES<span style="color: #7a0874; font-weight: bold;">)</span>
出现这个原因是,这样的命令默认连接localhost,会默认使用Unix socket方式连接;而使用SSH Tunnel这种情况应该使用TCP协议连接。
(mysql官网中的原文:For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. )
所以,解决方法有如下两种:
1. 在mysql命令中添加“–protocol=TCP”参数指定连接协议,命令为:mysql -P 2001 -u qa -p –protocol=TCP
2. 将用127.0.0.1 IP来代替默认的localhost,命令为:mysql -h 127.0.0.1 -P 2001 -u qa -p
另外,参考资料中的第二个,讲了如何在Windows下用putty设置建立SSH Tunnel。
参考资料:
http://www.revsys.com/writings/quicktips/ssh-tunnel.html
http://davidngos.blogspot.com/2013/06/how-to-use-mysql-workbench-behind.html
http://dev.mysql.com/doc/refman/5.6/en/connecting.html
Original article: MySQLWorkbench使用SSH Tunnel连接MySQL数据库
©2014 笑遍世界. All Rights Reserved.

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
