Skip to content

Instantly share code, notes, and snippets.

@jpzhu
Last active November 15, 2017 03:30
Show Gist options
  • Save jpzhu/6baa988a063aba87bc56c638a0550198 to your computer and use it in GitHub Desktop.
Save jpzhu/6baa988a063aba87bc56c638a0550198 to your computer and use it in GitHub Desktop.
mysql ssh vpn
http://mingxinglai.com/cn/2015/09/connect-mysql-via-ssh-tunnel/
使用SSH隧道连接MYSQL
https://blog.fundebug.com/2017/04/24/ssh-port-forwarding/
玩转SSH端口转发
MySQL 使用ssh连接例子
ssh -fCPN -L localhost:8964:127.0.0.1:3306 ss-manager
/usr/local/mysql/bin/mysql -hlocalhost -uroot --port=8964 -p'你的密码'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
In MySQL, the localhost keyword is reserved for connection using the MySQL socket
and you should use the ip-address 127.0.0.1 for TCP connections to the MySQL network port on 127.0.0.1.
This means that both the server must grant privileges to users from specifically 127.0.0.1,
and the client must use -h 127.0.0.1 to go through the tunnel instead of connecting to a local socket.
https://serverfault.com/questions/614094/mysql-access-denied-error-when-connecting-via-ssh-tunnel 原始答案
【一句话:localhost是给本地用的,要用TCP转发,请用127.0.0.1】
【以下为正常语句,运行前貌似要先启动Mysql进程,不确认】
/usr/local/mysql/bin/mysql -h127.0.0.1 -uroot --port=8964 -p'你的密码'
@jpzhu
Copy link
Author

jpzhu commented Nov 15, 2017

上面那个玩转ssh端口转发的文章例子中远程转发里。
在远端机器上执行 localhost:2000 可以放到到本地机器的开放的业务。
如果在其它机器上访问远端机器公网IP:2000端口,此时无法正常进行数据转发。 需要修改sshd_config里的转发配置
GatewayPorts yes
参考 https://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html 这个文章

@jpzhu
Copy link
Author

jpzhu commented Nov 15, 2017

利用comment中端口实现成功后。 又发现一个问题
curl 命令可以拿到输出。 但在浏览器里输入ip:port 则没有输出。 原因是数据请求要求的格式有关
参考 knuton/stubb#13
The reason it doesn't work from your browser is probably that it sends an accept header something like:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Whereas curl (and presumably the Chrome extension) asks for:

Accept: */*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment