简体   繁体   English

PHP连接到本地主机上的MySQL,从远程请求

[英]php connecting to mysql on localhost, request from remote

I have a wamp server running on my local machine that is exposed to the outside world. 我在暴露于外界的本地计算机上运行着一个垃圾服务器。 I have several php scripts that allow an android app to access information contained in a mySQL database running on the same machine as the WAMP server. 我有几个php脚本,它们允许android应用访问与WAMP服务器在同一台计算机上运行的mySQL数据库中包含的信息。 When tested from inside my local network everything works fine. 从我的本地网络内部进行测试时,一切正常。 However when I try to run the php scripts from outside my local network there appears to be an error connecting to mySQL. 但是,当我尝试从本地网络外部运行php脚本时,似乎无法连接到mySQL。

connect.php connect.php

<?php
header('Access-Control-Allow-Origin: *'); 

/*** mysql hostname ***/
$hostname = '127.0.0.1';

/*** mysql username ***/
$username = 'chaosjr_user';

/*** mysql password ***/
$password = 'pass';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=chaos_jr", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

I'm almost certain it has something to do with the fact I'm referencing localhost from the outside world, but when I replace 127.0.0.1 with the IP address on my internal network or the external IP address it doesn't work from anywhere. 我几乎可以肯定这与我从外部引用本地主机有关,但是当我将127.0.0.1替换为内部网络或外部IP地址上的IP地址时,它将无法在任何地方使用。 Any suggestions? 有什么建议么?

EDIT: Just to clarify, I can get to my php scripts from the outside (I do have a static IP address) and get a response from them as long as that particular script does't have to query MySQL. 编辑:只是为了澄清,我可以从外部访问我的php脚本(我确实有一个静态IP地址),并从它们中获取响应,只要该特定脚本不必查询MySQL。 For example I can connect to XXX.XXX.XXX.XXX/myPHPscript.php just fine and everything looks good. 例如,我可以很好地连接到XXX.XXX.XXX.XXX/myPHPscript.php,一切看起来都很好。 But menus that are supposed to populate with SQL data are empty. 但是应该填充SQL数据的菜单为空。

You are correct - the address 127.0.0.1 is your local IP address and not the one you would connect with from the outside world. 您是正确的-地址127.0.0.1是您的本地IP地址,而不是您与外界连接的地址。

If you have a static IP address, you'll need to use that. 如果您有一个静态IP地址,则需要使用该地址。 You may find things tricky if you don't have a fixed IP address, or if you have a firewall that blocks the port you are using. 如果您没有固定的IP地址,或者您的防火墙阻止了您正在使用的端口,则可能会感到棘手。

You should still use 127.0.0.1 to connect to the DB since the PHP script and the DB runs in the same server as far as I understood. 据我所知,您仍然应该使用127.0.0.1连接数据库,因为PHP脚本和数据库在同一服务器上运行。 It doesn't care if your script is called from the outside, if the script and the DB are in the same server, 127.0.0.1 is the correct address. 不管是否从外部调用脚本,如果脚本和DB位于同一服务器中,则127.0.0.1是正确的地址。

If you do want your database to be available from outside your network (please be aware that your machine will need to be powered on at all times in order for the database to be available), you will need to configure a so called port forward on your router to your internal address. 如果您确实希望数据库可以从网络外部访问(请注意,您的计算机将始终需要通电才能使数据库可用),则需要在网络上配置一个所谓的端口转发您的路由器到您的内部地址。 Then in your app, make sure to use your external IP address, your router will then reroute the request to your machine. 然后在您的应用程序中,确保使用您的外部IP地址,然后您的路由器会将请求重新路由到您的计算机。

Also be aware that your MySQL will need to accept external connections, by default it binds to your localhost and will not listen to remote connections. 另外请注意,您的MySQL将需要接受外部连接,默认情况下,它将绑定到本地主机,并且不会侦听远程连接。 This is because localhost is bound to a special loopback interface of your OS and is not actually bound to the network card in your computer. 这是因为localhost绑定到操作系统的特殊回送接口 ,而实际上未绑定到计算机中的网卡。 You will need to bind MySQL to the IP address that is bound to the network card, so it can communicate to the outside world. 您将需要将MySQL绑定到与网卡绑定的IP地址,以便它可以与外界通信。

In other words, what you want is possible, but takes many different steps to work, mainly network/access related and how to configure this depends on your setup. 换句话说,您想要的是可能的,但是需要执行许多不同的步骤,主要是与网络/访问相关的,以及如何配置取决于您的设置。 So there's no ready-to-go answer for this, it going to take some trial and error. 因此,对此没有现成的答案,这将需要反复试验。 But basically it comes down to these steps: 但基本上可以归结为以下步骤:

  1. Configure a port forward for your MySQL port (default: tcp 3306) on your router. 在路由器上为MySQL端口配置端口转发(默认值:tcp 3306)。
  2. Make sure that your MySQL server is bound to your "external" (in most LAN's this is a 192.168.0.x address) IP address, so not to localhost or 127.0.0.1 (this is a setting in the my.ini configuration file). 确保您的MySQL服务器绑定到您的“外部”(在大多数LAN上是192.168.0.x地址)IP地址,而不是本地主机或127.0.0.1(这是my.ini配置文件中的设置) )。
  3. Make sure your app attempts a database on your remote IP address (the one you got from your ISP, so not your 127.0.0.1 or 192.168.0.x address!). 确保您的应用尝试在您的远程 IP地址(从ISP获得的数据库,而不是您的127.0.0.1或192.168.0.x地址!)上尝试数据库。

For the last step, it's also important that your ISP has given you a static IP adress. 对于最后一步,ISP提供给您静态IP地址也很重要。 Many ISP's use DHCP adresses, meaning every time your modem resets, your IP address can be different when it boots up again. 许多ISP使用DHCP地址,这意味着每次调制解调器重置时,您的IP地址在重新启动时都可能不同。 You can verify this with your ISP. 您可以通过ISP进行验证。 Always remember that hosting stuff in-house is usually not ideal, it's OK for micro-projects or startup projects, but consider off-site hosting if things get serious. 永远记住,内部托管通常不是理想的选择,对于微型项目或启动项目来说还可以,但是如果情况严重,请考虑使用异地托管。

In your PHP code it should stay 127.0.0.1 because the PHP and the MySQL run on the same server, which is your localhost at that IP. 在您的PHP代码中,它应该保持127.0.0.1,因为PHP和MySQL在同一服务器上运行,该服务器是该IP上的本地主机。 But contacting it is a lot harder. 但是联系它要困难得多。

Okay, so your local host is running on your computer right, so this can be reached by contacting 127.0.0.1 from that same computer only. 好的,您的本地主机正在您的计算机上运行,​​因此可以通过仅从同一台计算机联系127.0.0.1来实现。

If you try to contact it from another device, you have to look up the private IP address of the device that you want to contact. 如果尝试从另一台设备联系它,则必须查找要联系的设备的专用IP地址。 This can be something like 192.168.xx in most cases. 在大多数情况下,这可能类似于192.168.xx。

Now contacting this computer from outside your network is even harder because you will connect to your router first (via its public IP), and that router should forward you to your pc on a certain port (probably port 80). 现在,从网络外部联系这台计算机变得更加困难,因为您将首先连接到路由器(通过其公用IP),并且该路由器应将您转发到某个端口(可能是端口80)上的PC。 You have to make sure that you configured your router so it can forward to the correct computer in your network and to the correct port on that computer. 您必须确保配置了路由器,以便它可以转发到网络中正确的计算机以及该计算机上的正确端口。

But wait, there's more! 但是,等等,还有更多! Most ISPs (internet service providers) change your public IP address every few days for security reasons, making it even harder to maintain the right reference to this computer you're trying to reach. 出于安全原因,大多数ISP(互联网服务提供商)每隔几天就会更改您的公共IP地址,这使得维护您要访问的这台计算机的正确引用更加困难。

You can find your WAN IP (public IP) by visiting http://www.whatismyip.com and check your local IP by executing "ifconfig" in the shell, or "ipconfig" in the windows command prompt 您可以通过访问http://www.whatismyip.com找到WAN IP(公共IP),并通过在Shell中执行“ ifconfig”或在Windows命令提示符中执行“ ipconfig”来检查本地IP。

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

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