简体   繁体   English

如何从C#连接在Web服务器上运行的MySQL?

[英]How to connect MySQL running on a web server from c#?

I am working on an C# application which would use the remote MySQL database located in my website hosted on a Linux server with PHP & MySQL support. 我正在使用一个C#应用程序,该应用程序将使用位于我的网站上的远程MySQL数据库,该数据库托管在具有PHP和MySQL支持的Linux服务器上。

I tried to connect directly to the MySQL database, but was not able to connect due to restrictions at my hoster side. 我试图直接连接到MySQL数据库,但是由于托管方的限制而无法连接。

can somebody help me please, can i do that with this restrictions ? 有人可以帮我吗,我可以用这个限制吗?

You can't. 你不能 The server needs to have allowed remote connections and if this isn't possible, than you're out of your luck. 服务器需要允许远程连接,如果这不可能,那么您就不走运了。

But, there may be another way... If you can create a web service on the server you're able to connect from, you should be able to communicate with the data in mysql virtually from anywhere. 但是,可能还有另一种方式...如果您可以在能够连接的服务器上创建Web服务,则您应该几乎可以在任何地方与mysql中的数据进行通信。

You need access to port 3306 on the remote machine. 您需要访问远程计算机上的端口3306。 You can test if you have access to this port using telnet or similar 您可以使用telnet或类似工具测试是否可以访问此端口

telnet ip 3306

These are the solutions you have: 这些是您拥有的解决方案:

A) Create local dev environment : You don't provide much information. A) 创建本地开发环境 :您没有提供太多信息。 If my guess is correct you are developing locally and later you plan to deploy your c# application to web server, that also will contain mysql db. 如果我的猜测是正确的,则说明您是在本地进行开发,后来计划将c#应用程序部署到Web服务器,该服务器还将包含mysql db。

  • Install mysql in your local machine 在本地计算机上安装mysql
  • Get a copy of the DB. 获取数据库的副本。
  • Configure your C# program to connect to your local mysql (localhost:3306) 配置您的C#程序以连接到本地mysql(localhost:3306)
  • When you deploy code it to your web server, it will connect also to your "localhost 3306" that will be the mysql installed into the web server. 将代码部署到Web服务器时,它还将连接到“ localhost 3306”,这将是安装在Web服务器中的mysql。

B) Use VPN to access mysql : If A is not applicable another solution consist in installing some kind of VPN between web server containing mySQL and the pc running your application. B) 使用VPN访问mysql :如果不适用A,则另一种解决方案是在包含mySQL的Web服务器和运行您的应用程序的PC之间安装某种VPN。 Once VPN is setup and you can access port 3306 (by telnet as explained before), then your app will work. 设置VPN后,您就可以访问端口3306(通过telnet进行访问,如前所述),然后您的应用即可正常工作。

C) Open ports to access mysql : The most easy solution is asking for administrator to open ports for you. C) 打开访问mysql的端口 :最简单的解决方案是要求管理员为您打开端口。 99% of the times the answer will be no, so I will not follow this route. 99%的答案是“否”,所以我不会遵循这条路线。 (Maybe there is some kind of solution in your web provider that allows to open specific ports for a given IP, but I doubt it) (也许您的网络提供商中提供了某种解决方案,可以为给定IP打开特定端口,但我对此表示怀疑)

您可以尝试创建一种API的方法,如果您的服务器阻止了来自Web的传入连接,只需重载您需要与PHP一起使用的MySQL函数,并将“ echo”结果作为服务器,然后将其作为c#应用程序作为客户端进行解析,组织这种系统的唯一方法,或者,您可以允许传入连接到MySQL,然后在CodeProject上观看本文

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

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