简体   繁体   English

python从第三台机器连接到mysql

[英]python connect to mysql from the third machine

Imagine that: 想象一下:

I have three machine. 我有三台机器。 M1 has my client program.M3 is mysql server. M1有我的客户端程序。M3是mysql服务器。 But M1 can not connect M3 directly. 但是M1无法直接连接M3。 M1 can access M2 using ssh or something else directly. M1可以直接使用ssh或其他方式访问M2。 M2 can access M3 directly. M2可以直接访问M3。 Well, how do client program on M1 to connect mysql server M3 through M2 using python and execute sql, such as select clause . 那么,如何在M1上的客户端程序使用python通过M2通过M2连接mysql服务器M3并执行sql,例如select clause Is it possible and how to do that? 是否有可能以及如何做到这一点? Thanks. 谢谢。

Maybe MySQL Proxy can be useful in your case - it's only an alpha. 也许MySQL Proxy在您的情况下可能有用-它只是一个alpha。

In another way, you can bing the port of mysql of M3 on M2 using SSH port forwarding . 换句话说,您可以使用SSH端口转发在M2上ping M3的mysql 端口

The default TCP port for MySQL is 3306 . MySQL的默认TCP端口是3306 The relevant SSH command line would be 相关的SSH命令行为

ssh M2 -L M1_port:M3:3306

(where you need to substitute M2 with the hostname/IP address of M2 and have M1_port as some previously unused port at M1, for example 3306 if you don't run an MySQL server at M1) (在这里,您需要用M2的主机名/ IP地址替换M2,并将M1_port用作M1上某些以前未使用的端口,例如,如果您不在M1上运行MySQL服务器,则使用3306)

Your MySQL client at M1 should connect to M1/localhost via port M1_port , and all of this traffic will be tunnelled to M3 at port 3306. 您的M1上的MySQL客户端应通过端口M1_port连接到M1 / localhost,所有这些流量都将通过端口3306传输到M3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM